解析 nlog.config 时出现 NLog 异常 - 找不到目标:'EventLog'
Posted
技术标签:
【中文标题】解析 nlog.config 时出现 NLog 异常 - 找不到目标:\'EventLog\'【英文标题】:NLog Exception when parsing nlog.config - Target cannot be found: 'EventLog'解析 nlog.config 时出现 NLog 异常 - 找不到目标:'EventLog' 【发布时间】:2019-06-12 07:15:13 【问题描述】:我正在 Visual Studio 2017 上开发 C# .NET Core 2.2 Web-Api。 我实现了 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 文件在应用程序文件夹的同一根目录下。
我尝试为文件名和此代码编写 NLog.config nad nlog.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
【问题讨论】:
可能是 nlog.config 文件格式的问题,看这里:github.com/nlog/NLog/wiki/EventLog-target 嘿Donal,我尝试使用您发布的链接中的示例,但我仍然遇到同样的错误。当我擦除孔标签目标时,我收到找不到它的错误。对我来说意味着它正在寻找正确的标签目标,但它有一些问题吗?!错误:NLog.NLogConfigurationException:找不到日志记录规则的目标“EventLog”:MonDbSvc。 尝试删除规则部分 将internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log"
作为属性添加到您的<nlog>
标签。然后文件会告诉你错误是什么。
@Priyank Panchal 我在初始消息中添加了上面的警告日志消息。
【参考方案1】:
关于找不到目标:'EventLog'错误
无法实例化 Target 类时会抛出“Target cannot be found”错误。由于您可以将目标插入 NLog,NLog 不知道哪些是可用的正手。另请注意,并非所有目标都适用于所有平台。
事件日志目标也是如此 - 并非在所有平台上都可用:
见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 也不是问题。
弹性搜索
也许我错过了,但为什么要使用事件日志目标进行弹性搜索?有一个弹性搜索目标。包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>
【讨论】:
【参考方案2】:谢谢大家!
我得到了它,日志现在正在写入 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>
【讨论】:
看到一个文件目标而不是 Elasticsearch 目标有点令人困惑 ;) @Julian 打扰一下,我调整了上面的文件。 好的,无论如何,对于这个配置<add assembly="NLog.Web.AspNetCore"/>
是不需要的。您的示例中没有任何内容使用 NLog.Web.AspNetCore。见nlog-project.org/config【参考方案3】:
您在<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");
【讨论】:
@FrankMehlhop,如果对您有帮助,请查看我的更新。 @Priyank Panchal 同样,这个新的记录器初始化也不起作用。它没有写入 Elasticsearch,当然也没有使用我的 nlog.config。但重要的是,我可以使用我的配置。 @FrankMehlhop,您的 nlog.config 文件配置为在 EventLog 中写入日志,而不是在 elasticsearch 中。我不知道它是如何与elasticsearch相关联的。抱歉,我没有在 NLog 中使用 elasticsearch。你的异常消失了吗? @PriyankPanchal,你说得对,nlog.config 文件被配置为在 EventLog 中写入日志,而不是在 elasticsearch 中。 这是一个有效的观察结果(日志属性放错了位置),但请注意,“找不到目标”异常与 XML 配置/布局无关。【参考方案4】:您可以在此处找到有关记录消息的更多详细信息:
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>
希望对你有帮助。
【讨论】:
您的解决方案将在文件中打印日志,而 OP 正在尝试在 EventLog 中打印日志。您提供的解决方案非常通用,可能不是 OP 正在寻找的。span> 没错,Priyank Panchal。并且日志级别 Info 也没有提供更详细的信息。 System.ArgumentException:找不到目标:'EventLog',为此您需要使用:nuget.org/packages/NLog.WindowsEventLog @Jayoti Parkash 我实现了这个包,但这并不能解决我的问题。 在你的 NLog.config 顶部添加程序集:以上是关于解析 nlog.config 时出现 NLog 异常 - 找不到目标:'EventLog'的主要内容,如果未能解决你的问题,请参考以下文章
使用 anotar catel nlog 日志记录时无法过滤类名(在 NLog.config 中)
安装了 NLog 但 NLog.config 文件被锁定在我的 .netcore xUnit 测试项目中