|
| 1 | + |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace OpcPublisher |
| 7 | +{ |
| 8 | + using static IotHubMessaging; |
| 9 | + using static OpcPublisher.Workarounds.TraceWorkaround; |
| 10 | + using static Program; |
| 11 | + using static PublisherNodeConfiguration; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Class to enable output to the console. |
| 15 | + /// </summary> |
| 16 | + public static class Diagnostics |
| 17 | + { |
| 18 | + public static uint DiagnosticsInterval |
| 19 | + { |
| 20 | + get => _diagnosticsInterval; |
| 21 | + set => _diagnosticsInterval = value; |
| 22 | + } |
| 23 | + |
| 24 | + public static int IotHubMessagingMessagesSentCount => _messagesSentCount; |
| 25 | + public static SemaphoreSlim DiagnosticsSemaphore; |
| 26 | + |
| 27 | + public static void Init() |
| 28 | + { |
| 29 | + // init data |
| 30 | + _showDiagnosticsInfoTask = null; |
| 31 | + _shutdownTokenSource = new CancellationTokenSource(); |
| 32 | + DiagnosticsSemaphore = new SemaphoreSlim(1); |
| 33 | + |
| 34 | + // kick off the task to show diagnostic info |
| 35 | + if (_diagnosticsInterval > 0) |
| 36 | + { |
| 37 | + _showDiagnosticsInfoTask = Task.Run(async () => await ShowDiagnosticsInfoAsync(_shutdownTokenSource.Token)); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + } |
| 42 | + public async static Task Shutdown() |
| 43 | + { |
| 44 | + // wait for diagnostic task completion if it is enabled |
| 45 | + if (_showDiagnosticsInfoTask != null) |
| 46 | + { |
| 47 | + _shutdownTokenSource.Cancel(); |
| 48 | + await _showDiagnosticsInfoTask; |
| 49 | + } |
| 50 | + |
| 51 | + DiagnosticsSemaphore.Dispose(); |
| 52 | + DiagnosticsSemaphore = null; |
| 53 | + _shutdownTokenSource = null; |
| 54 | + _showDiagnosticsInfoTask = null; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Kicks of the task to show diagnostic information each 30 seconds. |
| 59 | + /// </summary> |
| 60 | + public static async Task ShowDiagnosticsInfoAsync(CancellationToken ct) |
| 61 | + { |
| 62 | + while (true) |
| 63 | + { |
| 64 | + try |
| 65 | + { |
| 66 | + |
| 67 | + await Task.Delay((int)_diagnosticsInterval * 1000, ct); |
| 68 | + if (ct.IsCancellationRequested) |
| 69 | + { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + Trace("=========================================================================="); |
| 74 | + Trace($"OpcPublisher status @ {System.DateTime.UtcNow} (started @ {PublisherStartTime})"); |
| 75 | + Trace("---------------------------------"); |
| 76 | + Trace($"OPC sessions: {NumberOfOpcSessions}"); |
| 77 | + Trace($"connected OPC sessions: {NumberOfConnectedOpcSessions}"); |
| 78 | + Trace($"connected OPC subscriptions: {NumberOfConnectedOpcSubscriptions}"); |
| 79 | + Trace($"OPC monitored items: {NumberOfMonitoredItems}"); |
| 80 | + Trace("---------------------------------"); |
| 81 | + Trace($"monitored items queue bounded capacity: {MonitoredItemsQueueCapacity}"); |
| 82 | + Trace($"monitored items queue current items: {MonitoredItemsQueueCount}"); |
| 83 | + Trace($"monitored item notifications enqueued: {EnqueueCount}"); |
| 84 | + Trace($"monitored item notifications enqueue failure: {EnqueueFailureCount}"); |
| 85 | + Trace($"monitored item notifications dequeued: {DequeueCount}"); |
| 86 | + Trace("---------------------------------"); |
| 87 | + Trace($"messages sent to IoTHub: {SentMessages}"); |
| 88 | + Trace($"last successful msg sent @: {SentLastTime}"); |
| 89 | + Trace($"bytes sent to IoTHub: {SentBytes}"); |
| 90 | + Trace($"avg msg size: {SentBytes / (SentMessages == 0 ? 1 : SentMessages)}"); |
| 91 | + Trace($"msg send failures: {FailedMessages}"); |
| 92 | + Trace($"messages too large to sent to IoTHub: {TooLargeCount}"); |
| 93 | + Trace($"times we missed send interval: {MissedSendIntervalCount}"); |
| 94 | + Trace("---------------------------------"); |
| 95 | + Trace($"current working set in MB: {Process.GetCurrentProcess().WorkingSet64 / (1024 * 1024)}"); |
| 96 | + Trace($"--si setting: {DefaultSendIntervalSeconds}"); |
| 97 | + Trace($"--ms setting: {IotHubMessageSize}"); |
| 98 | + Trace($"--ih setting: {IotHubProtocol}"); |
| 99 | + Trace("=========================================================================="); |
| 100 | + } |
| 101 | + catch |
| 102 | + { |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static uint _diagnosticsInterval = 0; |
| 108 | + private static int _messagesSentCount = 0; |
| 109 | + private static CancellationTokenSource _shutdownTokenSource; |
| 110 | + private static Task _showDiagnosticsInfoTask; |
| 111 | + } |
| 112 | +} |
0 commit comments