无法使用.NET Core 2应用程序添加Microsoft.Extensions.Logging.EventLog包

时间:2018-03-05 23:37:02

标签: asp.net-core .net-core asp.net-core-2.0 coreclr .net-core-logging

因此,根据MS documentation,我们可以将EvventLog添加为日志记录提供程序之一。但是当我在我的.NET Core 2 Web Api应用程序中添加Microsoft.Extensions.Logging.EventLog包时,我看到如下的黄色解释标记

enter image description here

有了这样的警告,我仍然能够在BuildWebHost方法中添加EventLog

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

然而,当我运行应用程序时,我收到错误

  

无法加载类型&#39; System.Diagnostics.EventLog&#39;从装配   &#39;系统,版本= 4.0.0.0,文化=中立,   公钥= b77a5c561934e089&#39;&#39;

和警告

  

严重级代码描述项目文件行抑制状态   警告NU1701软件包&#39; Microsoft.Extensions.Logging.EventLog 2.0.0&#39;   使用&#39; .NETFramework,Version = v4.6.1&#39;进行了恢复。而不是   项目目标框架&#39; .NETCoreApp,Version = v2.0&#39;。这个包可能   与您的项目不完全兼容。

2 个答案:

答案 0 :(得分:0)

我在.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();
            }
        }
    }
}

答案 1 :(得分:0)

如果使用2.0,则需要按照ASP.NET Core and Windows Event Log中的说明添加对软件包Microsoft.Windows.Compatibility的引用。