Update NLog target filename at runtime

前端 未结 7 1696
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 18:14

In my application, I work on several thousand of document a day. I\'d like, in some cases some logs, one log by document. Then I\'d like for a specific target change the out

7条回答
  •  醉话见心
    2020-12-05 18:57

    Try ReconfigExistingLoggers method:

    var target = (FileTarget)LogManager.Configuration.FindTargetByName("logfile");
    target.FileName = "${logDirectory}/file2.txt";
    LogManager.ReconfigExistingLoggers();
    

    As written in docs:

    Loops through all loggers previously returned by GetLogger. and recalculates their target and filter list. Useful after modifying the configuration programmatically to ensure that all loggers have been properly configured.

    EDIT:

    Try use custom layout renderer: NLog config file to get configuration setting values from a web.config

提交回复
热议问题