From 5ddca9ac12adb11db80a93557cf6f88ef2b2a3a6 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Wed, 7 Dec 2022 17:46:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=8A=E6=96=AD=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DiagnosticSourceCollector.cs | 22 ++++++ .../LogStudy.DiagnosticLog.csproj | 4 + LogStudy.DiagnosticLog/Observer.cs | 37 ++++++++++ LogStudy.DiagnosticLog/Program.cs | 73 ++++++++++++++++++- 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 LogStudy.DiagnosticLog/DiagnosticSourceCollector.cs create mode 100644 LogStudy.DiagnosticLog/Observer.cs 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", + }); + } + } + } +}