C# 如何使 Nlog 归档带有日志记录日期的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1635973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make Nlog archive a file with the date when the logging took place
提问by Carl Bergquist
We are using Nlog as our logging framework and I cannot find a way to archive files the way I want. I would like to have the date of when the logging took place in the logging file name.
Ex All logging that happend from 2009-10-01 00:00 -> 2009-10-01:23:59
should be placed in Log.2009-10-01.log
. But all the logs for this day should be placed in Log.log
for tailing and such.
我们正在使用 Nlog 作为我们的日志记录框架,但我找不到一种以我想要的方式归档文件的方法。我想在日志文件名中包含日志记录的日期。
Ex 发生的所有日志记录2009-10-01 00:00 -> 2009-10-01:23:59
都应该放在Log.2009-10-01.log
. 但是这一天的所有原木都应该放在里面Log.log
做拖尾之类的。
The current NLog.config that I use looks like this.
我使用的当前 NLog.config 看起来像这样。
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<extensions>
<add assembly="My.Awesome.LoggingExentions"/>
</extensions>
<targets>
<target name="file1" xsi:type="File"
fileName="${basedir}/Logs/Log.log"
layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
archiveEvery="Day"
archiveFileName="${basedir}/Logs/Log${shortdate}-{#}.log"
archiveNumbering="Sequence"
maxArchiveFiles="99999"
keepFileOpen="true"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file1" />
</rules>
</nlog>
This however sets the date in the logfile to the date when the new logfile is created. Which cause frustration when you want to read logs later.
但是,这会将日志文件中的日期设置为创建新日志文件的日期。当您想稍后阅读日志时,这会导致沮丧。
It also seems like I have to have atleast one # in the archiveFileName, which I rather not. So if you got a solution for that also I would be twice as thankful =)
似乎我还必须在存档文件名中至少有一个 #,我宁愿没有。因此,如果您对此也有解决方案,我将感激不尽 =)
采纳答案by the_joric
Just in case if somebody still needs a solution -- requested feature has been added to NLog recently: https://github.com/NLog/NLog/pull/241, but it is still not available by Nuget
以防万一如果有人仍然需要解决方案 - 最近已将请求的功能添加到 NLog:https: //github.com/NLog/NLog/pull/241,但 Nuget 仍然不可用
回答by Brian
Probably too late to help you, but I believe all you need to do is include the date in the file name using the date layout rendererwith the proper date format. By including the date, you don't need to specify the archive features. When the date changes, a new file will be automatically created.
可能为时已晚,无法帮助您,但我相信您需要做的就是使用具有正确日期格式的日期布局渲染器在文件名中包含日期。通过包含日期,您无需指定存档功能。当日期更改时,将自动创建一个新文件。
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<extensions>
<add assembly="My.Awesome.LoggingExentions"/>
</extensions>
<targets>
<target name="file1" xsi:type="File"
fileName="${basedir}/Logs/${date:format=yyyy-MM-dd}.log"
layout="${longdate} ${level:uppercase=true:padding=5} ${session} ${storeid} ${msisdn} - ${logger:shortName=true} - ${message} ${exception:format=tostring}"
keepFileOpen="true"
/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file1" />
</rules>
</nlog>
回答by Shoham
Maybe this is what you need, Daily folder with the file Log.log in it
也许这就是您所需要的,其中包含文件 Log.log 的 Daily 文件夹
<target xsi:type="File" name="file1" fileName="${basedir}/logs/Log.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>