diff --git a/LogStudy.EventLog.Next/AdvancedDemo.cs b/LogStudy.EventLog.Next/AdvancedDemo.cs new file mode 100644 index 0000000..f654b22 --- /dev/null +++ b/LogStudy.EventLog.Next/AdvancedDemo.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Tracing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LogStudy.EventLog.Next +{ + /// + /// 高级事件源 + /// + [EventSource(Name = "EventLogNext-AdvancedEventSource")] + public sealed class AdvancedEventSource : EventSource + { + private AdvancedEventSource() { } + + public static AdvancedEventSource Logger { get; } = new AdvancedEventSource(); + + [Event(1, Task = Tasks.Request, Opcode = EventOpcode.Start)] + public void RequestStart(int RequestID, string Url) + { + WriteEvent(1, RequestID, Url); + } + + [Event(2, Task = Tasks.Request, Opcode = EventOpcode.Info)] + public void RequestPhase(int RequestID, string PhaseName) + { + WriteEvent(2, RequestID, PhaseName); + } + + [Event(3, Keywords = Keywords.Requests, Task = Tasks.Request, Opcode = EventOpcode.Stop)] + public void RequestStop(int RequestID) + { + WriteEvent(eventId: 3, RequestID); + } + + /// + /// 自定义任务 + /// + public class Tasks + { + public const EventTask Request = (EventTask)0x1; + } + + /// + /// 自定义 关键字 + /// + public class Keywords + { + public const EventKeywords Startup = (EventKeywords)0x0001; + public const EventKeywords Requests = (EventKeywords)0x0002; + public const EventKeywords AppStopd = (EventKeywords)0x0004; + } + } +} diff --git a/LogStudy.EventLog.Next/CustomDemo.cs b/LogStudy.EventLog.Next/CustomDemo.cs new file mode 100644 index 0000000..92399c8 --- /dev/null +++ b/LogStudy.EventLog.Next/CustomDemo.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Tracing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LogStudy.EventLog.Next +{ + /// + /// 自定义 EventSource 示例 + /// + [EventSource(Name ="EventLogNext-CustomEventSource")] + public sealed class CustomEventSource : EventSource + { + /// + /// 私有化构造,以使用单例优化性能 + /// + private CustomEventSource() { } + + /// + /// 静态方法,获取单实例 + /// + public static CustomEventSource Logger { get; } = new CustomEventSource(); + + /// + /// 自定义属性 + /// + [Event(1, Level = EventLevel.Informational, Keywords = Keywords.Startup)] + public void AppStarted(string message, int ss) + { + WriteEvent(1, message,ss); + } + + [Event(2, Keywords = Keywords.Requests)] + public void RequestStart(int requestId) + { + WriteEvent(2, requestId); + } + + [Event(3, Keywords = Keywords.Requests)] + public void RequestStop(int requestId) + { + WriteEvent(3, requestId); + } + + /// + /// 应用程序结果 + /// + [Event(4, Keywords = Keywords.Requests)] + public void AppStopd(string message) + { + WriteEvent(4, message); + } + + /// + /// 自定义 关键字 + /// + public class Keywords + { + public const EventKeywords Startup = (EventKeywords)0x0001; + public const EventKeywords Requests = (EventKeywords)0x0002; + public const EventKeywords AppStopd = (EventKeywords)0x0004; + } + } +} diff --git a/LogStudy.EventLog.Next/CustomEventListener.cs b/LogStudy.EventLog.Next/CustomEventListener.cs new file mode 100644 index 0000000..9c947b5 --- /dev/null +++ b/LogStudy.EventLog.Next/CustomEventListener.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Tracing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Newtonsoft.Json; + +namespace LogStudy.EventLog.Next +{ + public class CustomEventListener : EventListener + { + protected override void OnEventSourceCreated(EventSource eventSource) + { + if (eventSource.Name.StartsWith("EventLogNext-")) + { + EnableEvents(eventSource, EventLevel.Verbose); + } + } + + /// + /// 处理事件 + /// 其中 PayloadName 是个数组,表示事件源方法的形参数名称列表 + /// Payload也是个数组,为 PayloadName 形参列表对应的实参列表 + /// + /// + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + var content = ToText(eventData); + + Console.WriteLine("---------事件日志开始-----------------------------------------------------------------------"); + Console.WriteLine(content); + Console.WriteLine("=========事件日志结束======================================================================="); + Console.WriteLine(); + } + + private string ToJson(EventWrittenEventArgs eventData) + { + return Newtonsoft.Json.JsonConvert.SerializeObject(eventData, Formatting.Indented); + } + + private string ToText(EventWrittenEventArgs eventData) + { + StringBuilder stringBuilder= new StringBuilder(1000); + + stringBuilder.AppendLine($"[TimeStamp = { eventData.TimeStamp.ToLongDateString }]"); + stringBuilder.AppendLine($"[OSThreadId = { eventData.OSThreadId }]"); + stringBuilder.AppendLine($"[ActivityId = { eventData.ActivityId }]" ); + stringBuilder.AppendLine($"[RelatedActivityId = { eventData.RelatedActivityId }],"); + stringBuilder.AppendLine($"[Channel = { eventData.Channel }]"); + stringBuilder.AppendLine($"[Version = { eventData.Version }]"); + stringBuilder.AppendLine($"[Opcode = { eventData.Opcode }]"); + stringBuilder.AppendLine($"[EventSource = { eventData.EventSource }]"); + stringBuilder.AppendLine($"[EventId = { eventData.EventId }]"); + stringBuilder.AppendLine($"[EventName = { eventData.EventName }]"); + stringBuilder.AppendLine($"[Keywords = { eventData.Keywords }]"); + stringBuilder.AppendLine($"[Level = { eventData.Level }]"); + stringBuilder.AppendLine($"[Message = { eventData.Message }]"); + stringBuilder.AppendLine($"[PayloadNames = { eventData.PayloadNames }]"); + stringBuilder.AppendLine($"[Payload = { eventData.Payload }]"); + stringBuilder.AppendLine($"[Message = { eventData.Message }]"); + stringBuilder.AppendLine($"[Tags = { eventData.Tags },"); + stringBuilder.AppendLine($"[Task = { eventData.Task },"); + + return stringBuilder.ToString(); + } + } +} + diff --git a/LogStudy.EventLog.Next/InterfaceDemo.cs b/LogStudy.EventLog.Next/InterfaceDemo.cs new file mode 100644 index 0000000..974b80c --- /dev/null +++ b/LogStudy.EventLog.Next/InterfaceDemo.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Tracing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LogStudy.EventLog.Next +{ + /// + /// 日志接口 + /// EventSource 类型可能实现接口,以便无缝集成到使用接口定义常用日志记录目标的各种高级日志记录系统中。 + /// + public interface ILogger + { + void Error(int errorCode, string msg); + void Warning(string msg); + } + + [EventSource(Name = "EventLogNext-InterfaceEventSource")] + public class InterfaceEventSource : EventSource, ILogger + { + public InterfaceEventSource() { } + + public static InterfaceEventSource Logger { get; set; } = new InterfaceEventSource(); + + [Event(1)] + public void Error(int errorCode, string msg) + { + WriteEvent(1, errorCode, msg); + } + + [Event(2)] + public void Warning(string msg) + { + WriteEvent(2, msg); + } + } +} diff --git a/LogStudy.EventLog.Next/LogStudy.EventLog.Next.csproj b/LogStudy.EventLog.Next/LogStudy.EventLog.Next.csproj index 74abf5c..e5514ff 100644 --- a/LogStudy.EventLog.Next/LogStudy.EventLog.Next.csproj +++ b/LogStudy.EventLog.Next/LogStudy.EventLog.Next.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/LogStudy.EventLog.Next/MinDemo.cs b/LogStudy.EventLog.Next/MinDemo.cs new file mode 100644 index 0000000..a686e25 --- /dev/null +++ b/LogStudy.EventLog.Next/MinDemo.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Tracing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LogStudy.EventLog.Next +{ + [EventSource(Name = "EventLogNext-MinEventSource")] + public class MinEventSource : EventSource + { + private MinEventSource() + { + } + + public static MinEventSource Logger { get; } = new MinEventSource(); + + [Event(1)] + public void Log(string message, int favoriteNumber) + { + if (IsEnabled()) + { + Write(Name, "==================="); + } + } + } +} diff --git a/LogStudy.EventLog.Next/Program.cs b/LogStudy.EventLog.Next/Program.cs index 2e37069..7193c65 100644 --- a/LogStudy.EventLog.Next/Program.cs +++ b/LogStudy.EventLog.Next/Program.cs @@ -8,36 +8,16 @@ namespace LogStudy.EventLog.Next { Console.WriteLine("======== 高级事件日志学习 ========"); - //UseTest(); - UseDefaultSource(); + Thread.Sleep(10000); + CustomEventListener customEventListener = new CustomEventListener(); - Console.WriteLine("按回车键,退出!"); - //Console.ReadLine(); - } - - static void UseTest() - { - EventSource defaultEventSource = new EventSource("Demo"); - - defaultEventSource.Write("xxxxx","xxxxxxxxxxxxxxxxxxxxxxxx"); - } - - static void UseDefaultSource() - { - EventSourceSettings settings = EventSourceSettings.EtwSelfDescribingEventFormat | EventSourceSettings.ThrowOnEventWriteErrors; - EventSource defaultEventSource = new EventSource("Andy-DemoTest", settings, new string[] { "First", "Second" }); - - EventSourceOptions options = new EventSourceOptions() - { - Keywords= EventKeywords.All, - Level = EventLevel.Verbose, - Opcode = EventOpcode.Info, - Tags = EventTags.None, - ActivityOptions = EventActivityOptions.Recursive, - }; - - defaultEventSource.Write("xxxxx", options, "000000000000000000000000000000000000000000000000"); + Runner.RunMinEventSource(); + Runner.RunCustomEventSource(); + Runner.RunAdvancedEventSource(); + Runner.RunInterfaceEventSource(); + //Console.WriteLine("按回车键,退出!"); + Console.ReadLine(); } } } \ No newline at end of file diff --git a/LogStudy.EventLog.Next/Runner.cs b/LogStudy.EventLog.Next/Runner.cs new file mode 100644 index 0000000..f7b5ae7 --- /dev/null +++ b/LogStudy.EventLog.Next/Runner.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LogStudy.EventLog.Next +{ + /// + /// 示例执行器 + /// + public static class Runner + { + /// + /// 运行:最小示例 + /// + public static void RunMinEventSource() + { + //Console.WriteLine($"开始运行:最小示例"); + + MinEventSource.Logger.Log("MinEventSource info:--------------------------------------------", 12); + + //Console.WriteLine($"运行结束:最小示例"); + } + + /// + /// 自定义:事件ID、日志等级、关键字 + /// + public static void RunCustomEventSource() + { + CustomEventSource.Logger.AppStarted("CustomEventSource: AppStarted",22); + CustomEventSource.Logger.RequestStart(2); + CustomEventSource.Logger.RequestStop(2); + CustomEventSource.Logger.AppStopd(message: "CustomEventSource:AppStopd"); + } + + /// + /// 高级自定义 + /// + public static void RunAdvancedEventSource() + { + AdvancedEventSource.Logger.RequestStart(2,"localhost"); + AdvancedEventSource.Logger.RequestStop(2); + } + + /// + /// 实现接口 + /// + public static void RunInterfaceEventSource() + { + ILogger logger = InterfaceEventSource.Logger; + + logger.Warning("InterfaceEventSource 警告信息!"); + logger.Error(400, "InterfaceEventSource 错误信息!"); + } + } +} diff --git a/LogStudy.TraceLog.Next/Program.cs b/LogStudy.TraceLog.Next/Program.cs index 44f210f..c395023 100644 --- a/LogStudy.TraceLog.Next/Program.cs +++ b/LogStudy.TraceLog.Next/Program.cs @@ -8,6 +8,8 @@ namespace LogStudy.TraceLog.Next { Console.WriteLine("跟踪日志学习-进阶项目"); + Thread.Sleep(5000); + Test(); DefaultTraceSource(); diff --git a/LogStudy.sln b/LogStudy.sln index 2428608..101113e 100644 --- a/LogStudy.sln +++ b/LogStudy.sln @@ -28,7 +28,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogStudy.EventLog.Next", "L EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogStudy.TraceLog.Next", "LogStudy.TraceLog.Next\LogStudy.TraceLog.Next.csproj", "{5BC961D8-3DC8-4A18-A787-AA475865C801}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogStudy.DiagnosticLog.Next", "LogStudy.DiagnosticLog.Next\LogStudy.DiagnosticLog.Next.csproj", "{0E6F6193-3075-4ABA-A4E5-07718BA8F69C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogStudy.DiagnosticLog.Next", "LogStudy.DiagnosticLog.Next\LogStudy.DiagnosticLog.Next.csproj", "{0E6F6193-3075-4ABA-A4E5-07718BA8F69C}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EventPipeStudy", "EventPipeStudy", "{E78268EC-99A3-4EAC-9527-1898998B83E7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LogStudy.EventLog.EventPipeClient", "LogStudy.EventLog.EventPipeClient\LogStudy.EventLog.EventPipeClient.csproj", "{4C8E4EFD-FC57-47FC-A9F6-30C5283ACB4E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -68,6 +72,10 @@ Global {0E6F6193-3075-4ABA-A4E5-07718BA8F69C}.Debug|Any CPU.Build.0 = Debug|Any CPU {0E6F6193-3075-4ABA-A4E5-07718BA8F69C}.Release|Any CPU.ActiveCfg = Release|Any CPU {0E6F6193-3075-4ABA-A4E5-07718BA8F69C}.Release|Any CPU.Build.0 = Release|Any CPU + {4C8E4EFD-FC57-47FC-A9F6-30C5283ACB4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C8E4EFD-FC57-47FC-A9F6-30C5283ACB4E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C8E4EFD-FC57-47FC-A9F6-30C5283ACB4E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C8E4EFD-FC57-47FC-A9F6-30C5283ACB4E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -80,6 +88,7 @@ Global {D77E4B1E-D839-446B-AB96-46CC5E07D36F} = {78E9B0D7-537A-4791-BE80-E2BA04D8A5AA} {5BC961D8-3DC8-4A18-A787-AA475865C801} = {353B9475-1064-473D-B3EE-CF3A9BBA4102} {0E6F6193-3075-4ABA-A4E5-07718BA8F69C} = {D91AB187-F922-47DE-87ED-D3785F4E8D7B} + {4C8E4EFD-FC57-47FC-A9F6-30C5283ACB4E} = {78E9B0D7-537A-4791-BE80-E2BA04D8A5AA} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4D9A3A36-98D0-498C-B78A-2E5CFC5195AC}