C# Log4net 滚动每日文件名,文件名中包含日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1165084/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:45:39  来源:igfitidea点击:

Log4net rolling daily filename with date in the file name

c#logginglog4netfilenames

提问by JL.

I would like to have files named for example:

我想要命名的文件,例如:

dd.mm.yyyy.log

dd.mm.yyyy.log

How is this possible with log4net?

log4net怎么可能做到这一点?

采纳答案by Mun

In your Log4net config file, use the following parameter with the RollingFileAppender:

在您的 Log4net 配置文件中,将以下参数与 RollingFileAppender 一起使用:

<param name="DatePattern" value="dd.MM.yyyy'.log'" />

回答by BobD

For a RollingLogFileAppender you also need these elements and values:

对于 RollingLogFileAppender,您还需要这些元素和值:

<rollingStyle value="Date" />
<staticLogFileName value="false" />

回答by Nonkichi

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <file value="logs\" />
  <datePattern value="dd.MM.yyyy'.log'" />
  <staticLogFileName value="false" />
  <appendToFile value="true" />
  <rollingStyle value="Composite" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="5MB" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>

回答by AllenM

The extended configuration section in a previous response with

先前响应中的扩展配置部分

 ...
 ...
 <rollingStyle value="Composite" />
 ...
 ...

listed works but I did not have to use

列出的作品,但我不必使用

<staticLogFileName value="false" /> 

. I think the RollingAppender must (logically) ignore that setting since by definition the file gets rebuilt each day when the application restarts/reused. Perhaps it does matter for immediate rollover EVERY time the application starts.

. 我认为 RollingAppender 必须(逻辑上)忽略该设置,因为根据定义,当应用程序重新启动/重用时,每天都会重建文件。也许每次应用程序启动时立即翻转确实很重要。

回答by Hyman Thorley

Using Log4Net 1.2.13 we use the following configuration settings to allow date time in the file name.

使用 Log4Net 1.2.13,我们使用以下配置设置来允许文件名中的日期时间。

<file type="log4net.Util.PatternString" value="E:/logname-%utcdate{yyyy-MM-dd}.txt" />

<file type="log4net.Util.PatternString" value="E:/logname-%utcdate{yyyy-MM-dd}.txt" />

Which will provide files in the following convention: logname-2015-04-17.txt

它将提供以下约定的文件: logname-2015-04-17.txt

With this it's usually best to have the following to ensure you're holding 1 log per day.

有了这个,通常最好有以下内容以确保您每天持有 1 个日志。

<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />

If size of file is a concern the following allows 500 files of 5MB in size until a new day spawns. CountDirectionallows Ascending or Descending numbering of files which are no longer current.

如果文件大小是一个问题,以下允许 500 个 5MB 大小的文件,直到新的一天出现。CountDirection允许对不再是当前文件的文件进行升序或降序编号。

 <maxSizeRollBackups value="500" />
 <maximumFileSize value="5MB" />
 <rollingStyle value="Composite" />
 <datePattern value="yyyyMMdd" />
 <CountDirection value="1"/>
 <staticLogFileName value="true" />

回答by Njal

I ended up using (note the '.log' filename and the single quotes around 'myfilename_'):

我最终使用了(注意'.log'文件名和'myfilename_'周围的单引号):

  <rollingStyle value="Date" />
  <datePattern value="'myfilename_'yyyy-MM-dd"/>
  <preserveLogFileNameExtension value="true" />
  <staticLogFileName value="false" />
  <file type="log4net.Util.PatternString" value="c:\Logs\.log" />

This gives me:

这给了我:

myfilename_2015-09-22.log
myfilename_2015-09-23.log
.
.

回答by Fourat

To preserve file extension:

要保留文件扩展名:

<log4net>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="D:\LogFolder\%date{yyyyMM}\SchT.log" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <maximumFileSize value="30MB" />
      <staticLogFileName value="true" />
      <preserveLogFileNameExtension value="true"/>
      <datePattern value="ddMMyyyy" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>

回答by Matt

I've tried all the answers, but there was always something missing and not functioning as expected for me.

我已经尝试了所有的答案,但总是缺少一些东西并且没有按我的预期运行。

Then I experimented a bit with the hints given in each answer and was successful with the following setting:

然后我对每个答案中给出的提示进行了一些试验,并通过以下设置取得了成功:

<appender name="RollingActivityLog" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="C:\temp\LOG4NET_Sample_Activity.log" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <staticLogFileName value="false" />
  <preserveLogFileNameExtension value="true" />
  <datePattern value="-yyyyMMdd" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level - %message%newline" />
  </layout>
</appender>

The issue with other combinations of parameters was that the latest file didn't have the time pattern, or that the time pattern was appended as .log20171215which created a new file time (and a new file type!) each day - or both issues appeared.

其他参数组合的问题是最新的文件没有时间模式,或者时间模式被附加为每天.log20171215创建一个新的文件时间(和一个新的文件类型!) - 或者两个问题都出现了。

Now with this setting you are getting files like this one:

现在使用此设置,您将获得如下文件:

LOG4NET_Sample_Activity-20171215.log

LOG4NET_Sample_Activity-20171215.log

which is what I wanted.

这就是我想要的。



To summarize:

总结一下:

  • Don't put the date pattern in the <file value=...attribute, just define it in the datePattern.

  • Make sure you have the preserveLogFileNameExtensionvalueattribute set to true.

  • Make sure you have the staticLogFileNamevalueset to false.

  • Set the rollingStyleattribute valueto Date.

  • 不要将日期模式放在<file value=...属性中,只需在datePattern.

  • 确保您将preserveLogFileNameExtensionvalue属性设置为true

  • 确保您将staticLogFileName设置为false

  • rollingStyle属性设置为Date

回答by Parag Bangad

I moved configuration to code to enable easy modification from CI using system variable. I used this code for file name and result is 'Log_03-23-2020.log'

我将配置移动到代码中,以便使用系统变量从 CI 轻松修改。我使用此代码作为文件名,结果是“Log_03-23-2020.log”

            log4net.Repository.ILoggerRepository repository = LogManager.GetRepository(Assembly.GetEntryAssembly());
            Hierarchy hierarchy = (Hierarchy)repository;
            PatternLayout patternLayout = new PatternLayout();
            patternLayout.ConversionPattern = "%date %level - %message%newline%exception";
            patternLayout.ActivateOptions();

            RollingFileAppender roller = new RollingFileAppender();
            roller.AppendToFile = true;
            roller.File = "Log_";
            roller.DatePattern = "MM-dd-yyyy'.log'";
            roller.Layout = patternLayout;
            roller.MaxFileSize = 1024*1024*10;
            roller.MaxSizeRollBackups = 10;
            roller.StaticLogFileName = false;
            roller.RollingStyle = RollingFileAppender.RollingMode.Composite;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);