How to get path of current target file using NLog in runtime?

前端 未结 4 1714
猫巷女王i
猫巷女王i 2021-01-31 07:36

I use NLog with next configuration:

  
    

        
4条回答
  •  清歌不尽
    2021-01-31 07:57

    This did the trick for me:

    var fileTarget = (FileTarget) LogManager.Configuration.FindTargetByName("file");
    // Need to set timestamp here if filename uses date. 
    // For example - filename="${basedir}/logs/${shortdate}/trace.log"
    var logEventInfo = new LogEventInfo {TimeStamp = DateTime.Now}; 
    string fileName = fileTarget.FileName.Render(logEventInfo);
    if (!File.Exists(fileName))
        throw new Exception("Log file does not exist.");
    

提交回复
热议问题