在我的公司里,我们使用的是NLog。我们遇到了大量日志文件的问题。我们要做的是每天归档文件,最多保留x个文件。比方说7.我在互联网上读到了几个关于这个的话题,它们大多指向修改我的NLog.config文件的相同方向。然而,它似乎并不愿意像我期望的那样旋转文件。当前,所需文件夹中没有任何内容被存档。但所有文件都以以下格式保存在‘logs’目录中;
Log.info.2011-11-07.txt
在我的应用程序中,我有一个'logs‘目录。在该文件夹中保存所有日志文件。我还有一个名为“archives”的文件夹,我想在其中存档所有较旧的文件。在达到该目录中日志文件的最大数量后,应自动清除这些文件。这个是可能的吗?我当前的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" autoReload="true" throwExceptions="true" internalLogFile="C:\nlog-internal.txt" internalLogLevel="Error"> <extensions> <add assembly="NLog.Extended" /> </extensions> <targets> <!-- will move file to archive once it reaches 1MB. Files are archived by day, with a maximum of three files. ConcurrentWrites is set to false, change to true if multiple processes will be writing to the logfile--> <target name="file" xsi:type="File" fileName="${basedir}/logs/Log.info.${shortdate}.txt" layout="${longdate} ${callsite} ${level}: ${message} ${exception:format=Message,StackTrace} ${stacktrace}" archiveFileName="${basedir}/logs/archives/log.info.${shortdate}.txt" archiveAboveSize="1048576" archiveEvery="Day" archiveNumbering = "Rolling" maxArchiveFiles="7" concurrentWrites="false" <target name="file-default" xsi:type="File" fileName="${basedir}/log_default.txt"/> <target name="file-debug" xsi:type="File" fileName="${basedir}/log_debug.txt"/> <target name="file-testclass" xsi:type="File" fileName="${basedir}/log_testclass.txt"/> <target name="mail" xsi:type="Mail" subject="${level} - ${aspnet-request:serverVariabele=PATH_INFO} | ${callsite:includeSourcePath=true}" to="someone@mail.com" smtpServer="mail.server.com" from="no-reply@errormail.com"/> <target xsi:type="Database" name="TestDatabaseLogging" connectionString="Data Source=123.123.123.123;Initial Catalog=NLog_Test;User ID=su_Nlog;Password=test123" dbDatabase="NLog_Test"> <commandText> insert into INNO_LOG ([createDate], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (@createDate, @origin, @logLevel, @message, @exception, @stackTrace) </commandText> <parameter name="@createDate" layout="${date}"/> <parameter name="@origin" layout="${callsite}"/> <parameter name="@logLevel" layout="${level}"/> <parameter name="@message" layout="${message}"/> <parameter name="@exception" layout="${exception:format=Message,StackTrace}"/> <parameter name="@stackTrace" layout="${stacktrace}"/> </target> </targets> <rules> <logger name="*" minlevel="Fatal" writeTo="mail" /> <logger name="*" minlevel="Error" writeTo="TestDatabaseLogging" /> <logger name="*" minlevel="Debug" writeTo="file-debug" /> <logger name="*" minlevel="Info" writeTo="file" /> <!--Log to specific files for specific classes.--> <logger name="_Default" minlevel="Trace" writeTo="file-default" /> <logger name="TestClass" minlevel="Trace" writeTo="file-testclass" /> </rules> </nlog>
编辑:在ckellers回答之后, 最终(截取的)解决方案。
<target name="file" xsi:type="File" fileName="${basedir}/logs/Log.${level}.current.txt" layout="${longdate} ${callsite} ${level}: ${message} ${exception:format=Message,StackTrace} ${stacktrace}" archiveFileName="${basedir}/logs/archives/log.error.${shortdate}.{#}.txt" archiveAboveSize="5242880"