NLog 不写入事件日志 .NET Core 2.1

use*_*305 1 c# logging nlog asp.net-core asp.net-core-2.1

我已将 NLog 添加到我的 .NET 核心控制台应用程序中,它适用于文件和数据库目标。但是,当我尝试让它与 eventviewer 一起使用时,它不会记录任何内容。当我为事件查看器目标添加代码时,文件和数据库部分不记录任何内容。当我删除它时,日志记录再次开始工作。

我使用 Powershell 为应用程序添加了一个新的事件查看器源,所以这不是问题。应用程序不会崩溃或报告错误,它运行良好但在包含事件查看器时不会记录任何内容。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  autoReload="true"
  internalLogFile="c:\temp\console-example-internal.log"
  internalLogLevel="Info" >

<targets>    
  <target xsi:type="File" name="logfile" fileName="c:\temp\console-example.log"
        layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-e
        vent-properties}" />

<target xsi:type="Console" name="console"
        layout="[${longdate}][${machinename}][${level:uppercase=true}] ${message} ${exception}" />

<target xsi:type="EventLog" name="eventlog" source="testlogging" log="Application"
        layout="${message}${newline}${exception:format=ToString}" />

  <target xsi:type="Database" name="database" >
      <connectionString>Server=test; Database=test; User Id=sa; Password=password;</connectionString>
      <commandText>
          INSERT INTO dbo.Log (Application, Logged, Level, Message, Logger, CallSite, Exception ) 
          VALUES (@Application, @Logged, @Level, @Message, @Logger, @Callsite, @Exception);
      </commandText>
      <parameter name="@application" layout="TestLoggingApp" />
      <parameter name="@logged" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="url: ${aspnet-request-url} | action: ${aspnet-mvc-action} | ${message}" />

      <parameter name="@logger" layout="${logger}" />
      <parameter name="@callSite" layout="${callsite:filename=true}" />
      <parameter name="@exception" layout="${exception:tostring}" />
  </target>    
</targets>

<rules>
   <logger name="*" minlevel="Trace" writeTo="logfile, console, database" />        
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

关于如何实现这一点的任何想法,或者我是否遗漏了什么?

Jim*_*eff 6

对我来说,答案是将“NLog.WindowsEventLog”程序集添加到 nlog.config 的扩展区域:

<extensions>
   <add assembly="NLog.WindowsEventLog" />
</extensions>
Run Code Online (Sandbox Code Playgroud)

我有默认值:“NLog.Web.AspNetCore”。