【问题标题】:NLog can´t find custom target?NLog 找不到自定义目标?
【发布时间】:2020-05-12 10:07:39
【问题描述】:

我有一个 WinForm C# 项目,其中添加了 NLog 和自定义目标。为了加载这个目标,我在 NLog 配置中有这样的东西:

<logger name="GeneralLogger" minlevel="Warn" writeto="customTarget"></logger>

<target name="customTarget" type="LogToService"></target>

在 nlog-internal.log 我发现这个:

2020-05-12 11:48:16.0083 已引发警告错误。例外: NLog.NLogConfigurationException:创建目标类型失败: LogToService ---> System.ArgumentException:找不到目标: NLog.Config.Factory 中的“LogToService”`2.CreateInstance(String 项目名称)在 NLog.Config.LoggingConfigurationParser.CreateTargetType(字符串 targetTypeName) --- 内部异常堆栈跟踪结束 ---

我看到了有关使用程序集添加扩展的建议,但自定义目标与我添加 NLog Nuget 的项目在同一个项目中?

问候

【问题讨论】:

    标签: configuration config nlog target


    【解决方案1】:

    由于它不在单独的程序集中,您可以这样做:

    // NLog 4.7+
    NLog.LogManager.Setup().SetupExtensions(s =>
       s.RegisterTarget<MyNamespace.LogToService>("LogToService")
    );
    

    或 nlog.config 中的这个(&lt;targets&gt; 之前:

      <extensions> 
        <add assembly="YourAssembly"/> 
      </extensions> 
    

    有关所有选项,请参阅Register your custom component

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2014-03-25
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多