tags:

views:

2596

answers:

3

Hello,

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?

A: 

Use StaticLogFileName:

<param name="StaticLogFileName" value="true"/>
Bob Nadler
Please provide more details
Chris
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
+3  A: 

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>
Nuno G
Is there anything I need for the file param? The date still isn't being attached. Perhaps you could post the entire config?
Chris
Sure, I have edited the answer above and added the whole appender section.
Nuno G
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
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
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
I'm not going to mark my answer as accepted, that would be in bad taste.
Chris
+2  A: 

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.

Chris