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

Commit 78b97c7

Browse files
Merge pull request #175 from Azure/develop_hans_methodlog
add method responses, fix warnings
2 parents 0985e54 + 6e4def2 commit 78b97c7

19 files changed

+1644
-1009
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ OPC Publisher implements the following IoTHub direct method calls, which can be
7575
- GetConfiguredNodesOnEndpoint
7676
- GetDiagnosticInfo
7777
- GetDiagnosticLog
78+
- GetDiagnosticStartupLog
7879
- ExitApplication
7980
- GetInfo
8081

opcpublisher/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[assembly: System.Reflection.AssemblyConfiguration("Release")]
44
[assembly: System.Reflection.AssemblyDescription("Azure IoT Edge OPC Publisher Module")]
55
[assembly: System.Reflection.AssemblyFileVersion("2.3.0")]
6-
[assembly: System.Reflection.AssemblyInformationalVersion("2.3.0")]
6+
[assembly: System.Reflection.AssemblyInformationalVersion("2.3.0+Branch.develop_hans_methodlog.Sha.0985e54f01a0b0d7f143b1248936022ea5d749f9")]
77
[assembly: System.Reflection.AssemblyProduct("opcpublisher")]
88
[assembly: System.Reflection.AssemblyTitle("opcpublisher")]
99
[assembly: System.Reflection.AssemblyVersion("2.3.0.0")]

opcpublisher/Diagnostics.cs

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

2+
using Serilog.Core;
23
using System.Diagnostics;
34
using System.Threading;
45
using System.Threading.Tasks;
5-
using Serilog.Core;
66

77
namespace OpcPublisher
88
{
99
using Serilog;
1010
using Serilog.Configuration;
1111
using Serilog.Events;
12-
using System;
1312
using System.Collections.Generic;
13+
using System.Globalization;
1414
using static HubCommunication;
1515
using static Program;
1616
using static PublisherNodeConfiguration;
@@ -31,7 +31,7 @@ public static void Init()
3131
// kick off the task to show diagnostic info
3232
if (DiagnosticsInterval > 0)
3333
{
34-
_showDiagnosticsInfoTask = Task.Run(async () => await ShowDiagnosticsInfoAsync(_shutdownTokenSource.Token));
34+
_showDiagnosticsInfoTask = Task.Run(async () => await ShowDiagnosticsInfoAsync(_shutdownTokenSource.Token).ConfigureAwait(false));
3535
}
3636

3737

@@ -42,7 +42,7 @@ public async static Task ShutdownAsync()
4242
if (_showDiagnosticsInfoTask != null)
4343
{
4444
_shutdownTokenSource.Cancel();
45-
await _showDiagnosticsInfoTask;
45+
await _showDiagnosticsInfoTask.ConfigureAwait(false);
4646
}
4747

4848
_shutdownTokenSource = null;
@@ -59,10 +59,13 @@ public static DiagnosticInfoMethodResponseModel GetDiagnosticInfo()
5959
try
6060
{
6161
diagnosticInfo.PublisherStartTime = PublisherStartTime;
62-
diagnosticInfo.NumberOfOpcSessions = NumberOfOpcSessions;
63-
diagnosticInfo.NumberOfConnectedOpcSessions = NumberOfConnectedOpcSessions;
64-
diagnosticInfo.NumberOfConnectedOpcSubscriptions = NumberOfConnectedOpcSubscriptions;
65-
diagnosticInfo.NumberOfMonitoredItems = NumberOfMonitoredItems;
62+
diagnosticInfo.NumberOfOpcSessionsConfigured = NumberOfOpcSessionsConfigured;
63+
diagnosticInfo.NumberOfOpcSessionsConnected = NumberOfOpcSessionsConnected;
64+
diagnosticInfo.NumberOfOpcSubscriptionsConfigured = NumberOfOpcSubscriptionsConfigured;
65+
diagnosticInfo.NumberOfOpcSubscriptionsConnected = NumberOfOpcSubscriptionsConnected;
66+
diagnosticInfo.NumberOfOpcMonitoredItemsConfigured = NumberOfOpcMonitoredItemsConfigured;
67+
diagnosticInfo.NumberOfOpcMonitoredItemsMonitored = NumberOfOpcMonitoredItemsMonitored;
68+
diagnosticInfo.NumberOfOpcMonitoredItemsToRemove = NumberOfOpcMonitoredItemsToRemove;
6669
diagnosticInfo.MonitoredItemsQueueCapacity = MonitoredItemsQueueCapacity;
6770
diagnosticInfo.MonitoredItemsQueueCount = MonitoredItemsQueueCount;
6871
diagnosticInfo.EnqueueCount = EnqueueCount;
@@ -93,15 +96,13 @@ public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticLogAsync
9396
DiagnosticLogMethodResponseModel diagnosticLogMethodResponseModel = new DiagnosticLogMethodResponseModel();
9497
diagnosticLogMethodResponseModel.MissedMessageCount = _missedMessageCount;
9598
diagnosticLogMethodResponseModel.LogMessageCount = _logMessageCount;
96-
diagnosticLogMethodResponseModel.StartupLogMessageCount = _startupLog.Count;
9799

98-
if (StartupCompleted)
100+
if (DiagnosticsInterval >= 0)
99101
{
100-
List<string> log = new List<string>();
101-
102-
if (DiagnosticsInterval >= 0)
102+
if (StartupCompleted)
103103
{
104-
await _logQueueSemaphore.WaitAsync();
104+
List<string> log = new List<string>();
105+
await _logQueueSemaphore.WaitAsync().ConfigureAwait(false);
105106
try
106107
{
107108
string message;
@@ -116,15 +117,46 @@ public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticLogAsync
116117
_missedMessageCount = 0;
117118
_logQueueSemaphore.Release();
118119
}
120+
diagnosticLogMethodResponseModel.Log.AddRange(log);
121+
}
122+
else
123+
{
124+
diagnosticLogMethodResponseModel.Log.Add("Startup is not yet completed. Please try later.");
125+
}
126+
}
127+
else
128+
{
129+
diagnosticLogMethodResponseModel.Log.Add("Diagnostic log is disabled. Please use --di to enable it.");
130+
}
131+
132+
return diagnosticLogMethodResponseModel;
133+
}
134+
135+
/// <summary>
136+
/// Fetch diagnostic startup log data.
137+
/// </summary>
138+
public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticStartupLogAsync()
139+
{
140+
DiagnosticLogMethodResponseModel diagnosticLogMethodResponseModel = new DiagnosticLogMethodResponseModel();
141+
diagnosticLogMethodResponseModel.MissedMessageCount = 0;
142+
diagnosticLogMethodResponseModel.LogMessageCount = _startupLog.Count;
143+
144+
if (DiagnosticsInterval >= 0)
145+
{
146+
if (StartupCompleted)
147+
{
148+
diagnosticLogMethodResponseModel.Log.AddRange(_startupLog);
119149
}
120150
else
121151
{
122-
_startupLog.Add("Diagnostic log is disabled in OPC Publisher. Please use --di to enable it.");
123-
log.Add("Diagnostic log is disabled in OPC Publisher. Please use --di to enable it.");
152+
diagnosticLogMethodResponseModel.Log.Add("Startup is not yet completed. Please try later.");
124153
}
125-
diagnosticLogMethodResponseModel.StartupLog = _startupLog.ToArray();
126-
diagnosticLogMethodResponseModel.Log = log.ToArray();
127154
}
155+
else
156+
{
157+
diagnosticLogMethodResponseModel.Log.Add("Diagnostic log is disabled. Please use --di to enable it.");
158+
}
159+
128160
return diagnosticLogMethodResponseModel;
129161
}
130162

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

143175
try
144176
{
145-
await Task.Delay((int)DiagnosticsInterval * 1000, ct);
177+
await Task.Delay(DiagnosticsInterval * 1000, ct).ConfigureAwait(false);
146178

147179
DiagnosticInfoMethodResponseModel diagnosticInfo = GetDiagnosticInfo();
148180
Logger.Information("==========================================================================");
149181
Logger.Information($"OpcPublisher status @ {System.DateTime.UtcNow} (started @ {diagnosticInfo.PublisherStartTime})");
150182
Logger.Information("---------------------------------");
151-
Logger.Information($"OPC sessions: {diagnosticInfo.NumberOfOpcSessions}");
152-
Logger.Information($"connected OPC sessions: {diagnosticInfo.NumberOfConnectedOpcSessions}");
153-
Logger.Information($"connected OPC subscriptions: {diagnosticInfo.NumberOfConnectedOpcSubscriptions}");
154-
Logger.Information($"OPC monitored items: {diagnosticInfo.NumberOfMonitoredItems}");
183+
Logger.Information($"OPC sessions (configured/connected): {diagnosticInfo.NumberOfOpcSessionsConfigured}/{diagnosticInfo.NumberOfOpcSessionsConnected}");
184+
Logger.Information($"OPC subscriptions (configured/connected): {diagnosticInfo.NumberOfOpcSubscriptionsConfigured}/{diagnosticInfo.NumberOfOpcSubscriptionsConnected}");
185+
Logger.Information($"OPC monitored items (configured/monitored/to remove): {diagnosticInfo.NumberOfOpcMonitoredItemsConfigured}/{diagnosticInfo.NumberOfOpcMonitoredItemsMonitored}/{diagnosticInfo.NumberOfOpcMonitoredItemsToRemove}");
155186
Logger.Information("---------------------------------");
156187
Logger.Information($"monitored items queue bounded capacity: {diagnosticInfo.MonitoredItemsQueueCapacity}");
157188
Logger.Information($"monitored items queue current items: {diagnosticInfo.MonitoredItemsQueueCount}");
@@ -261,7 +292,7 @@ public void Emit(LogEvent logEvent)
261292

262293
private string FormatMessage(LogEvent logEvent)
263294
{
264-
return $"[{logEvent.Timestamp:T} {logEvent.Level.ToString().Substring(0, 3).ToUpper()}] {logEvent.RenderMessage()}";
295+
return $"[{logEvent.Timestamp:T} {logEvent.Level.ToString().Substring(0, 3).ToUpper(CultureInfo.InvariantCulture)}] {logEvent.RenderMessage()}";
265296
}
266297

267298
private List<string> FormatException(LogEvent logEvent)
@@ -271,7 +302,7 @@ private List<string> FormatException(LogEvent logEvent)
271302
{
272303
exceptionLog = new List<string>();
273304
exceptionLog.Add(logEvent.Exception.Message);
274-
exceptionLog.Add(logEvent.Exception.StackTrace.ToString());
305+
exceptionLog.Add(logEvent.Exception.StackTrace.ToString(CultureInfo.InvariantCulture));
275306
}
276307
return exceptionLog;
277308
}

0 commit comments

Comments
 (0)