diff --git a/LogStudy.DiagnosticLog/DiagnosticSourceCollector.cs b/LogStudy.DiagnosticLog/DiagnosticSourceCollector.cs new file mode 100644 index 0000000..24840c3 --- /dev/null +++ b/LogStudy.DiagnosticLog/DiagnosticSourceCollector.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.DiagnosticAdapter; + +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LogStudy.DiagnosticLog +{ + public class DiagnosticSourceCollector + { + [DiagnosticName("CommandExcute")] + public void OnCommandExcute(string commandType, string commandText) + { + Console.WriteLine($"Event Name: OnCommandExcute"); + Console.WriteLine($"CommandType:{commandType}"); + Console.WriteLine($"CommandText:{commandText}"); + } + } +} diff --git a/LogStudy.DiagnosticLog/LogStudy.DiagnosticLog.csproj b/LogStudy.DiagnosticLog/LogStudy.DiagnosticLog.csproj index 74abf5c..8785e51 100644 --- a/LogStudy.DiagnosticLog/LogStudy.DiagnosticLog.csproj +++ b/LogStudy.DiagnosticLog/LogStudy.DiagnosticLog.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/LogStudy.DiagnosticLog/Observer.cs b/LogStudy.DiagnosticLog/Observer.cs new file mode 100644 index 0000000..7323c56 --- /dev/null +++ b/LogStudy.DiagnosticLog/Observer.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LogStudy.DiagnosticLog +{ + /// + /// 观察者 + /// + /// + public class Observer : IObserver + { + private readonly Action _onNext; + + public Observer(Action onNext) + { + _onNext = onNext; + } + + public void OnCompleted() + { + Console.WriteLine("完成"); + } + + public void OnError(Exception error) + { + Console.WriteLine($"出现异常:{error.Message}"); + } + + public void OnNext(T value) + { + _onNext(value); + } + } +} diff --git a/LogStudy.DiagnosticLog/Program.cs b/LogStudy.DiagnosticLog/Program.cs index 1bc52a6..bf8d322 100644 --- a/LogStudy.DiagnosticLog/Program.cs +++ b/LogStudy.DiagnosticLog/Program.cs @@ -1 +1,72 @@ -Console.WriteLine("Hello, World!"); +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", + }); + } + } + } +}