相关疑难解决方法(0)

在运行时更新NLog目标文件名

在我的应用程序中,我每天处理数千个文档.我想,在某些情况下,一些日志,一个按文档记录.然后我想要一个特定的目标在运行时更改输出文件名(和只有文件名).

在网络上我发现如何通过编程来创建目标我只想通过编程更新文件名.我试过下面的代码.我收到的错误是"无法找到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)

c# logging nlog

22
推荐指数
3
解决办法
3万
查看次数

如何将NLog的fileName设置为进程开始日期?

我试过了

<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)

但它不起作用

.net formatting configuration filenames nlog

7
推荐指数
1
解决办法
4636
查看次数

标签 统计

nlog ×2

.net ×1

c# ×1

configuration ×1

filenames ×1

formatting ×1

logging ×1