private void OnEventObservered(KeyValuePair <string, object> eventInfo) { if (eventInfo.Key != null && eventInfo.Value != null && eventInfo.Value is DiagnosticEventsSpecification.EventPayload eventPayload) { if (eventInfo.Key.Equals(DiagnosticEventsSpecification.DirectSourceEventName, StringComparison.Ordinal) && eventPayload.SourceName != null && eventPayload.SourceName.Equals(DiagnosticEventsSpecification.DirectSourceName, StringComparison.Ordinal)) { _directSourceResultAccumulator.SetReceived(eventPayload.Iteration); return; } if (eventInfo.Key.Equals(DiagnosticEventsSpecification.StubbedSourceEventName, StringComparison.Ordinal) && eventPayload.SourceName != null && eventPayload.SourceName.Equals(DiagnosticEventsSpecification.StubbedSourceName, StringComparison.Ordinal)) { _stubbedSourceResultAccumulator.SetReceived(eventPayload.Iteration); return; } } ConsoleWrite.Line(); ConsoleWrite.Line($"Unexpected event info:"); ConsoleWrite.Line($" Name: \"{eventInfo.Key ?? "<null>"}\""); ConsoleWrite.Line($" Payload type: \"{eventInfo.Value?.GetType()?.FullName ?? "<null>"}\""); ConsoleWrite.Line($" Payload value: \"{eventInfo.Value?.ToString() ?? "<null>"}\""); }
private void OnDynamicDiagnosticSourceInvokerInvalidated(DiagnosticSourceAssembly.IDynamicInvoker dynamicInvoker, object state) { // This listener method is just here for demo purposes. It does not perform any business logic (but it could). Guid sessionId = (state is Guid stateGuid) ? stateGuid : Guid.Empty; ConsoleWrite.LineLine($"This {this.GetType().Name} noticed that a dynamic DiagnosticSource invoker was invalidated (session {sessionId})." + $" Some errors may be temporarily observed until stubs are re-initialized."); }
private static void Dispose(DiagnosticSourceStub diagnosticSource) { try { diagnosticSource.Dispose(); } catch (Exception ex) { // If there was some business logic required to handle such errors, it would go here. ConsoleWrite.Exception(ex); } }
private string GetName(DiagnosticListenerStub diagnosticListener) { try { return(diagnosticListener.Name); } catch (Exception ex) { // If there was some business logic required to handle such errors, it would go here. ConsoleWrite.Exception(ex); return(null); } }
public async Task Run() { ConsoleWrite.Line(); ConsoleWrite.Line($"Starting {this.GetType().Name}.{nameof(Run)}."); string srcName = DiagnosticEventsSpecification.DirectSourceName; DiagnosticSource diagnosticSource = new DiagnosticListener(srcName); Random rnd = new Random(); int currIteration = CurrentIteration; while (currIteration < _maxInterations) { if (diagnosticSource.IsEnabled(DiagnosticEventsSpecification.DirectSourceEventName)) { diagnosticSource.Write(DiagnosticEventsSpecification.DirectSourceEventName, new DiagnosticEventsSpecification.EventPayload(currIteration, srcName)); } if (currIteration == _phaseOneIterations) { PhaseOneCompletedEvent.Set(); } int sleepMillis = rnd.Next(MaxSleepMillis); if (sleepMillis == 0) { ; } if (sleepMillis == 1) { Thread.Yield(); } else { await Task.Delay(sleepMillis); } currIteration = Interlocked.Increment(ref _currentIteration); } ConsoleWrite.Line(); ConsoleWrite.Line($"Finishing {this.GetType().Name}.{nameof(Run)}."); if (diagnosticSource is IDisposable disposableSource) { disposableSource.Dispose(); } ConsoleWrite.Line($"Finished {this.GetType().Name}.{nameof(Run)}."); }
public async Task Run() { ConsoleWrite.LineLine($"Starting {this.GetType().Name}.{nameof(Run)}."); Random rnd = new Random(); DiagnosticSourceAssembly.SubscribeDynamicInvokerInitializedListener(OnDynamicDiagnosticSourceInvokerInitialized, rnd); { ConsoleWrite.LineLine($"Kicking off the DS magic."); bool prevInit = DiagnosticSourceAssembly.IsInitialized; bool nowInit = DiagnosticSourceAssembly.EnsureInitialized(); ConsoleWrite.Line($"DiagnosticSourceAssembly-magic status: prevInit={prevInit}, nowInit={nowInit}."); } int currIteration = CurrentIteration; while (currIteration < _maxInterations) { WriteIfEnabled(_diagnosticSource, currIteration); if (currIteration == _phaseOneIterations) { PhaseOneCompletedEvent.Set(); } int sleepMillis = rnd.Next(MaxSleepMillis); if (sleepMillis == 0) { ; } else if (sleepMillis == 1) { Thread.Yield(); } else { await Task.Delay(sleepMillis - 2); } currIteration = Interlocked.Increment(ref _currentIteration); } ConsoleWrite.Line(); ConsoleWrite.Line($"Finishing {this.GetType().Name}.{nameof(Run)}."); Dispose(_diagnosticSource); ConsoleWrite.Line($"Finished {this.GetType().Name}.{nameof(Run)}."); }
private void OnDynamicDiagnosticSourceInvokerInitialized(DiagnosticSourceAssembly.IDynamicInvoker dynamicInvoker) { ConsoleWrite.LineLine($"This {this.GetType().Name} noticed that an" + $" {nameof(DiagnosticSourceAssembly)}.{nameof(DiagnosticSourceAssembly.IDynamicInvoker)} became available." + $" DiagnosticSourceAssemblyName: \"{dynamicInvoker.DiagnosticSourceAssemblyName}\"."); Guid sessionId = Guid.NewGuid(); ConsoleWrite.LineLine($"Settng up {this.GetType().Name} listening session \"{sessionId}\"."); SubscribeToAllSources(); ConsoleWrite.LineLine($"Finished settng up {this.GetType().Name} listening session \"{sessionId}\"."); dynamicInvoker.SubscribeInvalidatedListener(OnDynamicDiagnosticSourceInvokerInvalidated, sessionId); }
private static void CreateNewSource(ref DiagnosticSourceStub diagnosticSource) { try { DiagnosticSourceStub newDiagSrc = DiagnosticListening.CreateNewSource(DiagnosticEventsSpecification.StubbedSourceName); diagnosticSource = newDiagSrc; } catch (Exception ex) { diagnosticSource = DiagnosticSourceStub.NoOpStub; // If there was some business logic required to handle such errors, it would go here. ConsoleWrite.Exception(ex); } }
private void OnDynamicDiagnosticSourceInvokerInitialized(DiagnosticSourceAssembly.IDynamicInvoker dynamicInvoker, object state) { Random rnd = (Random)state; ConsoleWrite.LineLine($"This {this.GetType().Name} noticed that an" + $" {nameof(DiagnosticSourceAssembly)}.{nameof(DiagnosticSourceAssembly.IDynamicInvoker)} became available." + $" DiagnosticSourceAssemblyName: \"{dynamicInvoker.DiagnosticSourceAssemblyName}\"."); int sessionId = rnd.Next(1000); ConsoleWrite.LineLine($"Initializing new Diagnostic Source generator session \"{sessionId.ToString("000")}\"."); CreateNewSource(ref _diagnosticSource); ConsoleWrite.LineLine($"Finsihed initializing new Diagnostic Source generator session \"{sessionId.ToString("000")}\"."); dynamicInvoker.SubscribeInvalidatedListener(OnDynamicDiagnosticSourceInvokerInvalidated, sessionId); }
private static void WriteIfEnabled(DiagnosticSourceStub diagnosticSource, int currentIteration) { try { if (!diagnosticSource.IsNoOpStub && diagnosticSource.IsEnabled(DiagnosticEventsSpecification.StubbedSourceEventName)) { diagnosticSource.Write(DiagnosticEventsSpecification.StubbedSourceEventName, new DiagnosticEventsSpecification.EventPayload(currentIteration, DiagnosticEventsSpecification.StubbedSourceName)); } } catch (Exception ex) { // If there was some business logic required to handle such errors, it would go here. ConsoleWrite.Exception(ex); } }
private IDisposable SubscribeToAllSources() { try { return(DiagnosticListening.SubscribeToAllSources(ObserverAdapter.OnAllHandlers( (DiagnosticListenerStub dl) => OnEventSourceObservered(dl), (Exception err) => ConsoleWrite.Exception(err), // Just for demo. Error handler is not actually necessary. () => ConsoleWrite.LineLine($"All-EventSources-Subscription Completed.")))); // Just for demo. Completion handler is not actually necessary. } catch (Exception ex) { // If there was some business logic required to handle such errors, it would go here. ConsoleWrite.Exception(ex); return(null); } }
private IDisposable SubscribeToEvents(DiagnosticListenerStub diagnosticListener, string eventNamePrefix, Action <KeyValuePair <string, object> > eventHandler) { try { if (eventNamePrefix == null) { return(diagnosticListener.SubscribeToEvents(ObserverAdapter.OnNextHandler(eventHandler), null)); } else { return(diagnosticListener.SubscribeToEvents( ObserverAdapter.OnNextHandler(eventHandler), (string eventName, object _, object __) => (eventName != null) && eventName.StartsWith(eventNamePrefix, StringComparison.Ordinal))); } } catch (Exception ex) { // If there was some business logic required to handle such errors, it would go here. ConsoleWrite.Exception(ex); return(null); } }