Cannot add Microsoft.Extensions.Logging.EventLog package with .NET Core 2 application

2k Views Asked by At

so as per the MS documentation we can add EvventLog as one of the logging provider. However when i add Microsoft.Extensions.Logging.EventLog package in my .NET Core 2 Web Api application i see yellow explaination mark like below

enter image description here

With such warning i was still able to add EventLog in BuildWebHost method

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .UseUrls("http://*:40006")                
            .ConfigureLogging((hostingContext,logging)=> 
            {
                logging.AddEventLog(new Microsoft.Extensions.Logging.EventLog.EventLogSettings()
                {                    
                    SourceName = "MySource",
                });
            })
            .Build();

however when i run the application i get error

Could not load type 'System.Diagnostics.EventLog' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'

and Warning

Severity Code Description Project File Line Suppression State Warning NU1701 Package 'Microsoft.Extensions.Logging.EventLog 2.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.

2

There are 2 best solutions below

0
On

If using 2.0 you'll need to add a reference to package Microsoft.Windows.Compatibility as described in ASP.NET Core and Windows Event Log.

1
On

I've had some success with this in .NET Core 2.x:

using System;
using System.Runtime.InteropServices;

namespace Foo
{
    public enum EventLogType
    {
        EVENTLOG_SUCCESS = 0x000,
        EVENTLOG_AUDIT_FAILURE = 0x0010,
        EVENTLOG_AUDIT_SUCCESS = 0x0008,
        EVENTLOG_ERROR = 0x0001,
        EVENTLOG_INFORMATION = 0x0004,
        EVENTLOG_WARNING = 0x0002
    }

    public static class EventLog
    {
        [DllImport("advapi32.dll", EntryPoint = "RegisterEventSource", SetLastError = true)]
        public static extern long RegisterEventSource(string lpUNCServerName, string lpSourceName);

        [DllImport("advapi32.dll", EntryPoint = "DeregisterEventSource", SetLastError = true)]
        public static extern int DeregisterEventSource(IntPtr hHandle);

        [DllImport("advapi32.dll", SetLastError = true)]
        static extern int ReportEvent(IntPtr hHandle, ushort wType, ushort wCategory, uint dwEventID, IntPtr uSid, ushort wStrings, uint dwDataSize, string[] lpStrings, byte bData);

        public static void Write(string source, string message, EventLogType type)
        {
            int lastError = -1;
            IntPtr handle = IntPtr.Zero;
            string[] strs = new string[2];

            long rawHandle64 = RegisterEventSource(null, source);
            handle = new IntPtr(rawHandle64);

            lastError = Marshal.GetLastWin32Error();

            if (lastError == 0)
            {
                strs[0] = source;
                strs[1] = message;

                int res = ReportEvent(handle, (ushort)type, 0, 0, IntPtr.Zero, 2, 0, strs, 0);

                lastError = Marshal.GetLastWin32Error();

                DeregisterEventSource(handle);

                lastError = Marshal.GetLastWin32Error();
            }
        }
    }
}