想要将动态文件路径从主Web应用程序传递到单独的DLL。该DLL使用NLog单独完成

问题描述 投票:0回答:1

这是我用来传递文件路径并在DLL(类库)中的NLOG.config文件中从其他Web应用程序提供文件路径以生成日志文件的代码。但我无法做到这一点。

string filePath = path;
var target = (FileTarget)LogManager.Configuration.FindTargetByName("logFile");
target.FileName = "" + filePath + "/current.log";
    LogManager.ReconfigExistingLoggers();
c# nlog
1个回答
1
投票

通常,整个应用程序只有一个全局NLog.config。

而不是修改活动NLog-config中的各个目标,我认为修改NLog全局变量更容易。

https://github.com/NLog/NLog/wiki/Gdc-layout-renderer

https://github.com/NLog/NLog/wiki/Var-Layout-Renderer

你可以这样做:

<target type="file" filename="${gdc:item=MyAppPath}current.log" />

然后在启动时执行此命令以修改GDC:

NLog.GlobalDiagnosticsContext.Set("MyAppPath", filePath + "/");
© www.soinside.com 2019 - 2024. All rights reserved.