Log4Net: Rolling File appender, define extension

余生颓废 提交于 2019-12-02 16:28:29
gregwhitaker

Try adding the .log extension to your date pattern like so and remove it from the file attribute.

<datePattern value="yyyy-MM-dd.lo\g"/>
...
<staticLogFileName value="false" />

The other answers escape the "g" in "log" since "g" is a special character in datePattern. This isn't wrong, but I prefer to wrap the entire set of non-date characters in single quotes, like so:

<datePattern value="yyyy-MM-dd'.log'" />

This gives the same results, but is easier for me to manage. This way, I don't have to recall which specific characters are special for datePattern (the list is long and varied). If I forget one character then I don't run the risk of borking my file names; they're all nicely escaped en masse.

log4net now also provides a PreserveLogFileNameExtension property that can force your .log extension to the end of the compound file name (including date pattern and/or size sequence number):

<file value="LogFiles/.log"/>
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyy-MM-dd" />

add ".lo\g" to the end of your datepattern

Talon

This is my log file xml config. The path to the log file is in the "file" tag

This will create a log file "2012-11-22.log" under the folder "LogFiles" in the route folder of my website.

NOTE: Make sure that the folder exists first!

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,     log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="INFO"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="LogFiles/"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <maxSizeRollBackups value="5"/>
      <maximumFileSize value="10MB"/>
      <datePattern value="yyyy-MM-dd'.log'" />
      <staticLogFileName value="false"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
      </layout>
    </appender>
  </log4net>
</configuration>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!