简体   繁体   English

用层次结构写入事件日志

[英]Writing to the event log with hierarchy

I know there are a lot of topics about writing to the event viewer on SO, but I didn't find what I need. 我知道有很多关于在SO上编写事件查看器的主题,但是我没有找到我需要的东西。

I have the following code to create a new Event Log Category in the event Viewer: 我有以下代码在事件查看器中创建新的事件日志类别:

string sSource = "MyWebService";
string sLog = "My Application";
string sMsg = "Error Message Goes here";

if (!EventLog.SourceExists(sSource))
{ EventLog.CreateEventSource(sSource, sLog); }

This works as expected and in the Event Viewer, I have the following output: 这可以按预期工作,并且在事件查看器中,我具有以下输出:

在此处输入图片说明

So, writing to the event viewer is not a problem, but the problem occurs when I want to create some hierarchy in the logs. 因此,写入事件查看器不是问题,但是当我想在日志中创建某些层次结构时, 就会出现问题

So, let's assume that I've written an application that consists out of various components: 因此,假设我编写了一个包含各种组件的应用程序:

- MVC
- Web API
- Windows Service

Then in the Event Viewer, I want to create a dictionary that contains all those elements just as Microft does: 然后在事件查看器中,我想创建一个包含所有这些元素的字典,就像Microft一样:

在此处输入图片说明

This means I would like to have an output that looks like: 这意味着我希望输出如下所示:

- Application (directory)
    - MVC (directory)
        - Others (logs)
    - Web API (directory)
        - Demo (logs)
    - Windows Service (directory)
        - Authentication (logs)

I've tried to do the following: 我尝试执行以下操作:

string sSource = "MyWebService";
string sLog = "My Application";
string sMsg = "Error Message Goes here";

if (!EventLog.SourceExists(sSource))
{ EventLog.CreateEventSource(sSource, sLog); }

if (!EventLog.SourceExists(sLog))
{ EventLog.CreateEventSource(sLog, "Web Service"); }

But of course, this didn't work. 但是,当然,这没有用。

Anyone knows a how to create a hierarchical structure in the Event Viewer? 有人知道如何在事件查看器中创建层次结构吗?

Important to say: I like to have full control over my code, so I don't wan't to use any third-party libraries. 重要提示:我想完全控制自己的代码,所以我不想使用任何第三方库。

To do what you want you need to use Event Tracing for Windows (ETW). 若要执行所需的操作,您需要使用Windows事件跟踪(ETW)。 In .NET you can use the EventSource class but there is a more up to date NuGet package that I suggest you use: Microsoft EventSource Library . 在.NET中,您可以使用EventSource类,但我建议您使用一个更新的NuGet包: Microsoft EventSource Library There is also a NuGet package with samples which should provide a good starting point if you are new to ETW. 还有一个带有示例的NuGet软件包,如果您不熟悉ETW,则应该提供一个很好的起点。

In addition to creating code that writes to the event log you need to create a manifest and an associated resource DLL. 除了创建写入事件日志的代码之外,您还需要创建清单和关联的资源DLL。 The NuGet package has a tool that automates this process based on the log events that you have created in code. NuGet软件包具有一个工具,可根据您在代码中创建的日志事件来自动执行此过程。 You then need to register the event source using the wevtutil command line tool. 然后,您需要使用wevtutil命令行工具注册事件源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM