Log4net保留LogFileNameExtension无法正常工作 [英] Log4net preserveLogFileNameExtension not working

查看:95
本文介绍了Log4net保留LogFileNameExtension无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的log4net配置,扩展日志文件创建了错误的扩展名.创建的第一个文件名为log_debug.txt,过渡文件名为log_debug.txt.1.但理想情况下,应该为log_debug.1.txt.

this is my log4net configuration, the rollover log file is created wrong extension. The first file created with name of log_debug.txt and the rollover file created with log_debug.txt.1. but ideally it should be log_debug.1.txt.

我使用了keepLo​​gFileNameExtension值为true,但似乎无法正常工作.您能检查一下并让我知道是否有问题吗?

I used preserveLogFileNameExtension value to be true, but it seems not working. Can you please check and let me know if anything wrong?

<appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
    <filter type="log4net.Filter.LevelRangeFilter">
      <levelMin value="DEBUG" />
      <levelMax value="DEBUG" />
    </filter>
    <file value=".\logs\log_debug.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="20" />
    <maximumFileSize value="2MB" />
    <preserveLogFileNameExtension value="true" />
    <staticLogFileName value="true" />
    <layout type="propertyPatternLayout">
      <conversionPattern value="%date || Thread=%thread" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  </appender>

推荐答案

我的第一个答案(如下)是基于log4net v1.2.10的,我很高兴地向做的v1.2.13版本 包含preserveLogFileNameExtension.一些测试证实该属性也可以按预期运行,并且不依赖于staticLogFilename属性.

My first answer (below) was based on v1.2.10 of log4net and I was gently prodded towards the v1.2.13 version which does contain the preserveLogFileNameExtension. Some tests confirmed that the property also works as intended, and is not dependent on the staticLogFilename property.

我认为OP使用的是不包含该属性的log4net的早期版本,因此表现出以下所述的行为(making3和Sivakumar评论在IMO上当场

I think that OP is using a previous version of log4net that doesn't contain the property and thus exhibits the behavior described below (making3 and Sivakumar comment are on the spot IMO)

再次感谢您将我从1.2.10凹槽中唤醒.

Thank again for waking me out of the 1.2.10 groove.

第一件事:香草log4net中没有RollingFileAppender(或任何附加程序)的preserveLogFileNameExtension属性(我正在查看版本1.2.10.0).大多数log4net组件都会使用您发送的任何属性配置,而不会抱怨,但不一定会转化为组件中的行为

First thing first: there is no preserveLogFileNameExtension property for a RollingFileAppender (or any appender) in vanilla log4net (I'm looking at version 1.2.10.0). Most log4net components will consume any property configuration you send their way without complaining but it doesn't necessarily translate to a behavior in the component

按大小(或复合大小)滚动时,会发生以下情况:对滚动进行硬编码,以在日志文件后缀上迭代编号.您将始终获得带有与下一个文件号匹配的n的"file.log.n"(取决于您的maxSizeRollBackupscountDirection值).您无法使用当前的RollingFileAppender避免这种情况(请查看RollingFileAppender.RollOverRenameFiles方法)

When rolling on size (or composite), what happens is that the rolling over is hardcoded to suffix the log file with the iteration number. You will always get a "file.log.n" with n matching the next file number (which depends on your maxSizeRollBackups and countDirection values). You cannot avoid this with the current RollingFileAppender (take a look at the RollingFileAppender.RollOverRenameFiles method)

该怎么办?您可以决定按日期滚动.在这种情况下,可以使用datePattern属性.滚动时,datePattern会附加在文件名后面,因此您可以使用

What to do? You can decide to roll by date. In this case it is possible to use the datePattern property. The datePattern is appended to the file name when rolling, so you can use something like

<file value="file.log" />
<datePattern value=".yyyy-MM-dd-hh-mm-ss'.log'" />

,它将保留扩展名并创建一个类似于file.log.2014-12-11-11-47-54.log的文件(请参见文件名中的原始.log扩展名).但是您正在失去尺寸限制方面.如果您选择Composite,则会遇到相同的问题;因为RollOverSize总是出现在RollOverDate之后,所以会出现数字前缀.

which would preserve the extension and create a file like file.log.2014-12-11-11-47-54.log (see the original .log extension in the file name). But you are losing the size limitation aspect. if you go for Composite you will encounter the same problem; since RollOverSize always occur after RollOverDate the number prefix will be present.

您还可以决定使用您想要/需要的命名约定来推出(啊啊)您自己的附加程序.您甚至可以尝试从香草RollingFileAppender继承,而仅覆盖必要的方法. AdjustFileBeforeAppend是虚拟的,并且是所有翻转需求的起点;例如,您可以尝试将对RollOverDateRollOverSize的调用调回以使用复合模式...

You can also decide to roll out (ah ah) your own appender with the naming conventions you want/need. You could even try to inherit from the vanilla RollingFileAppender and only override necessary method. AdjustFileBeforeAppend is virtual and is the starting point for all rollovers needs; you could try for example to reverse the calls to RollOverDate and RollOverSize to use the Composite mode...

这篇关于Log4net保留LogFileNameExtension无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆