Skip to content

Added logging that helped me a lot with debugging automated tests. #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions libraries/MTConnect.NET-Common/Adapters/MTConnectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using MTConnect.Input;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MTConnect.Logging;

namespace MTConnect.Adapters
{
Expand All @@ -20,6 +21,7 @@ public class MTConnectAdapter : IMTConnectAdapter
private readonly object _lock = new object();
private readonly int? _interval;
private readonly bool _bufferEnabled;
private readonly ILogger _logger;

private readonly Dictionary<string, IDeviceInput> _currentDevices = new Dictionary<string, IDeviceInput>();
private readonly Dictionary<string, IDeviceInput> _lastDevices = new Dictionary<string, IDeviceInput>();
Expand Down Expand Up @@ -80,10 +82,14 @@ public class MTConnectAdapter : IMTConnectAdapter
public event EventHandler<AdapterEventArgs<string>> SendError;


public MTConnectAdapter(int? interval = null, bool bufferEnabled = false)
public MTConnectAdapter(
int? interval = null,
bool bufferEnabled = false,
ILogger logger = null)
{
_interval = interval;
_bufferEnabled = bufferEnabled;
_logger = logger;

FilterDuplicates = true;
OutputTimestamps = true;
Expand Down Expand Up @@ -304,6 +310,14 @@ public void AddObservation(IObservationInput observation)

_observationsBuffer.Add(CreateUniqueId(newObservation), newObservation);

if (_logger?.IsEnabled(LogLevel.Trace) == true)
{
var values = string.Join(",", observation.Values.Select(v => $"{v.Key}/{v.Value}"));
_logger?.LogTrace(
$"{nameof(MTConnectAdapter)}:{DateTime.Now.ToString("hh:mm:ss.ffffff")} Data item added in adapter {newObservation.DataItemKey}:{values}.");

}

// Call Overridable Method
OnObservationAdd(newObservation);
}
Expand Down Expand Up @@ -446,6 +460,8 @@ protected bool WriteChangedObservations()

if (!dataItems.IsNullOrEmpty())
{
LogReporter.Report($"Sending {dataItems.Count} changed items...", _logger);

var success = Write(dataItems);
if (success)
{
Expand Down Expand Up @@ -505,6 +521,8 @@ public bool WriteBufferObservations(int count = 1000)
sendObservations.Add(sendObservation);
}

LogReporter.Report($"Sending {sendObservations.Count} buffered items...", _logger);

var success = Write(sendObservations);
if (success)
{
Expand Down
119 changes: 103 additions & 16 deletions libraries/MTConnect.NET-Common/Agents/MTConnectAgent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2024 TrakHound Inc., All Rights Reserved.
// TrakHound Inc. licenses this file to you under the MIT license.

using Microsoft.Extensions.Logging;
using MTConnect.Agents.Metrics;
using MTConnect.Assets;
using MTConnect.Configurations;
Expand All @@ -14,6 +15,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MTConnect.Logging;

namespace MTConnect.Agents
{
Expand Down Expand Up @@ -54,7 +56,7 @@ public class MTConnectAgent : IMTConnectAgent, IDisposable
private string _sender;
private System.Timers.Timer _informationUpdateTimer;
private bool _updateInformation;

protected readonly ILogger _logger;

#region "Properties"

Expand Down Expand Up @@ -200,14 +202,16 @@ public MTConnectAgent(
string uuid = null,
ulong instanceId = 0,
long deviceModelChangeTime = 0,
bool initializeAgentDevice = true
bool initializeAgentDevice = true,
ILogger logger = null
)
{
_uuid = !string.IsNullOrEmpty(uuid) ? uuid : Guid.NewGuid().ToString();
_instanceId = instanceId > 0 ? instanceId : CreateInstanceId();
_configuration = new AgentConfiguration();
_information = new MTConnectAgentInformation(_uuid, _instanceId, _deviceModelChangeTime);
_deviceModelChangeTime = deviceModelChangeTime;
_logger = logger;
_mtconnectVersion = MTConnectVersions.Max;
_version = GetAgentVersion();
InitializeAgentDevice(initializeAgentDevice);
Expand All @@ -218,14 +222,16 @@ public MTConnectAgent(
string uuid = null,
ulong instanceId = 0,
long deviceModelChangeTime = 0,
bool initializeAgentDevice = true
bool initializeAgentDevice = true,
ILogger logger = null
)
{
_uuid = !string.IsNullOrEmpty(uuid) ? uuid : Guid.NewGuid().ToString();
_instanceId = instanceId > 0 ? instanceId : CreateInstanceId();
_configuration = configuration != null ? configuration : new AgentConfiguration();
_information = new MTConnectAgentInformation(_uuid, _instanceId, _deviceModelChangeTime);
_deviceModelChangeTime = deviceModelChangeTime;
_logger = logger;
_mtconnectVersion = _configuration != null && _configuration.DefaultVersion != null ? _configuration.DefaultVersion : MTConnectVersions.Max;
_version = GetAgentVersion();
InitializeAgentDevice(initializeAgentDevice);
Expand Down Expand Up @@ -814,6 +820,15 @@ protected bool CheckCurrentObservation(string deviceUuid, IDataItem dataItem, IO

// Check Filters
var update = FilterPeriod(dataItem, timestamp, existingTimestamp);
if (!update)
{
if (_logger?.IsEnabled(LogLevel.Trace) == true)
{
_logger?.LogTrace(
$"FilterPeriod {dataItem.Id}: {observation.Timestamp.ToDateTime():o} & {existingTimestamp.ToDateTime():o} .");
}
}

if (update) update = FilterDelta(dataItem, observation, existingObservationInput);

// Update if Filters are passed or if the DataItem is set to Discrete
Expand Down Expand Up @@ -1648,7 +1663,11 @@ private string Remove3dSuffix(string s)
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(IDataItem dataItem, object value, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
IDataItem dataItem,
object value,
bool? convertUnits = null,
bool? ignoreCase = null)
{
if (dataItem != null && dataItem.Device != null && !string.IsNullOrEmpty(dataItem.Device.Uuid) && !string.IsNullOrEmpty(dataItem.Id))
{
Expand All @@ -1675,7 +1694,12 @@ public bool AddObservation(IDataItem dataItem, object value, bool? convertUnits
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(string deviceKey, string dataItemKey, object value, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
string deviceKey,
string dataItemKey,
object value,
bool? convertUnits = null,
bool? ignoreCase = null)
{
var input = new ObservationInput
{
Expand All @@ -1697,7 +1721,12 @@ public bool AddObservation(string deviceKey, string dataItemKey, object value, b
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(IDataItem dataItem, object value, long timestamp, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
IDataItem dataItem,
object value,
long timestamp,
bool? convertUnits = null,
bool? ignoreCase = null)
{
if (dataItem != null && dataItem.Device != null && !string.IsNullOrEmpty(dataItem.Device.Uuid) && !string.IsNullOrEmpty(dataItem.Id))
{
Expand Down Expand Up @@ -1725,7 +1754,13 @@ public bool AddObservation(IDataItem dataItem, object value, long timestamp, boo
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(string deviceKey, string dataItemKey, object value, long timestamp, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
string deviceKey,
string dataItemKey,
object value,
long timestamp,
bool? convertUnits = null,
bool? ignoreCase = null)
{
return AddObservation(new ObservationInput
{
Expand All @@ -1745,7 +1780,12 @@ public bool AddObservation(string deviceKey, string dataItemKey, object value, l
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(IDataItem dataItem, object value, DateTime timestamp, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
IDataItem dataItem,
object value,
DateTime timestamp,
bool? convertUnits = null,
bool? ignoreCase = null)
{
if (dataItem != null && dataItem.Device != null && !string.IsNullOrEmpty(dataItem.Device.Uuid) && !string.IsNullOrEmpty(dataItem.Id))
{
Expand Down Expand Up @@ -1773,7 +1813,13 @@ public bool AddObservation(IDataItem dataItem, object value, DateTime timestamp,
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(string deviceKey, string dataItemKey, object value, DateTime timestamp, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
string deviceKey,
string dataItemKey,
object value,
DateTime timestamp,
bool? convertUnits = null,
bool? ignoreCase = null)
{
return AddObservation(new ObservationInput
{
Expand All @@ -1794,7 +1840,13 @@ public bool AddObservation(string deviceKey, string dataItemKey, object value, D
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(string deviceKey, string dataItemKey, string valueKey, object value, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
string deviceKey,
string dataItemKey,
string valueKey,
object value,
bool? convertUnits = null,
bool? ignoreCase = null)
{
return AddObservation(new ObservationInput
{
Expand All @@ -1816,7 +1868,14 @@ public bool AddObservation(string deviceKey, string dataItemKey, string valueKey
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(string deviceKey, string dataItemKey, string valueKey, object value, long timestamp, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
string deviceKey,
string dataItemKey,
string valueKey,
object value,
long timestamp,
bool? convertUnits = null,
bool? ignoreCase = null)
{
return AddObservation(new ObservationInput
{
Expand All @@ -1838,7 +1897,14 @@ public bool AddObservation(string deviceKey, string dataItemKey, string valueKey
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(string deviceKey, string dataItemKey, string valueKey, object value, DateTime timestamp, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
string deviceKey,
string dataItemKey,
string valueKey,
object value,
DateTime timestamp,
bool? convertUnits = null,
bool? ignoreCase = null)
{
return AddObservation(new ObservationInput
{
Expand All @@ -1857,7 +1923,11 @@ public bool AddObservation(string deviceKey, string dataItemKey, string valueKey
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(IObservationInput observationInput, bool? ignoreTimestamp = null, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
IObservationInput observationInput,
bool? ignoreTimestamp = null,
bool? convertUnits = null,
bool? ignoreCase = null)
{
if (observationInput != null && !string.IsNullOrEmpty(observationInput.DeviceKey))
{
Expand All @@ -1876,7 +1946,12 @@ public bool AddObservation(IObservationInput observationInput, bool? ignoreTimes
/// <param name="convertUnits">Used to override the default configuration for the Agent to ConvertUnits</param>
/// <param name="ignoreCase">Used to override the default configuration for the Agent to IgnoreCase of the Value</param>
/// <returns>True if the Observation was added successfully</returns>
public bool AddObservation(string deviceKey, IObservationInput observationInput, bool? ignoreTimestamp = null, bool? convertUnits = null, bool? ignoreCase = null)
public bool AddObservation(
string deviceKey,
IObservationInput observationInput,
bool? ignoreTimestamp = null,
bool? convertUnits = null,
bool? ignoreCase = null)
{
if (observationInput != null)
{
Expand Down Expand Up @@ -1984,9 +2059,22 @@ public bool AddObservation(string deviceKey, IObservationInput observationInput,
OnObservationAdded(observation);

success = true;

if (_logger?.IsEnabled(LogLevel.Trace) == true)
{
var values = string.Join(",", observation.Values.Select(v => $"{v.Key}/{v.Value}"));
_logger?.LogTrace(
$"{nameof(MTConnectAgent)}:{DateTime.Now.ToString("hh:mm:ss.ffffff")} Data item added async in agent {observation.DataItemId}:{values} with success.");
}
}

LogReporter.Report($"Added data item {dataItem.Id} to buffer with {success}.", _logger);
}
else
{
LogReporter.Report($"Skipped adding data item {dataItem.Id}.", _logger);
success = true; // Return true if no update needed
}
else success = true; // Return true if no update needed
}

if (!validationResult.IsValid && InvalidObservationAdded != null)
Expand All @@ -2005,7 +2093,6 @@ public bool AddObservation(string deviceKey, IObservationInput observationInput,
return false;
}


/// <summary>
/// Add new Observations for DataItems to the Agent
/// </summary>
Expand Down
21 changes: 13 additions & 8 deletions libraries/MTConnect.NET-Common/Agents/MTConnectAgentBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;

namespace MTConnect.Agents
{
Expand Down Expand Up @@ -147,8 +148,9 @@ public MTConnectAgentBroker(
string uuid = null,
ulong instanceId = 0,
long deviceModelChangeTime = 0,
bool initializeAgentDevice = true
) : base(uuid, instanceId, deviceModelChangeTime, false)
bool initializeAgentDevice = true,
ILogger logger = null
) : base(uuid, instanceId, deviceModelChangeTime, false, logger)
{
var config = new AgentConfiguration();
//_deviceBuffer = new MTConnectDeviceBuffer();
Expand All @@ -163,8 +165,9 @@ public MTConnectAgentBroker(
string uuid = null,
ulong instanceId = 0,
long deviceModelChangeTime = 0,
bool initializeAgentDevice = true
) : base(configuration, uuid, instanceId, deviceModelChangeTime, false)
bool initializeAgentDevice = true,
ILogger logger = null
) : base(configuration, uuid, instanceId, deviceModelChangeTime, false, logger)
{
var config = configuration != null ? configuration : new AgentConfiguration();
//_deviceBuffer = new MTConnectDeviceBuffer();
Expand All @@ -180,8 +183,9 @@ public MTConnectAgentBroker(
string uuid = null,
ulong instanceId = 0,
long deviceModelChangeTime = 0,
bool initializeAgentDevice = true
) : base(uuid, instanceId, deviceModelChangeTime, false)
bool initializeAgentDevice = true,
ILogger logger = null
) : base(uuid, instanceId, deviceModelChangeTime, false, logger)
{
var config = new AgentConfiguration();
_observationBuffer = observationBuffer != null ? observationBuffer : new MTConnectObservationBuffer(config);
Expand All @@ -197,8 +201,9 @@ public MTConnectAgentBroker(
string uuid = null,
ulong instanceId = 0,
long deviceModelChangeTime = 0,
bool initializeAgentDevice = true
) : base(configuration, uuid, instanceId, deviceModelChangeTime, false)
bool initializeAgentDevice = true,
ILogger logger = null
) : base(configuration, uuid, instanceId, deviceModelChangeTime, false, logger)
{
var config = configuration != null ? configuration : new AgentConfiguration();
_observationBuffer = observationBuffer != null ? observationBuffer : new MTConnectObservationBuffer(config);
Expand Down
Loading