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?

5 Answers 5

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.

2
  • What if you wanted this to drop into a subfolder below App_Data? May 8, 2014 at 13:34
  • .\\App_Data would be the solution @JsonStatham or .\[Folder_Name]
    – Jean-Paul
    Mar 12, 2019 at 15:06
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.

3
  • 3
    I used this DatePattern: <param name="DatePattern" value="_(yyyy.MM.dd-hh_mm_ss).\tx\t"/> 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 5, 2011 at 9: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, 2012 at 2: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 7, 2013 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>
5
  • 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, 2009 at 16:48
  • Sure, I have edited the answer above and added the whole appender section.
    – Nuno G
    Feb 18, 2009 at 9: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, 2009 at 9: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<date>.
    – Nuno G
    Feb 19, 2009 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, 2009 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.

1

Use StaticLogFileName:

<param name="StaticLogFileName" value="true"/>
2
  • 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, 2009 at 20:38
  • Check RollingFileAppender section: logging.apache.org/log4net/release/config-examples.html
    – Mike Cole
    Feb 25, 2013 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.