Update NLog target filename at runtime

前端 未结 7 1683
被撕碎了的回忆
被撕碎了的回忆 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:44

    This worked for me.

    using NLog;
    using NLog.Targets;
    using NLog.Targets.Wrappers;
    
    var target = LogManager.Configuration.FindTargetByName("logfile");
    var asyncTarget = target as AsyncTargetWrapper;
    var fileTarget = asyncTarget.WrappedTarget as FileTarget;
    var file = fileTarget.FileName;
    var archiveFile = fileTarget.ArchiveFileName;
    

    //FileName and ArchiveFileName are of type Layout. ToString gives the string representaion. We can change it and assign it back. Remember to put .Trim('\'') for removing the extra single quotes that gets added while converting to string.

提交回复
热议问题