Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
add method responses, fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hansgschossmann committed Jan 15, 2019
1 parent 0985e54 commit 6e4def2
Show file tree
Hide file tree
Showing 19 changed files with 1,644 additions and 1,009 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ OPC Publisher implements the following IoTHub direct method calls, which can be
- GetConfiguredNodesOnEndpoint
- GetDiagnosticInfo
- GetDiagnosticLog
- GetDiagnosticStartupLog
- ExitApplication
- GetInfo

Expand Down
2 changes: 1 addition & 1 deletion opcpublisher/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[assembly: System.Reflection.AssemblyConfiguration("Release")]
[assembly: System.Reflection.AssemblyDescription("Azure IoT Edge OPC Publisher Module")]
[assembly: System.Reflection.AssemblyFileVersion("2.3.0")]
[assembly: System.Reflection.AssemblyInformationalVersion("2.3.0")]
[assembly: System.Reflection.AssemblyInformationalVersion("2.3.0+Branch.develop_hans_methodlog.Sha.0985e54f01a0b0d7f143b1248936022ea5d749f9")]
[assembly: System.Reflection.AssemblyProduct("opcpublisher")]
[assembly: System.Reflection.AssemblyTitle("opcpublisher")]
[assembly: System.Reflection.AssemblyVersion("2.3.0.0")]
81 changes: 56 additions & 25 deletions opcpublisher/Diagnostics.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

using Serilog.Core;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Serilog.Core;

namespace OpcPublisher
{
using Serilog;
using Serilog.Configuration;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Globalization;
using static HubCommunication;
using static Program;
using static PublisherNodeConfiguration;
Expand All @@ -31,7 +31,7 @@ public static void Init()
// kick off the task to show diagnostic info
if (DiagnosticsInterval > 0)
{
_showDiagnosticsInfoTask = Task.Run(async () => await ShowDiagnosticsInfoAsync(_shutdownTokenSource.Token));
_showDiagnosticsInfoTask = Task.Run(async () => await ShowDiagnosticsInfoAsync(_shutdownTokenSource.Token).ConfigureAwait(false));
}


Expand All @@ -42,7 +42,7 @@ public async static Task ShutdownAsync()
if (_showDiagnosticsInfoTask != null)
{
_shutdownTokenSource.Cancel();
await _showDiagnosticsInfoTask;
await _showDiagnosticsInfoTask.ConfigureAwait(false);
}

_shutdownTokenSource = null;
Expand All @@ -59,10 +59,13 @@ public static DiagnosticInfoMethodResponseModel GetDiagnosticInfo()
try
{
diagnosticInfo.PublisherStartTime = PublisherStartTime;
diagnosticInfo.NumberOfOpcSessions = NumberOfOpcSessions;
diagnosticInfo.NumberOfConnectedOpcSessions = NumberOfConnectedOpcSessions;
diagnosticInfo.NumberOfConnectedOpcSubscriptions = NumberOfConnectedOpcSubscriptions;
diagnosticInfo.NumberOfMonitoredItems = NumberOfMonitoredItems;
diagnosticInfo.NumberOfOpcSessionsConfigured = NumberOfOpcSessionsConfigured;
diagnosticInfo.NumberOfOpcSessionsConnected = NumberOfOpcSessionsConnected;
diagnosticInfo.NumberOfOpcSubscriptionsConfigured = NumberOfOpcSubscriptionsConfigured;
diagnosticInfo.NumberOfOpcSubscriptionsConnected = NumberOfOpcSubscriptionsConnected;
diagnosticInfo.NumberOfOpcMonitoredItemsConfigured = NumberOfOpcMonitoredItemsConfigured;
diagnosticInfo.NumberOfOpcMonitoredItemsMonitored = NumberOfOpcMonitoredItemsMonitored;
diagnosticInfo.NumberOfOpcMonitoredItemsToRemove = NumberOfOpcMonitoredItemsToRemove;
diagnosticInfo.MonitoredItemsQueueCapacity = MonitoredItemsQueueCapacity;
diagnosticInfo.MonitoredItemsQueueCount = MonitoredItemsQueueCount;
diagnosticInfo.EnqueueCount = EnqueueCount;
Expand Down Expand Up @@ -93,15 +96,13 @@ public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticLogAsync
DiagnosticLogMethodResponseModel diagnosticLogMethodResponseModel = new DiagnosticLogMethodResponseModel();
diagnosticLogMethodResponseModel.MissedMessageCount = _missedMessageCount;
diagnosticLogMethodResponseModel.LogMessageCount = _logMessageCount;
diagnosticLogMethodResponseModel.StartupLogMessageCount = _startupLog.Count;

if (StartupCompleted)
if (DiagnosticsInterval >= 0)
{
List<string> log = new List<string>();

if (DiagnosticsInterval >= 0)
if (StartupCompleted)
{
await _logQueueSemaphore.WaitAsync();
List<string> log = new List<string>();
await _logQueueSemaphore.WaitAsync().ConfigureAwait(false);
try
{
string message;
Expand All @@ -116,15 +117,46 @@ public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticLogAsync
_missedMessageCount = 0;
_logQueueSemaphore.Release();
}
diagnosticLogMethodResponseModel.Log.AddRange(log);
}
else
{
diagnosticLogMethodResponseModel.Log.Add("Startup is not yet completed. Please try later.");
}
}
else
{
diagnosticLogMethodResponseModel.Log.Add("Diagnostic log is disabled. Please use --di to enable it.");
}

return diagnosticLogMethodResponseModel;
}

/// <summary>
/// Fetch diagnostic startup log data.
/// </summary>
public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticStartupLogAsync()
{
DiagnosticLogMethodResponseModel diagnosticLogMethodResponseModel = new DiagnosticLogMethodResponseModel();
diagnosticLogMethodResponseModel.MissedMessageCount = 0;
diagnosticLogMethodResponseModel.LogMessageCount = _startupLog.Count;

if (DiagnosticsInterval >= 0)
{
if (StartupCompleted)
{
diagnosticLogMethodResponseModel.Log.AddRange(_startupLog);
}
else
{
_startupLog.Add("Diagnostic log is disabled in OPC Publisher. Please use --di to enable it.");
log.Add("Diagnostic log is disabled in OPC Publisher. Please use --di to enable it.");
diagnosticLogMethodResponseModel.Log.Add("Startup is not yet completed. Please try later.");
}
diagnosticLogMethodResponseModel.StartupLog = _startupLog.ToArray();
diagnosticLogMethodResponseModel.Log = log.ToArray();
}
else
{
diagnosticLogMethodResponseModel.Log.Add("Diagnostic log is disabled. Please use --di to enable it.");
}

return diagnosticLogMethodResponseModel;
}

Expand All @@ -142,16 +174,15 @@ public static async Task ShowDiagnosticsInfoAsync(CancellationToken ct)

try
{
await Task.Delay((int)DiagnosticsInterval * 1000, ct);
await Task.Delay(DiagnosticsInterval * 1000, ct).ConfigureAwait(false);

DiagnosticInfoMethodResponseModel diagnosticInfo = GetDiagnosticInfo();
Logger.Information("==========================================================================");
Logger.Information($"OpcPublisher status @ {System.DateTime.UtcNow} (started @ {diagnosticInfo.PublisherStartTime})");
Logger.Information("---------------------------------");
Logger.Information($"OPC sessions: {diagnosticInfo.NumberOfOpcSessions}");
Logger.Information($"connected OPC sessions: {diagnosticInfo.NumberOfConnectedOpcSessions}");
Logger.Information($"connected OPC subscriptions: {diagnosticInfo.NumberOfConnectedOpcSubscriptions}");
Logger.Information($"OPC monitored items: {diagnosticInfo.NumberOfMonitoredItems}");
Logger.Information($"OPC sessions (configured/connected): {diagnosticInfo.NumberOfOpcSessionsConfigured}/{diagnosticInfo.NumberOfOpcSessionsConnected}");
Logger.Information($"OPC subscriptions (configured/connected): {diagnosticInfo.NumberOfOpcSubscriptionsConfigured}/{diagnosticInfo.NumberOfOpcSubscriptionsConnected}");
Logger.Information($"OPC monitored items (configured/monitored/to remove): {diagnosticInfo.NumberOfOpcMonitoredItemsConfigured}/{diagnosticInfo.NumberOfOpcMonitoredItemsMonitored}/{diagnosticInfo.NumberOfOpcMonitoredItemsToRemove}");
Logger.Information("---------------------------------");
Logger.Information($"monitored items queue bounded capacity: {diagnosticInfo.MonitoredItemsQueueCapacity}");
Logger.Information($"monitored items queue current items: {diagnosticInfo.MonitoredItemsQueueCount}");
Expand Down Expand Up @@ -261,7 +292,7 @@ public void Emit(LogEvent logEvent)

private string FormatMessage(LogEvent logEvent)
{
return $"[{logEvent.Timestamp:T} {logEvent.Level.ToString().Substring(0, 3).ToUpper()}] {logEvent.RenderMessage()}";
return $"[{logEvent.Timestamp:T} {logEvent.Level.ToString().Substring(0, 3).ToUpper(CultureInfo.InvariantCulture)}] {logEvent.RenderMessage()}";
}

private List<string> FormatException(LogEvent logEvent)
Expand All @@ -271,7 +302,7 @@ private List<string> FormatException(LogEvent logEvent)
{
exceptionLog = new List<string>();
exceptionLog.Add(logEvent.Exception.Message);
exceptionLog.Add(logEvent.Exception.StackTrace.ToString());
exceptionLog.Add(logEvent.Exception.StackTrace.ToString(CultureInfo.InvariantCulture));
}
return exceptionLog;
}
Expand Down
Loading

0 comments on commit 6e4def2

Please sign in to comment.