1
1
2
+ using Serilog . Core ;
2
3
using System . Diagnostics ;
3
4
using System . Threading ;
4
5
using System . Threading . Tasks ;
5
- using Serilog . Core ;
6
6
7
7
namespace OpcPublisher
8
8
{
9
9
using Serilog ;
10
10
using Serilog . Configuration ;
11
11
using Serilog . Events ;
12
- using System ;
13
12
using System . Collections . Generic ;
13
+ using System . Globalization ;
14
14
using static HubCommunication ;
15
15
using static Program ;
16
16
using static PublisherNodeConfiguration ;
@@ -31,7 +31,7 @@ public static void Init()
31
31
// kick off the task to show diagnostic info
32
32
if ( DiagnosticsInterval > 0 )
33
33
{
34
- _showDiagnosticsInfoTask = Task . Run ( async ( ) => await ShowDiagnosticsInfoAsync ( _shutdownTokenSource . Token ) ) ;
34
+ _showDiagnosticsInfoTask = Task . Run ( async ( ) => await ShowDiagnosticsInfoAsync ( _shutdownTokenSource . Token ) . ConfigureAwait ( false ) ) ;
35
35
}
36
36
37
37
@@ -42,7 +42,7 @@ public async static Task ShutdownAsync()
42
42
if ( _showDiagnosticsInfoTask != null )
43
43
{
44
44
_shutdownTokenSource . Cancel ( ) ;
45
- await _showDiagnosticsInfoTask ;
45
+ await _showDiagnosticsInfoTask . ConfigureAwait ( false ) ;
46
46
}
47
47
48
48
_shutdownTokenSource = null ;
@@ -59,10 +59,13 @@ public static DiagnosticInfoMethodResponseModel GetDiagnosticInfo()
59
59
try
60
60
{
61
61
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 ;
66
69
diagnosticInfo . MonitoredItemsQueueCapacity = MonitoredItemsQueueCapacity ;
67
70
diagnosticInfo . MonitoredItemsQueueCount = MonitoredItemsQueueCount ;
68
71
diagnosticInfo . EnqueueCount = EnqueueCount ;
@@ -93,15 +96,13 @@ public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticLogAsync
93
96
DiagnosticLogMethodResponseModel diagnosticLogMethodResponseModel = new DiagnosticLogMethodResponseModel ( ) ;
94
97
diagnosticLogMethodResponseModel . MissedMessageCount = _missedMessageCount ;
95
98
diagnosticLogMethodResponseModel . LogMessageCount = _logMessageCount ;
96
- diagnosticLogMethodResponseModel . StartupLogMessageCount = _startupLog . Count ;
97
99
98
- if ( StartupCompleted )
100
+ if ( DiagnosticsInterval >= 0 )
99
101
{
100
- List < string > log = new List < string > ( ) ;
101
-
102
- if ( DiagnosticsInterval >= 0 )
102
+ if ( StartupCompleted )
103
103
{
104
- await _logQueueSemaphore . WaitAsync ( ) ;
104
+ List < string > log = new List < string > ( ) ;
105
+ await _logQueueSemaphore . WaitAsync ( ) . ConfigureAwait ( false ) ;
105
106
try
106
107
{
107
108
string message ;
@@ -116,15 +117,46 @@ public static async Task<DiagnosticLogMethodResponseModel> GetDiagnosticLogAsync
116
117
_missedMessageCount = 0 ;
117
118
_logQueueSemaphore . Release ( ) ;
118
119
}
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 ) ;
119
149
}
120
150
else
121
151
{
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." ) ;
124
153
}
125
- diagnosticLogMethodResponseModel . StartupLog = _startupLog . ToArray ( ) ;
126
- diagnosticLogMethodResponseModel . Log = log . ToArray ( ) ;
127
154
}
155
+ else
156
+ {
157
+ diagnosticLogMethodResponseModel . Log . Add ( "Diagnostic log is disabled. Please use --di to enable it." ) ;
158
+ }
159
+
128
160
return diagnosticLogMethodResponseModel ;
129
161
}
130
162
@@ -142,16 +174,15 @@ public static async Task ShowDiagnosticsInfoAsync(CancellationToken ct)
142
174
143
175
try
144
176
{
145
- await Task . Delay ( ( int ) DiagnosticsInterval * 1000 , ct ) ;
177
+ await Task . Delay ( DiagnosticsInterval * 1000 , ct ) . ConfigureAwait ( false ) ;
146
178
147
179
DiagnosticInfoMethodResponseModel diagnosticInfo = GetDiagnosticInfo ( ) ;
148
180
Logger . Information ( "==========================================================================" ) ;
149
181
Logger . Information ( $ "OpcPublisher status @ { System . DateTime . UtcNow } (started @ { diagnosticInfo . PublisherStartTime } )") ;
150
182
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 } ") ;
155
186
Logger . Information ( "---------------------------------" ) ;
156
187
Logger . Information ( $ "monitored items queue bounded capacity: { diagnosticInfo . MonitoredItemsQueueCapacity } ") ;
157
188
Logger . Information ( $ "monitored items queue current items: { diagnosticInfo . MonitoredItemsQueueCount } ") ;
@@ -261,7 +292,7 @@ public void Emit(LogEvent logEvent)
261
292
262
293
private string FormatMessage ( LogEvent logEvent )
263
294
{
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 ( ) } ";
265
296
}
266
297
267
298
private List < string > FormatException ( LogEvent logEvent )
@@ -271,7 +302,7 @@ private List<string> FormatException(LogEvent logEvent)
271
302
{
272
303
exceptionLog = new List < string > ( ) ;
273
304
exceptionLog . Add ( logEvent . Exception . Message ) ;
274
- exceptionLog . Add ( logEvent . Exception . StackTrace . ToString ( ) ) ;
305
+ exceptionLog . Add ( logEvent . Exception . StackTrace . ToString ( CultureInfo . InvariantCulture ) ) ;
275
306
}
276
307
return exceptionLog ;
277
308
}
0 commit comments