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); } }
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)}'."); }
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); } }
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)}'."); }