解析nlog.config时出现NLog异常-找不到目标:'EventLog'

时间:2019-06-12 07:15:13

标签: c# elasticsearch asp.net-web-api asp.net-core nlog

我正在 Visual Studio 2017 上开发 C#.NET Core 2.2 Web-Api 。 我实现了{strong> NLog 4.6.1,如github所述。 当我使用IIS启动应用程序时, NLog不会写入Elasticsearch 。在program.cs

的行上出现错误
var logger = NLog.Web.NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();

我在 NLogError.log 中找到错误消息:

2019-06-12 09:00:49.1606 Error Penter code herearsing configuration from NLog.config failed. Exception: NLog.NLogConfigurationException: Exception when parsing NLog.config.  ---> System.ArgumentException: Target cannot be found: 'EventLog'
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Config.LoggingConfigurationParser.ParseTargetsElement(ILoggingConfigurationElement targetsElement)
   at NLog.Config.LoggingConfigurationParser.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.LoggingConfigurationParser.LoadConfig(ILoggingConfigurationElement nlogConfig, String basePath)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(ILoggingConfigurationElement nlogElement, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- End of inner exception stack trace ---

我试图缩短配置文件以避免其他问题,但仍然是相同的错误。目标“ EventLog”将在此处找到。

NLog.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" />
          </targets>
          <rules>
            <logger name="MonDbSvc" minlevel="Error" writeTo="EventLog" 
              log="Application" />
          </rules>
      </nlog>

NLog.config文件和program.cs文件位于应用程序文件夹的同一根级别上。

我尝试写 NL og.config nad nl og.config 作为文件名,位于上面的代码行。

什么会导致此解析问题?我该如何解决?

感谢支持!

当我将日志级别从错误更改为警告时,我会得到更明确的消息。我确定该xml作为xml是有效的。

2019-06-12 10:27:51.3735 Info Message Template Auto Format enabled
2019-06-12 10:27:51.4347 Error Parsing configuration from NLog.config failed. Exception: NLog.NLogConfigurationException: Exception when parsing NLog.config.  ---> System.ArgumentException: Target cannot be found: 'EventLog'
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Config.LoggingConfigurationParser.ParseTargetsElement(ILoggingConfigurationElement targetsElement)
   at NLog.Config.LoggingConfigurationParser.ParseNLogSection(ILoggingConfigurationElement configSection)
   at NLog.Config.LoggingConfigurationParser.LoadConfig(ILoggingConfigurationElement nlogConfig, String basePath)
   at NLog.Config.XmlLoggingConfiguration.ParseNLogElement(ILoggingConfigurationElement nlogElement, String filePath, Boolean autoReloadDefault)
   at NLog.Config.XmlLoggingConfiguration.Initialize(XmlReader reader, String fileName, Boolean ignoreErrors)
   --- End of inner exception stack trace ---
2019-06-12 10:27:51.4347 Warn Failed loading config from NLog.config. Invalid XML?
2019-06-12 10:27:51.4597 Debug Watching path 'C:\Repositories\DataPicker\DataPicker\DataPicker.Api' filter 'NLog.config' for changes.
2019-06-12 10:27:51.4597 Debug --- NLog configuration dump ---
2019-06-12 10:27:51.4680 Debug Targets:
2019-06-12 10:27:51.4680 Debug Rules:
2019-06-12 10:27:51.4680 Debug --- End of NLog configuration dump ---
2019-06-12 10:27:51.4680 Trace FindReachableObject<System.Object>:
2019-06-12 10:27:51.4680 Info Found 0 configuration items
2019-06-12 10:27:51.4889 Debug Targets not configured for logger: DataPicker.Api.Program
2019-06-12 10:27:55.2679 Debug ScanAssembly('NLog.Web.AspNetCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2019-06-12 10:27:55.7717 Debug Hide assemblies for callsite
2019-06-12 10:27:55.7717 Trace Assembly 'NLog.Extensions.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c' will be hidden in callsite stacktrace
2019-06-12 10:27:55.7855 Debug ScanAssembly('NLog.Extensions.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
2019-06-12 10:27:55.7855 Trace Assembly 'Microsoft.Extensions.Logging, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' will be hidden in callsite stacktrace
2019-06-12 10:27:55.7855 Trace Assembly 'Microsoft.Extensions.Logging.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' will be hidden in callsite stacktrace
2019-06-12 10:27:55.8068 Debug Targets not configured for logger: Microsoft.AspNetCore.Hosting.Internal.ApplicationLifetime
2019-06-12 10:27:55.8068 Debug Targets not configured for logger: Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv
2019-06-12 10:27:55.8178 Debug Targets not configured for logger: Microsoft.AspNetCore.Server.Kestrel
2019-06-12 10:27:55.8465 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.TypeForwardingActivator
2019-06-12 10:27:55.8465 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager
2019-06-12 10:27:55.8622 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngGcmAuthenticatedEncryptorFactory
2019-06-12 10:27:55.8622 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.CngCbcAuthenticatedEncryptorFactory
2019-06-12 10:27:55.8622 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ManagedAuthenticatedEncryptorFactory
2019-06-12 10:27:55.8776 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlEncryptor
2019-06-12 10:27:55.8776 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository
2019-06-12 10:27:55.9084 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver
2019-06-12 10:27:55.9084 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider
2019-06-12 10:27:55.9084 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.Internal.DataProtectionStartupFilter
2019-06-12 10:27:55.9835 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor
2019-06-12 10:27:56.1465 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.Formatters.JsonPatchInputFormatter
2019-06-12 10:27:56.1465 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.Formatters.JsonInputFormatter
2019-06-12 10:27:56.1674 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.RazorPages.Internal.RazorProjectPageRouteModelProvider
2019-06-12 10:27:56.2420 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker
2019-06-12 10:27:56.2524 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler
2019-06-12 10:27:56.2524 Debug Targets not configured for logger: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector
2019-06-12 10:27:56.2707 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider
2019-06-12 10:27:56.2707 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker
2019-06-12 10:27:56.2814 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.Internal.ActionSelector
2019-06-12 10:27:56.2814 Debug Targets not configured for logger: Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler
2019-06-12 10:27:56.2984 Debug Targets not configured for logger: Microsoft.AspNetCore.Builder.RouterMiddleware
2019-06-12 10:27:56.3256 Debug Targets not configured for logger: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware
2019-06-12 10:27:56.3523 Debug Targets not configured for logger: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware
2019-06-12 10:27:56.3523 Debug Targets not configured for logger: Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware
2019-06-12 10:27:56.3592 Debug Targets not configured for logger: Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware
2019-06-12 10:27:56.3592 Debug Targets not configured for logger: Microsoft.AspNetCore.Hosting.Internal.WebHost
2019-06-12 10:27:56.3783 Debug Targets not configured for logger: Microsoft.AspNetCore.Hosting.Internal.HostedServiceExecutor

4 个答案:

答案 0 :(得分:2)

找不到关于目标的消息:“ EventLog”错误

无法实例化Target类时,引发“找不到目标”错误。因为您可以将目标插入NLog,所以NLog不知道哪些可用。另请注意,并非所有平台都提供所有目标。

后一种情况也适用于eventlog目标-并非在所有平台上都可用:

请参见https://github.com/NLog/NLog/wiki/EventLog-target

  

受支持的平台:受限制的(仅适用于Net35,Net40,Net45和NetStandard 2.0。注意:NetStandard 2.0必须使用NLog.WindowsEventLog程序包)

如果您使用的是.NET Core 1.x,它将无法正常工作-Microsoft不会移植API,因为事件日志仅适用于Windows。

对于.NET Core 2,您需要安装NLog.WindowsEventLog软件包并将其添加到您的nlog.config中(在顶部):

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

请注意:您发布的错误将告诉您该目标不可用。该配置似乎有效,并且记录器代码中无需更改。此外,NLog.config或nlog.config也不是问题。

弹性搜索

也许我错过了,但是为什么要使用eventlog目标进行弹性搜索?有一个弹性搜索目标。软件包https://www.nuget.org/packages/NLog.Targets.ElasticSearch

此软件包支持.NET Standard 1.3+和.NET Standard 2 +

用法:

<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>

答案 1 :(得分:1)

谢谢大家!

我知道了,日志现在正在写入Elasticsearch。 我当前的配置如下所示:

<?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>

答案 2 :(得分:0)

您已将日志属性放置在<logger>中而不是<target>中。请尝试以下配置:

<?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>

更新

此外,请尝试在c#文件中初始化记录器,如下所示:

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

答案 3 :(得分:0)

您可以在此处找到有关记录消息的更多详细信息:

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

使用此命令提供对事件日志的访问: https://www.nuget.org/packages/NLog.WindowsEventLog

将标记添加到配置文件中:

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

希望这会对您有所帮助。