[英]Want to pass dynamic File path from main web application to a separate DLL. That DLL is separately done using NLog
This is the code I am using to pass the file path and giving the file path in NLOG.config file in DLL (class library) from other web application to generate the logs file . 这是我用来传递文件路径并从其他Web应用程序提供DLL(类库)中NLOG.config文件中文件路径的代码,以生成日志文件。 But I am unable to do this. 但是我做不到。
string filePath = path;
var target = (FileTarget)LogManager.Configuration.FindTargetByName("logFile");
target.FileName = "" + filePath + "/current.log";
LogManager.ReconfigExistingLoggers();
Normally there is only one global NLog.config for the entire application. 通常,整个应用程序只有一个全局NLog.config。
Instead of modifying the individual targets in the active NLog-config, then I think it is easier to modify the NLog global variables. 我认为与其修改活动NLog-config中的单个目标,不如修改NLog全局变量更容易。
https://github.com/NLog/NLog/wiki/Gdc-layout-renderer https://github.com/NLog/NLog/wiki/Gdc-layout-renderer
https://github.com/NLog/NLog/wiki/Var-Layout-Renderer https://github.com/NLog/NLog/wiki/Var-Layout-Renderer
You could do it like this: 您可以这样做:
<target type="file" filename="${gdc:item=MyAppPath}current.log" />
And then at startup execute this command to modify the GDC: 然后在启动时执行以下命令来修改GDC:
NLog.GlobalDiagnosticsContext.Set("MyAppPath", filePath + "/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.