Log4Net: Rolling File appender, define extension
Asked Answered
M

5

71

I want my logfile to look something like this: 2009-02-13.log

but the problem is that I can't seem to find any way to add the .log extension.

I've tried a lot of things but nothing helps. This is what I have this far:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs/Log4Net/.log"/>
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <datePattern value="yyyy-MM-dd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
    </layout>
</appender>
Mollee answered 5/3, 2009 at 14:49 Comment(0)
E
60

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" />
Emphasize answered 5/3, 2009 at 14:58 Comment(1)
Also remember to add <staticLogFileName value="false" />Girder
O
122

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.

Overpay answered 16/3, 2009 at 17:12 Comment(3)
So what is the value you have for <file /> with this?Bump
@ssmith: Leave off the .log from <file />. Log4Net appends the datepattern onto the file value, so you want the file extension on the former.Overpay
Since I want to monitor a predictable log file name for the current day, I use for file value = logs/server.log ... then for datePattern value, I add a period before yyyy. Current log is server.log then yesterday's log is server.log.2014-08-13.log ... Its not ideal, but since I haven't figured another way to do it.Fasciate
E
60

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" />
Emphasize answered 5/3, 2009 at 14:58 Comment(1)
Also remember to add <staticLogFileName value="false" />Girder
K
21

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" />
Keverne answered 15/4, 2014 at 8:37 Comment(2)
Must be on newer versions only? I couldn't get that parameter to work on my system. I ended having to use .log twice, once in file value, and another time in datePattern :(Fasciate
I believe it was added in 1.2.12Amrita
B
8

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

Bosun answered 5/3, 2009 at 14:59 Comment(0)
R
4

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>
Raynold answered 22/11, 2012 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.