12

All I want to do is append the current date and time to my log file, say:

"export_(Wed_Feb_21_2009_at_1_36_41PM)"

Here is my current config from my app.config

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="c:\export.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message %stackTrace%newline" />
    </layout>
</appender>

Is appending the date to my log file possible, or is it one of those things I need to do in code and not config?

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Chris
  • 6,702
  • 8
  • 44
  • 60

5 Answers5

18

To produce file name like:

log_2013-12-19.txt

make changes

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="log_"/>
    <param name="RollingStyle" value="Date"/>
    <param name="DatePattern" value="yyyy-MM-dd.\tx\t" />
    <param name="StaticLogFileName" value="false"/>
</appender>

Please observe param "DatePattern" where .\tx\t makes the file name extension .txt. If you provide .txt instead of .\tx\t, then this would save file name with extension .PxP if the time is PM or .AxA in case of AM. so I used \t to enforce to write character instead of pattern. Time may also be added and what ever time pattern needed.

So, this may be really what Philipp M wanted.

Muhammad Rizwan
  • 348
  • 2
  • 12
13

For those who are interested, here is the solution:

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="C:\\Users\\chris\\Documents\\log_.txt"/>
    <param name="RollingStyle" value="Date"/>
    <param name="DatePattern" value="_(yyyy.MM.dd-hh_mm_ss)"/>
    <param name="StaticLogFileName" value="false"/>
    <maximumFileSize value="100KB" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />           
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message %stackTrace%newline" />
    </layout>
</appender>

and the unit test which verifies this:

[Test]
public void TestLogger()
{
    logger.Info("Start Log");

    for (int i = 0; i < 2500; i++)
    {
        logger.Info(i);
    }

    logger.Info("End Log Log");
}

It produces the following output:

    log_.txt_(2009.02.19-01_16_34)

Not really what I wanted, but better than what I had before.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Chris
  • 6,702
  • 8
  • 44
  • 60
  • 3
    I used this DatePattern: and it produced a file with this format: log_(2011.05.05-11_28_47).txt also named File to "log", instead of log.txt. – Rauland May 05 '11 at 09:33
  • Hi, I have been trying to get my Log4Net to have different files according to date. I have followed the instructions not only at this site but also several sites. But I cannot get it done. Any idea that I might be missing? – william Jan 19 '12 at 02:09
  • @william you would be better served by asking a separate question and sharing your code rather than asking a question in the comments of this one. – Ryan Gates Oct 07 '13 at 15:06
9

Add the following to your config file

<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
  <param name="File" value="c:\\ProjectX\\Log\\log.txt"/>
  <param name="AppendToFile" value="true"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <param name="RollingStyle" value="Date"/>
  <param name="DatePattern" value="yyyy.MM.dd"/>
  <param name="StaticLogFileName" value="true"/>
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
  </layout>
</appender>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Nuno G
  • 2,035
  • 3
  • 14
  • 16
  • Is there anything I need for the file param? The date still isn't being attached. Perhaps you could post the entire config? – Chris Feb 17 '09 at 16:48
  • Sure, I have edited the answer above and added the whole appender section. – Nuno G Feb 18 '09 at 09:16
  • Sorry... still no go. Are you using "log4net" or "Common.Logging"? I'm using Common.Logging. I still get a file named "log.txt" – Chris Feb 19 '09 at 09:11
  • Just to be sure there is no misunderstanding here - the current file is always called "log.txt". Every day, upon logging for the 1st time, the former file gets renamed to log.txt. – Nuno G Feb 19 '09 at 14:36
  • OK, that makes sense. Also, you'd have to change "StaticLogFileName" to false, or the logger will not produce a second file, it will just append to the first. – Chris Feb 19 '09 at 20:14
3

If you want to use a .log suffix (to generate a file like myLogPrefix.20160309.log) use:

<appender name="GeofenceFileAppender" type="log4net.Appender.RollingFileAppender">
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
        <file type="log4net.Util.PatternString" value="C:\\Logs\\myLogPrefix" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value=".yyyyMMdd.lo'g'" />
        <staticLogFileName value ="false" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger – %message%newline" />
        </layout>
</appender>

The gotchas are the staticLogFileName = false which allows you to generate rollups on the fly (instead of having to restart the service\server) and the awkward 'g' in the datepattern since g is a magic character in datePattern. There isn't good documentation on the log4net site to study this, so it's really wisdom gained from trial and error and scouring other people's experiences.

arviman
  • 5,087
  • 41
  • 48
1

Use StaticLogFileName:

<param name="StaticLogFileName" value="true"/>
Bob Nadler
  • 2,755
  • 24
  • 20
  • 1
    With StaticLogFileName true, your rolling files will be date/time stamped instead of sequential (.1, .2, etc.). Now that I look at it, you have to set the rollingStyle to either "Date" or "Composite" for this to work. The RollingFileAppender doc. is pretty clear on these settings. – Bob Nadler Feb 10 '09 at 20:38
  • Check RollingFileAppender section: http://logging.apache.org/log4net/release/config-examples.html – Mike Cole Feb 25 '13 at 20:12