在 NLog for .NET Core 应用程序中添加 EventLog 作为目标会导致解析配置错误

Adding EventLog as target in NLog for .NET Core app causes Parsing configuration error

我有一个使用 NLog 的 .NET Core 控制台应用程序。 当我尝试在 nlog.config 中添加 EventLog 作为目标时,配置加载失败并显示以下内容:

System.ArgumentException: Target cannot be found: 'EventLog'.

我在Whosebug中发现了一个类似的问题: NLog not writing to eventlog .NET Core 2.1。建议的解决方案是添加我拥有的 Nuget 包 NLog.WindowsEventLog。而且我下载的版本是4.5.10所以应该没有其他Whosebugpost提到的v.4.5.7中的bug。而且还是不行。

这里有一些信息:

这里是 nlog.config:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: 
https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogFile="logs\internal-log.log"
      internalLogLevel="Trace" >

     <!-- the targets to write to -->
      <targets>
          <target xsi:type="EventLog" name="eventlog" source="testlogging" 
log="Application" layout="${message}" />
      </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*" minlevel="Trace" writeTo="eventlog" />
  </rules>
</nlog>

我是不是漏掉了什么?

添加到您的配置中,在 <nlog ...>

之后
<extensions>
   <add assembly="NLog.WindowsEventLog"/>
</extensions>

如果这不起作用,请检查内部日志:https://github.com/NLog/NLog/wiki/Internal-logging