Log4net have a FileAppender with date pattern - c#

I am trying to do something that I thought was simple.
To have a FileAppender in log4net with a date in the filename.
Before you rush to answer this, I need to make clear that I don't need a RollingFileAppender, because I know it's possible with that one.
I want to produce 1 log file every time I run my console app. So I don't need a rolling file appender, just 1 file each time, with a timestamp on it.
This is where it gets tricky because this configuration:
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="MyFileAppender" />
</root>
<appender name="MyFileAppender" type="log4net.Appender.FileAppender">
<file value="logs\" />
<datePattern value="dd.MM.yyyy'.log'" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
Gives this error:
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [datePattern] to se
t object on [log4net.Appender.FileAppender]
It works if I don't have a pattern and just have a static file that keeps getting overriden each time the app.
So is this not possible with a file appender?
I am using version 1.2.15.0.

that did it:
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="logs\App-%date{yyyy-MM-dd_HH-mm-ss}.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%2thread] %-5level - %message%newline" />
</layout>
</appender>

Related

See debug messages of log4net in immediate window

I have an application that uses log4net. I dump debug to a file as well as stdout.
When launching the application normally, I see all the messages in the output section as well as in the file.
If I create a class/ run a function that writes something to the log in the immediate window, I do not see anything in the Output nor in the Immediate window. I do see the log in the file though.
Is there any way I could fix it so I will be able to see these messages in the Immediate window?
Log4net configuration:
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="FileAppender"/>
<appender-ref ref="ConsoleAppender"/>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline"/>
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs/log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="1" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline"/>
</layout>
</appender>
</root>
</log4net>
Add a DebugAppender to your configuration in order to have messages appear in the Immediate window (I tested this on VS2013)
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="DebugAppender" />
</root>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline" />
</layout>
</appender>

Set log level for Gallio execution log

How do I configure log4net or my Gallio test runners (Icarus and Echo) to use a different logging level for the Gallio Execution Log and my own file appender. I want my log file to have a DEBUG log level and Gallio to have an ERROR log level.
I thought this would work but Gallio is reading both appenders:
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="File" type="log4net.Appender.FileAppender">
<file value="test.log" />
<appendToFile value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<logger name="TestNamespace">
<level value="DEBUG" />
<appender-ref ref="File" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="Console" />
</root>
</log4net>
Gallio is redirecting console output to its Execution Log. So I needed to set the threshold of the ConsoleAppender to the level I want for the Gallio Execution Log.
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="ERROR"/>
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="File" type="log4net.Appender.FileAppender">
<threshold value="DEBUG"/>
<file value="test.log" />
<appendToFile value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="Console" />
<appender-ref ref="File" />
</root>
</log4net>

Log4net generating wrong log file name

I want to create log files names with the following pattern:
SBRF_20120820.log
SBRF_20120821.log
SBRF_20120822.log
SBRF_20120823.log
In other words, create a new file for each day. So, I create the following configuration to do that:
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<file type="log4net.Util.PatternString" value="Logs/SBRF_%date{yyyyMMdd}.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
<logger name="LogEmArquivo">
<level value="INFO" />
<appender-ref ref="FileAppender" />
</logger>
</log4net>
When I run the program, today for example, the file SBRF_20120823.log will be created. But in the following days the log keeps to write in the SBRF_20120823.log file, and the files that are created are:
SBRF_20120823.log.2012-08-23
SBRF_20120823.log.2012-08-24
SBRF_20120823.log.2012-08-25
SBRF_20120823.log.2012-08-26
And if I run the program tomorrow, the files that will be created are:
SBRF_20120824.log.2012-08-24
SBRF_20120824.log.2012-08-25
SBRF_20120824.log.2012-08-26
SBRF_20120824.log.2012-08-27
Why?
You do not put the date pattern in the <file> - that is the static part of the filename. You need to put it into the <datePattern>.
Also, if you are using log4net 1.2.11, you can use <preserveLogFileNameExtension> which puts the datePattern on the current file also.
I think this is what you want your configuration to look like:
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<file type="log4net.Util.PatternString" value="Logs/SBRF_.log"/>
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<preserveLogFileNameExtension value="true"/>
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
<logger name="LogEmArquivo">
<level value="INFO" />
<appender-ref ref="FileAppender" />
</logger>
Remove the <rollingStyle value="Date" />.
Don't think you change this behaviour with the RollingFileAppender, so you would have to create your own appender.

How to read and display my log file info in application using Log4net

I am using Log4net in my application.
Can anyone please explain how to read my log files(Txt) and show it in my application.because my log files are there in another server.
What will be the config settings to read the log files from the server and show it in the C# application.
This is the code have used to write the logs.
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="\\10.8.8.87\temp\logs.log" />
<appendToFile value="true" />
<maximumFileSize value="10MB" />
<maxSizeRollBackups value="50" />
<rollingStyle value="Size" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%identity---- %date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
Instead of an rollingfile appender you can better use for example a sql appender to log your exceptions to a database and from your application you can query the database. On this page you can find some config examples:
http://logging.apache.org/log4net/release/config-examples.html

current date in app.config

Im trying to create a log file using log4net.
I have it create a log file. But i can't get the name of the log file to be the current date.
I have tried this:
<file value="log\\$date.txt"/>
But this just leans me with a file called "$date.txt" in the log folder.
I would like the file to be named "25-04-2012.txt"
Anyone know the little trick to make this work?
From the documentation, you should use a RollingFileAppender
This example show how to configure the RollingFileAppender to roll log
files on a date period. This example will roll the log file every
minute! To change the rolling period adjust the DatePattern value. For
example, a date pattern of "yyyyMMdd" will roll every day. See
System.Globalization.DateTimeFormatInfo for a list of available
patterns.
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd-HHmm" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
Documentation (search for rollingFileappender)
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\Akhila\logger\logger\bin\Debug\log-" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy-MM-dd" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>

Categories