예제 #1
0
        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 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);
     }
 }
예제 #3
0
        public static void Run()
        {
            // This demo shows how to use the stub APIs directly, without protecting against dynamic invocation exceptions.
            // The corresponding Net Core demo shows one of several possible ways for dealing with such exceptions.
            // Other demos show other approaches for dealing with these exceptions.

            ConsoleWrite.LineLine($"STARTING DEMO '{nameof(UseDiagnosticSourceStub)}'.");

            SetupListening();

            ConsoleWrite.LineLine("Starting to create new Diagnostic Sources.");

            DiagnosticSourceStub diagnosticSource1 = DiagnosticListening.CreateNewSource("DemoXxx.UseDiagnosticSource.Name1");
            DiagnosticSourceStub diagnosticSource2 = DiagnosticListening.CreateNewSource("DemoXxx.UseDiagnosticSource.Name2");

            ConsoleWrite.Line("Finished creating new Diagnostic Sources.");

            ConsoleWrite.LineLine("Starting to emit DiagnosticSource events.");
            for (int i = 0; i < 1000; i++)
            {
                if (diagnosticSource1.IsEnabled("EventXyzName.A"))
                {
                    diagnosticSource1.Write("EventXyzName", new EventXyzNamePayload("Foo", 42, i));
                }

                if (diagnosticSource1.IsEnabled("EventXyzName.B", arg1: "Something", arg2: 13.7))
                {
                    diagnosticSource1.Write("EventXyzName", new EventXyzNamePayload("Bar", new[] { 1, 2, 3 }, i));
                }

                if (diagnosticSource1.IsEnabled("EventXyzName.C", arg1: -1))
                {
                    diagnosticSource1.Write("EventXyzName", new EventXyzNamePayload(null, null, i));
                }

                diagnosticSource2.Write("EventAbcName", new { Value = "Something", IterationNr = i });

                ConsoleWrite.LineLine($"-----------{i}-----------");
            }

            ConsoleWrite.Line("Finished to emit DiagnosticSource events.");

            ConsoleWrite.LineLine($"FINISHED DEMO '{nameof(UseDiagnosticSourceStub)}'.");
        }
예제 #4
0
        public static bool SubscribeToAllSourcesSafe(IObserver <DiagnosticListenerStub> diagnosticSourcesObserver, out IDisposable result, out Exception error)
        {
            try
            {
                error  = null;
                result = DiagnosticListening.SubscribeToAllSources(diagnosticSourcesObserver);
                return(true);
            }
            catch (Exception ex)
            {
                error = ex;
                if (s_isLogExceptionsEnabled)
                {
                    Log.Error(s_logComponentMoniker, ex);
                }

                result = null;
                return(false);
            }
        }
예제 #5
0
        public static bool CreateNewSourceSafe(string diagnosticSourceName, out DiagnosticSourceStub result, out Exception error)
        {
            try
            {
                error  = null;
                result = DiagnosticListening.CreateNewSource(diagnosticSourceName);
                return(true);
            }
            catch (Exception ex)
            {
                error = ex;
                if (s_isLogExceptionsEnabled)
                {
                    Log.Error(s_logComponentMoniker, ex);
                }

                result = DiagnosticSourceStub.NoOpStub;
                return(false);
            }
        }
예제 #6
0
        public static void Run()
        {
            ConsoleWrite.LineLine($"STARTING DEMO '{nameof(UseDiagnosticSourceStub)}'.");

            SetupListening();

            ConsoleWrite.LineLine("Starting to create new Diagnostic Sources.");

            DiagnosticSourceStub diagnosticSource1 = DiagnosticListening.CreateNewSource("DemoXxx.UseDiagnosticSource.Name1");
            DiagnosticSourceStub diagnosticSource2 = DiagnosticListening.CreateNewSource("DemoXxx.UseDiagnosticSource.Name2");

            ConsoleWrite.Line("Finished creating new Diagnostic Sources.");

            ConsoleWrite.LineLine("Starting to emit DiagnosticSource events.");
            for (int i = 0; i < 1000; i++)
            {
                if (diagnosticSource1.IsEnabled("EventXyzName.A"))
                {
                    diagnosticSource1.Write("EventXyzName", new EventXyzNamePayload("Foo", 42, i));
                }

                if (diagnosticSource1.IsEnabled("EventXyzName.B", arg1: "Something", arg2: 13.7))
                {
                    diagnosticSource1.Write("EventXyzName", new EventXyzNamePayload("Bar", new[] { 1, 2, 3 }, i));
                }

                if (diagnosticSource1.IsEnabled("EventXyzName.C", arg1: -1))
                {
                    diagnosticSource1.Write("EventXyzName", new EventXyzNamePayload(null, null, i));
                }

                diagnosticSource2.Write("EventAbcName", new { Value = "Something", IterationNr = i });

                ConsoleWrite.LineLine($"-----------{i}-----------");
            }

            ConsoleWrite.Line("Finished to emit DiagnosticSource events.");

            ConsoleWrite.LineLine($"FINISHED DEMO '{nameof(UseDiagnosticSourceStub)}'.");
        }