在我的应用程序中,我每天处理数千个文档.我想,在某些情况下,一些日志,一个按文档记录.然后我想要一个特定的目标在运行时更改输出文件名(和只有文件名).
在网络上我发现如何通过编程来创建目标我只想通过编程更新文件名.我试过下面的代码.我收到的错误是"无法找到LayoutRender'logDirectory'.
任何的想法 ?
谢谢,
var target = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
target.FileName = "${logDirectory}/file2.txt";
LoggingConfiguration config = new LoggingConfiguration();
var asyncFileTarget = new AsyncTargetWrapper(target);
config.AddTarget("logfile", asyncFileTarget);
LogManager.Configuration = config;
Run Code Online (Sandbox Code Playgroud)
配置文件是:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="logDirectory" value="C:/MyLogs"/>
<targets>
<target name="logfile" xsi:type="File" layout="${date:format=dd/MM/yyyy HH\:mm\:ss.fff}|${level}|${stacktrace}|${message}" fileName="${logDirectory}/file.txt" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud) 我试过了
<target name="txtFile"
xsi:type="File"
fileName="${date:format=yyyy-MM-dd HH-mm-ss}.txt"
layout="${longdate} ${level} ${message}"/>
Run Code Online (Sandbox Code Playgroud)
但它每分钟创建一个新文件.我意识到有,${processinfo:property=StartTime}但我无法格式化它.我试过了:
${processinfo:property=StartTime:format=yyyy-MM-dd HH-mm-ss}
Run Code Online (Sandbox Code Playgroud)
但它不起作用