How can I log the events in log files based on program per execution and date format? - log4net

I am using following log4net.config file:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value= " D:\Logs_Event\log_event.txt"/>
<staticLogFilename value = "false"/>
<appendToFile value="true"/>
<datepattern value = "yyyy-MM-dd"/>
<rollingStyle value="once"/>
<layout type = "log4net.Layout.PatternLayout">
<conversionPattern
value="%date [%thread] %-5level %logger ==> %message%newline"/>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
When the application starts, I have to log all events in text files till the application ends. If the application starts next time the events will get logged into another text file with the respective date format. How can I achieve this?

You can achieve this using the PatternString option provided in log4Net.
<file type="log4net.Util.PatternString"
value="LogFileName_%date{dd_MM_y(hh:mm:ss)}.log" />
check here for the date conversion pattern and here for Date time formatter.
related question: https://stackoverflow.com/a/10349613/5395773

Related

What's wrong with my log4net config file

I have a config file with two appenders, one file appender and one database appender. I want to log everything to the file appender, and only log exceptions to the database appender. When setting up both appenders in the section it logs fine but all log events are sent to both appenders, which is not what i want.
I changed the configuration but with this current configuration, exceptions get logged to the database, and nothing is getting written to the file appender. Can anyone tell me why I am not getting anything written to the file appender?
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Log4net\Workflow\TestLog.txt" />
<threshold value="All" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p {%logger} %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<!-- Removed to keep this snippet simple-->
</appender>
<root>
<level value="Error" />
<appender-ref ref="AdoNetAppender" />
</root>
<logger name="AllLogs">
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
What you have here is the following:
all logs events with the level Error or more will go to the AdoNetAppender
all logs events originating from a logger whose name is based on AllLogs will go to the RollingLogFileAppender
From what I understand you want all logs to default to the file, and only error ones to go also to the database. Then simply add both appenders to your root logger so that both get all events, and add filters to only let filters you're interested in pass through: a level range filter on your database appender would work
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!-- rest of config snipped to save space -->
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<!-- rest of config snipped to save space -->
</appender>
<root>
<appender-ref ref="AdoNetAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
Of course if you don't want any duplicate just filter errors and above in the rolling file appender

log4Net - Loggers between classes - Releasing a File

I am new to log4net. Here is my app.config. What is happening is at a point after going through a switch statement, my logger "X" stops writing to my log file. I tried x.Logger.Repository.Shutdown();, and all that does is stop my "Y" from even logging. My suspicion is that the file is in use, and can't be written to (the next logging statement does begin in a different class. Again, I'm new. Basically, it writes about 10 lines, goes to another class with a logger of the same name, works in that class for a few more lines, and then when it goes back to the original class, it stops logging. I'm a little lost. P.S. I have the correct section name, and I am calling my log as such:
private static readonly ILog eventLog = LogManager.GetLogger("EventLog");
<appender type="log4net.Appender.FileAppender" name="event">
<file value="C:X.log" />
<layout type="log4net.Layout.PatternLayout">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender type="log4net.Appender.FileAppender" name="cLog">
<file value="Y.log" />
<layout type="log4net.Layout.PatternLayout">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name="EventLog">
<level value="INFO" />
<maximumfilesize value="256KB" />
<param value="INFO" name="Threshold" />
<appender-ref ref="event" />
</logger>
<logger name="crLog">
<level value="INFO" />
<maximumfilesize value="256KB" />
<param value="INFO" name="Threshold" />
<appender-ref ref="cLog" />
</logger>
As it turns out, when you call x.Logger.Repository.Shutdown(); it kills any logs that you might want to write to at that point in time.
I will either move x.Logger.Repository.Shutdown(), or find a different method for releasing my files so the last portion of my app can use (email) the file.

How to append rolling index BEFORE file extension in log filename

I am using the RollingLogFileAppender and it works great and it's rolling over into new files. But it's adding the .1, .2, etc at the very end of the file. So I end up with .log.1, .log.2, etc. So every file technically has a new extension that explorer doesn't know, so I can't just double click on a file to open.
How can I get the rolling file appender to insert that index BEFORE the file extension?
What I want is
.1.log
.2.log
Bonus would be for the current file to always be .0.log, that way they always sort correctly in explorer.
EDIT: added my current config settings
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="App_Data\\Logs\\" />
<datePattern value="dd.MM.yyyy'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<logger name="File">
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
You just need to use the PreserveLogFileNameExtension property on the RollingFileAppender.
See the following questions:
Log4net appender filename issue
Log4net RollingFileAppender Size rollingStyle file extension

Log4net - Create new log file each time application is started

Can we create separate log files each time when the application is run? If i run my application 2 times, I should get 2 separate log files, hopefully the file names can be appended with the created dateTime
eg:
log_0830 - when application is run on 8:30 am
log_2130 - when application is run on 9:30 pm
Put that into your app.config:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="log_"/>
<rollingStyle value="Date"/>
<datePattern value="HHmm.\tx\t"/>
<staticLogFileName value="false"/>
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %message%newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="file"/>
</root>
</log4net>
That configuration would produce filenames:
log_0830.txt - when application is run on 8:30 am
log_2130.txt - when application is run on 9:30 pm
I believe you could do this in the configuration like so:
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="log-file-%d [%t].txt" />
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>

What is the preferred way to log the conversionPattern value used by the log4net?

My question may seem a little strange - I wish to log the conversionPattern used to format the messages logged with log4net when certain appenders are used.
For instance, if my log4net section looks like this:
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<appender name="MainAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file type="log4net.Util.PatternString" value="${NCLOGS}\NC.Entities.Test${CI_TAG}.%processid.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<MaximumFileSize value="10MB" />
<StaticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="MainAppender" />
</root>
</log4net>
Then I would like to see %date [%thread] %-5level %logger - %message%newline in the log file.
Rationale I am using Log4View to view our logs and it demands the conversion pattern in order to be able to format the messages. I find it convenient if the conversion pattern is found in the log itself, this spares me the need to look for the respective config file and copy it from there.
Right now I am doing using some XPATH magic, but I am wondering if there is a better solution, probably using some log4net API.
Thanks.
One way to do it would be to add the pattern as a property when you start up the application. You could read the pattern from the config file or there might be a way to get the pattern for each of the appenders programatically, in which case you can add one property for each appender and then have each pattern for each appender log the property.
Something like this to add the property:
log4net.GlobalContext.Properties["PatternForAppender"]= getLoggerPattern();
And something like this to include it in the log:
<conversionPattern value="%logger (%property{PatternForAppender}) [%level] - %message%newline" />

Resources