using System.Diagnostics; namespace LogStudy.DiagnosticLog { public class Program { static void Main(string[] args) { Console.WriteLine("======== 诊断日志学习 ========"); DemoA(); DemoB(); } static void DemoA() { var sourceName = "LogStudy-DiagnosticLog-DiagnosticSource"; DiagnosticListener.AllListeners.Subscribe(new Observer(listener => { if (listener.Name == sourceName) { listener.Subscribe(new Observer>(eventData => { Console.WriteLine($"Event Name: {eventData.Key}"); if (eventData.Value != null) { dynamic payload = eventData.Value; Console.WriteLine($"CommandType:{payload.CommandType}"); Console.WriteLine($"CommandText:{payload.CommandText}"); } })); } })); var diagnosticSource = new DiagnosticListener(sourceName); if (diagnosticSource.IsEnabled()) { diagnosticSource.Write("CommandExecution", new { CommandType = "SQL Server", CommandText = "select * from tableA", }); } } static void DemoB() { var sourceName = "LogStudy-DiagnosticLog-DiagnosticSource2"; DiagnosticListener.AllListeners.Subscribe(new Observer(listener => { if (listener.Name == sourceName) { listener.SubscribeWithAdapter(new DiagnosticSourceCollector()); } })); var diagnosticSource = new DiagnosticListener(sourceName); if (diagnosticSource.IsEnabled()) { diagnosticSource.Write("CommandExcute", new { CommandType = "SQL Server", CommandText = "update tableA set id=2", }); } } } }