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.");
        }
Пример #2
0
        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);
        }
Пример #4
0
        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 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);
     }
 }