NLog Exception when parsing nlog.config - Target cannot be found: 'EventLog'

故事扮演 提交于 2019-12-04 17:00:26

About the Target cannot be found: 'EventLog' error

The "Target cannot be found" error is thrown when the Target class cannot be instantiated. As you could plug in targets into NLog, NLog don't know which are available forehand. Please also note that not all targets are available in all platforms.

The latter is als the case for the eventlog target - is not available in all platforms:

See https://github.com/NLog/NLog/wiki/EventLog-target

Platforms Supported: Limited (Only available for Net35, Net40, Net45 and NetStandard 2.0. Note: NetStandard 2.0 must use NLog.WindowsEventLog package)

If you're using .NET Core 1.x, it won't work - Microsoft did not port the API because the eventlog is Windows only.

For .NET Core 2 you need to install the NLog.WindowsEventLog package and add this in your nlog.config (on top):

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

Please note: the error you're posting will tell that the target is unavailable. The config seems to be valid and no changes are needed in the logger code. Also NLog.config or nlog.config is not an issue.

Elastic search

Maybe I missed it, but why using the eventlog target for Elastic search? There is a Elastic search target. Package https://www.nuget.org/packages/NLog.Targets.ElasticSearch

This package supports .NET Standard 1.3+ and .NET Standard 2+

usage:

<nlog>
  <extensions>
    <add assembly="NLog.Targets.ElasticSearch"/>
  </extensions>
  <targets>
    <target name="elastic" xsi:type="BufferingWrapper" flushTimeout="5000">
      <target xsi:type="ElasticSearch"/>
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="elastic" />
  </rules>
</nlog>

Thanks to all of you!

I got it working, logs are writing into Elasticsearch now. My current configuration looks like that:

<?xml version="1.0" encoding="utf-8" ?>
<nlog autoReload="true" throwExceptions="false"
      internalLogLevel="Error" internalLogFile="NLogError.log"
      xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>
  <targets>
    <target xsi:type="Network"
        name="CentralLog"
        newLine ="false"
        maxMessageSize="65000"
        connectionCacheSize="5"
        encoding="utf-8"
        keepConnection="false"
        maxQueueSize="100"
        address="tcp://abc.xyz.org:5544"
        onOverflow="Split">
     <layout type="JsonLayout">
      <attribute name="machinename" layout="${machinename}" />
      <attribute name="level" layout="${level:upperCase=true}" />
      <attribute name="processname" layout="${processname}" />
      <attribute name="processid" layout="${processid}" />
     </layout>
   </target>
  </targets>
  <rules>
  <logger name="*" minlevel="Trace" writeTo="CentralLog" />
  </rules>
</nlog>

You have misplaced the log attribute in <logger> instead of <target>. Please try the following config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog autoReload="true" throwExceptions="false"
  internalLogLevel="Error" internalLogFile="NLogError.log"
  xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="EventLog" xsi:type="EventLog"
      layout="${longdate} ${level} ${callsite} -> ${message} 
      ${exception:format=Message,StackTrace}"
      source="MonDBSvc" log="Application" />
  </targets>
  <rules>
    <logger name="MonDbSvc" minlevel="Error" writeTo="EventLog" />
  </rules>
</nlog>

UPDATE

Also, please try to initialize your logger in c# file like below:

var logger = LogManager.GetLogger("MonDbSvc");

You can find more details here for logging the message:

https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

Use this to provide the access to event log: https://www.nuget.org/packages/NLog.WindowsEventLog

Added the tag in the config file:

<extensions>
    <add assembly="NLog.Web.AspNetCore"/>
</extensions>

Hope this will helps you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!