EventSource in .NET 4.6 & Event Viewer

2019-01-26 00:02发布

I'd like to ask a very specific question about writing to the event viewer using the System.Diagnostics.Tracing.EventSource and .NET 4.6 class.

In the past, if you wanted to use the event viewer channels you needed to write/generate an XML manifest and register it with the operating system. Is this still the case?

If so I'm struggling to find out how to get the build to generate the manifest, I belive this is possible with the EventSource nuget package, but I'd like to use the in built class under the System.Diagnostics.Tracing namespace if possible.

Thanks in advance.

1条回答
走好不送
2楼-- · 2019-01-26 00:40

Take a look at the Microsoft EventRegister Tool package on NuGet:

This package includes eventRegister.exe, which enables validation and registration of user defined EventSource classes. It supports both BCL event sources (classes derived from System.Diagnostics.Tracing.EventSource) and NuGet event sources (classes derived from Microsoft.Diagnostics.Tracing.EventSource).

Install it via Package Management console in VS:

Install-Package Microsoft.Diagnostics.Tracing.EventRegister

This registers your Eventsource classes, so that you can write to Eventlog:

[EventSource(Name = "Samples-EventSourceDemos-EventLog")]
public sealed class MinimalEventSource : EventSource
{
    public static MinimalEventSource Log = new MinimalEventSource();

    [Event(1, Message="{0} -> {1}", Channel = EventChannel.Admin)]
    public void Load(long baseAddress, string imageName)
    {
        WriteEvent(1, baseAddress, imageName);
    }
}

enter image description here

查看更多
登录 后发表回答