Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append current Date to Log file with Log4Net

Tags:

config

log4net

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?

like image 894
Chris Avatar asked Feb 10 '09 19:02

Chris


People also ask

What is rolling file Appender in log4net?

RollingFileAppender can roll log files based on size or date or both depending on the setting of the RollingStyle property. When set to Size the log file will be rolled once its size exceeds the MaximumFileSize.

What is Maxsizerollbackups in log4net?

Gets or sets the maximum number of backup files that are kept before the oldest is erased.

Where is log file log4net?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.


1 Answers

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.

like image 165
Muhammad Rizwan Avatar answered Oct 12 '22 14:10

Muhammad Rizwan