You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NLog无法加载无项目引用的自定义扩展程序集问题求助

NLog无法加载无项目引用的自定义扩展程序集问题求助

我现在遇到一个NLog自定义Target加载的棘手问题,想请教各位帮忙排查下。具体情况是这样的:

我想给UiPath这个第三方应用使用自定义的NLog Target,它本身是基于NLog做日志输出的。因为没法把我的Target程序集添加成它的项目依赖,我就把自己的MyTarget.dll复制到了UiPath存放NLog.dll和NLog.config的同一个文件夹里。

我已经按照NLog的配置文档更新了NLog.config,本来以为这样自定义Target就能从程序集里自动注册成功了。现在我的配置是这样的(MyTarget是个极简版的Target,专门用来排除Target本身的依赖或运行时问题):

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  internalLogFile="D:\nlog-internal.txt" internalLogLevel="Trace">
<variable name="WorkflowLoggingDirectory" value="${specialfolder:folder=LocalApplicationData}/UiPath/Logs" />
<rules>
<logger name="*" writeTo="WorkflowLogFiles" final="true" />
<logger name="*" writeTo="MyFirst" final="true" />
</rules>
<targets>
<target type="File" name="WorkflowLogFiles" fileName="${WorkflowLoggingDirectory}/${shortdate}_Execution.log" layout="${basedir} ==> ${message}," keepFileOpen="true" openFileCacheTimeout="5" concurrentWrites="true" encoding="utf-8" writeBom="true" />
<target type="MyFirst, MyTarget" name="MyFirst" Host="na" layout="${message}" />
</targets>
</nlog>

这个例子里我是通过<target>标签里的完全限定名来指定程序集的,另外我还试过用<extensions>节点的方式来注册:

<extensions>
<assembly>MyTarget</assembly>
</extensions>

对应的Target配置改成:

<target type="MyFirst" name="MyFirst" layout="${message}" />

不管用哪种配置方式,nlog-internal.txt里都会报同样的错误:

Warn Error has been raised. Exception: NLog.NLogConfigurationException: Error loading extensions: MyTarget
---> System.IO.FileNotFoundException: Could not load file or assembly 'MyTarget, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'MyTarget, Culture=neutral, PublicKeyToken=null'

我实在搞不懂为啥加载不了,可能是漏了什么非常基础的配置细节?我还做了个极简的复现示例(Program.cs代码如下):

using NLog;

namespace Example
{
static class Program
{
static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static void Main()
{
Logger.Info("Hello!");
Console.ReadKey();
}
}
}

这个示例的表现和UiPath里完全一致:只要我编译前把MyTarget.dll添加成项目依赖,Target就能正常找到并工作;但如果不添加依赖,只把dll扔到输出目录里,就会出现同样的加载失败问题。

(编辑:最后一句里的“assembly”改成了“dependency”,参考下方评论)

备注:内容来源于stack exchange,提问作者Tom Heintzberger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 14:09:05