Write to EventLog in .Net Core

2020-03-12 06:42发布

问题:

I need a way to write to Windows' event viewer in my app that's using dnx. But, the EventLog class isn't available in the System.Diagnostics namespace so I'm stuck. Is there any other way to write to the EventViewer?

回答1:

Add from NuGet Microsoft.Extensions.Logging.EventLog Version 2.1.1

CM> Install-Package Microsoft.Extensions.Logging.EventLog -Version 2.1.1

Include the namespace Microsoft.Extensions.Logging.EventLog in the Program.cs file

This resolved my problem.



回答2:

Good news! EventLog is already ported to corefx and will be available in .NET Core 2.1.

Right now you can download a preview package System.Diagnostics.EventLog from their MyGet feed.



回答3:

it is available in 2.0 but you have to install the compatibility pack. https://github.com/dotnet/corefx/issues/25440

Here for more information: https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/