master
bicijinlian 2 years ago
parent 433d6ab8ed
commit 7535e96645

@ -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
{
/// <summary>
/// 高级事件源
/// </summary>
[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);
}
/// <summary>
/// 自定义任务
/// </summary>
public class Tasks
{
public const EventTask Request = (EventTask)0x1;
}
/// <summary>
/// 自定义 关键字
/// </summary>
public class Keywords
{
public const EventKeywords Startup = (EventKeywords)0x0001;
public const EventKeywords Requests = (EventKeywords)0x0002;
public const EventKeywords AppStopd = (EventKeywords)0x0004;
}
}
}

@ -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
{
/// <summary>
/// 自定义 EventSource 示例
/// </summary>
[EventSource(Name ="EventLogNext-CustomEventSource")]
public sealed class CustomEventSource : EventSource
{
/// <summary>
/// 私有化构造,以使用单例优化性能
/// </summary>
private CustomEventSource() { }
/// <summary>
/// 静态方法,获取单实例
/// </summary>
public static CustomEventSource Logger { get; } = new CustomEventSource();
/// <summary>
/// 自定义属性
/// </summary>
[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);
}
/// <summary>
/// 应用程序结果
/// </summary>
[Event(4, Keywords = Keywords.Requests)]
public void AppStopd(string message)
{
WriteEvent(4, message);
}
/// <summary>
/// 自定义 关键字
/// </summary>
public class Keywords
{
public const EventKeywords Startup = (EventKeywords)0x0001;
public const EventKeywords Requests = (EventKeywords)0x0002;
public const EventKeywords AppStopd = (EventKeywords)0x0004;
}
}
}

@ -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);
}
}
/// <summary>
/// 处理事件
/// 其中 PayloadName 是个数组,表示事件源方法的形参数名称列表
/// Payload也是个数组为 PayloadName 形参列表对应的实参列表
/// </summary>
/// <param name="eventData"></param>
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();
}
}
}

@ -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
{
/// <summary>
/// 日志接口
/// EventSource 类型可能实现接口,以便无缝集成到使用接口定义常用日志记录目标的各种高级日志记录系统中。
/// </summary>
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);
}
}
}

@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
</Project>

@ -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, "===================");
}
}
}
}

@ -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();
}
}
}

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LogStudy.EventLog.Next
{
/// <summary>
/// 示例执行器
/// </summary>
public static class Runner
{
/// <summary>
/// 运行:最小示例
/// </summary>
public static void RunMinEventSource()
{
//Console.WriteLine($"开始运行:最小示例");
MinEventSource.Logger.Log("MinEventSource info:--------------------------------------------", 12);
//Console.WriteLine($"运行结束:最小示例");
}
/// <summary>
/// 自定义事件ID、日志等级、关键字
/// </summary>
public static void RunCustomEventSource()
{
CustomEventSource.Logger.AppStarted("CustomEventSource: AppStarted",22);
CustomEventSource.Logger.RequestStart(2);
CustomEventSource.Logger.RequestStop(2);
CustomEventSource.Logger.AppStopd(message: "CustomEventSource:AppStopd");
}
/// <summary>
/// 高级自定义
/// </summary>
public static void RunAdvancedEventSource()
{
AdvancedEventSource.Logger.RequestStart(2,"localhost");
AdvancedEventSource.Logger.RequestStop(2);
}
/// <summary>
/// 实现接口
/// </summary>
public static void RunInterfaceEventSource()
{
ILogger logger = InterfaceEventSource.Logger;
logger.Warning("InterfaceEventSource 警告信息!");
logger.Error(400, "InterfaceEventSource 错误信息!");
}
}
}

@ -8,6 +8,8 @@ namespace LogStudy.TraceLog.Next
{
Console.WriteLine("跟踪日志学习-进阶项目");
Thread.Sleep(5000);
Test();
DefaultTraceSource();

@ -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}

Loading…
Cancel
Save