Skip to content

Commit

Permalink
Updated Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickRitchie committed Dec 11, 2023
1 parent ce47711 commit e09844b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void Run(string[] args, bool isBlocking = false)
var configuration = OnConfigurationFileRead(configFile);
if (configuration != null)
{
_agentLogger.Info($"Configuration File Read Successfully from: {configuration.Path}");
_applicationLogger.Info($"Configuration File Read Successfully from: {configuration.Path}");

// Set Service Name
if (!string.IsNullOrEmpty(configuration.ServiceName)) serviceDisplayName = configuration.ServiceName;
Expand All @@ -132,7 +132,7 @@ public void Run(string[] args, bool isBlocking = false)
}
else
{
_agentLogger.Warn("Error Reading Configuration File. Default Configuration is loaded.");
_applicationLogger.Warn("Error Reading Configuration File. Default Configuration is loaded.");

configuration = new AgentApplicationConfiguration();
}
Expand Down Expand Up @@ -561,16 +561,16 @@ private void StartMetrics()
var observationAverage = _mtconnectAgent.Metrics.ObservationAverage;
observationDelta = observationCount - observationLastCount;

_agentMetricLogger.Info("[Agent] : Observations - Delta for last " + updateInterval + " seconds: " + observationDelta);
_agentMetricLogger.Info("[Agent] : Observations - Average for last " + windowInterval + " minutes: " + Math.Round(observationAverage, 5));
_agentMetricLogger.Debug("Observations - Delta for last " + updateInterval + " seconds: " + observationDelta);
_agentMetricLogger.Debug("Observations - Average for last " + windowInterval + " minutes: " + Math.Round(observationAverage, 5));

// Assets
var assetCount = _mtconnectAgent.Metrics.GetAssetCount();
var assetAverage = _mtconnectAgent.Metrics.AssetAverage;
assetDelta = assetCount - assetLastCount;

_agentMetricLogger.Info("[Agent] : Assets - Delta for last " + updateInterval + " seconds: " + assetDelta);
_agentMetricLogger.Info("[Agent] : Assets - Average for last " + windowInterval + " minutes: " + Math.Round(assetAverage, 5));
_agentMetricLogger.Debug("Assets - Delta for last " + updateInterval + " seconds: " + assetDelta);
_agentMetricLogger.Debug("Assets - Average for last " + windowInterval + " minutes: " + Math.Round(assetAverage, 5));

observationLastCount = observationCount;
assetLastCount = assetCount;
Expand Down Expand Up @@ -660,7 +660,6 @@ private void ModuleLogReceived(object sender, MTConnectLogLevel logLevel, string

var logEvent = new LogEventInfo();
logEvent.LoggerName = loggerId;
//logEvent.Properties["logId"] = loggerId;
logEvent.Message = message;

switch (logLevel)
Expand All @@ -686,15 +685,38 @@ private void ProcessorLogReceived(object sender, MTConnectLogLevel logLevel, str
{
if (!string.IsNullOrEmpty(message))
{
var processor = (IMTConnectAgentProcessor)sender;

var loggerId = logId;
if (string.IsNullOrEmpty(loggerId)) loggerId = processor.Id;
loggerId = $"processors.{loggerId.Replace(' ', '-')}".ToLower();

Logger logger;
lock (_lock)
{
_loggers.TryGetValue(loggerId, out logger);
if (logger == null)
{
logger = LogManager.GetLogger(loggerId);
_loggers.Add(loggerId, logger);
}
}

var logEvent = new LogEventInfo();
logEvent.LoggerName = loggerId;
logEvent.Message = message;

switch (logLevel)
{
case MTConnectLogLevel.Fatal: _processorLogger.Fatal(message); break;
case MTConnectLogLevel.Error: _processorLogger.Error(message); break;
case MTConnectLogLevel.Warning: _processorLogger.Warn(message); break;
case MTConnectLogLevel.Information: _processorLogger.Info(message); break;
case MTConnectLogLevel.Debug: _processorLogger.Debug(message); break;
case MTConnectLogLevel.Trace: _processorLogger.Trace(message); break;
case MTConnectLogLevel.Fatal: logEvent.Level = LogLevel.Fatal; break;
case MTConnectLogLevel.Error: logEvent.Level = LogLevel.Error; break;
case MTConnectLogLevel.Warning: logEvent.Level = LogLevel.Warn; break;
case MTConnectLogLevel.Information: logEvent.Level = LogLevel.Info; break;
case MTConnectLogLevel.Debug: logEvent.Level = LogLevel.Debug; break;
case MTConnectLogLevel.Trace: logEvent.Level = LogLevel.Trace; break;
}

logger.Log(logEvent);
}
}

Expand Down
4 changes: 2 additions & 2 deletions agent/MTConnect.NET-Applications-Agents/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<rules>

<!--Write to Console-->
<logger name="*" minlevel="Debug" writeTo="logconsole" />
<logger name="*" minlevel="Info" writeTo="logconsole" />

<!--Application Logger-->
<logger name="application-logger" minlevel="Info" writeTo="application-file" final="true" />
Expand All @@ -48,7 +48,7 @@
<logger name="modules.*" minlevel="Info" writeTo="module-file" final="true" />

<!--Processor Logger-->
<logger name="processor-logger" minlevel="Info" writeTo="processor-file" final="true" />
<logger name="processors.*" minlevel="Info" writeTo="module-file" final="true" />

<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ public Processor(object configuration)
_configuration = AgentApplicationConfiguration.GetConfiguration<ProcessorConfiguration>(configuration);
if (_configuration == null) _configuration = new ProcessorConfiguration();

Load();

StartWatcher();
}


private void Load()
public override void Load()
{
lock (_lock) _functions.Clear();

Expand Down Expand Up @@ -75,14 +73,14 @@ private void LoadEngine(string file)
var process = scope.GetVariable<Func<ProcessObservation, ProcessObservation>>(_functionName);
if (process != null)
{
Log(Logging.MTConnectLogLevel.Information, $"[Python-Processor] : Script Loaded : {file}");
Log(Logging.MTConnectLogLevel.Debug, $"Python Script Loaded : {file}");

AddFunction(file, process);
}
}
catch (Exception ex)
{
Log(Logging.MTConnectLogLevel.Error, $"[Python-Processor] : Error Loading Script : {file} : {ex.Message}");
Log(Logging.MTConnectLogLevel.Error, $"Error Loading Python Script : {file} : {ex.Message}");
}
}

Expand Down Expand Up @@ -111,7 +109,7 @@ protected override IObservationInput OnProcess(ProcessObservation observation)
}
catch (Exception ex)
{
Log(Logging.MTConnectLogLevel.Error, $"[Python-Processor] : Process Error : {functionKey} : {ex.Message}");
Log(Logging.MTConnectLogLevel.Error, $"Process Error : {functionKey} : {ex.Message}");
}
}
}
Expand All @@ -121,7 +119,7 @@ protected override IObservationInput OnProcess(ProcessObservation observation)
}
catch (Exception ex)
{
Log(Logging.MTConnectLogLevel.Error, $"[Python-Processor] : Error During Process : {ex.Message}");
Log(Logging.MTConnectLogLevel.Error, $"Error During Process : {ex.Message}");
}

if (outputObservation != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public interface IMTConnectAgentProcessor
event MTConnectLogEventHandler LogReceived;


void Load();


IObservationInput Process(ProcessObservation observation);

IAsset Process(IAsset asset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public abstract class MTConnectAgentProcessor : IMTConnectAgentProcessor
public event MTConnectLogEventHandler LogReceived;


public virtual void Load() { }


public IObservationInput Process(ProcessObservation observation) => OnProcess(observation);

public IAsset Process(IAsset asset) => OnProcess(asset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void Load()
// Create new Instance of the Controller and add to cached dictionary
var processor = (IMTConnectAgentProcessor)Activator.CreateInstance(processorType, new object[] { processorConfiguration });
processor.LogReceived += HandleProcessorLogReceived;
processor.Load();

var processorId = Guid.NewGuid().ToString();

Expand Down

0 comments on commit e09844b

Please sign in to comment.