是否可以在 internalLogFile 中使用 ${shortdate}?

Is it possible to use the ${shortdate} in the internalLogFile?

是否可以在internalLogFile中使用${shortdate}

<nlog internalLogFile="C:\Logs${shortdate}_nlog.log"
    <targets>
        <target name="logfile"
                fileName="C:/logs/${shortdate}_dev.log"
        </target>

我得到了预期日期的日志文件,但内部日志文件名为 ... ${shortdate}_nlog.log

简答:否

更长的答案:内部记录器文件名只是一个字符串。它在初始化期间被读入,XmlLoggingConfiguration class 确保目录存在,而(例如)FileTarget 使用 Layout 作为文件名,它转换使用 [= 提供的值13=].

https://github.com/NLog/NLog/issues/581#issuecomment-74923718

我从他们的评论中了解到,内部日志记录应该简单、稳定并谨慎使用。通常你应该只在试图找出你的设置出了什么问题时才打开它。

如果需要,您仍然可以根据日期时间动态命名内部日志文件。但是,它不会具有与目标文件相同的翻转效果。我认为每当您初始化记录器时,它基本上都会有不同的日期时间。

DateTime dt = DateTime.Now;                
NLog.Common.InternalLogger.LogFile = @"C:\CustomLogs\NLog_Internal\internal_NLogs" + dt.ToString("yyyy-MM-dd") + ".txt";