在使用NLog输出日志到文件情况下,如果文件路径包含多层次结构,就会涉及到不同操作系统的文件路径分隔符不一样的问题,比如window下是\,而liunx下时/ 。
在NLog 4.7中,新增了一个配置项目${dir-separator},可以确保自动适配不同的操作系统路径。
1 | <target name= "logfile" xsi:type= "File" fileName= "log${dir-separator}App-${shortdate}.log" layout= "${longdate}--${logger}--${level}--${message}" ></target> |
那么无论运行于window还是linux,都可以保证文件将输入到 log目录下的新文件中。而不会在linux下出现问题(日志输出到一个名为 “log\\App-2021-1-1.log” 的文件内);
OS-dependend directory separator LayoutRender · Issue #3791 · NLog/NLog (github.com)
Added ${dir-separator} by 304NotModified · Pull Request #152 · NLog/NLog.github.io