You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2 KiB
C#

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<DiagnosticListener>(listener =>
{
if (listener.Name == sourceName)
{
listener.Subscribe(new Observer<KeyValuePair<string, object?>>(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<DiagnosticListener>(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",
});
}
}
}
}