Log4net generating wrong log file name - c#

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.

Related

Second Log4net logger not writing to unique file

I have an application that is using Anotar.Log4Net for logging. I cannot strip out the Anotar.Log4Net as other team members are using it. I would like to add my own logging file. I added an additional file appender to my app.config called BGDEV. I reviewed the post here to see how other's are accomplishing the task of having two file appenders. The desired behavior below is for each logger to log to it's own file. Everything is logging to MainLog.txt. Is this because there is something wrong with the app.config settings or because I have the anotar library installed?
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MainLog.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<appender name="BGDEV" type="log4net.Appender.RollingFileAppender">
<file value="BG_Log.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
</root>
<logger additivity="false" name="BGDev">
<level value="INFO"/>
<appender-ref ref="BGDev" />
</logger>
In code I create a logger and use it. Is there something wrong with my config file or is this becaise I am using
private static readonly ILog otherLog = LogManager.GetLogger("BGDEV");
otherLog.Info("TEST OTHER LOG");
You have a couple of issues with naming in your code and config. Names are case sensitive - you're defining a logger named BGDev, but you call GetLogger("BGDEV"), since no logger named BGDEV exists, it falls back to the root logger.
Your logger named BGDev also references an appender named BGDev, however again, no such appender exists, since you are defining it with an all-uppercase name BGDEV.
Finally, your BGDev logger needs to have a <layout>...</layout> section, or it won't be able to format log messages.
If you fix these issues, it should work as you expect.

Load only one appender instead of all

I'm currently working on a console C# app and I have to log two type of data :
All steps of the app in general (AppLog)
All information that I get from a service (MachineLog)
MachineLog needs 2 propeties, but when I start the app I do not define them I have some work to do before. But I need to log from start whith AppLog so at le beginning I use :
log4net.Config.XmlConfigurator.Configure();
And it create me the C:\DNC\Suivi\Logs.txt but also C:\DNC\Suivi\(null)\(null)-logs.txt
Is there any way to only load/configure AppLog and later MachineLog ?
I tried to add parameters to Configure() and all failed.
My App.config is :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="AppLog" />
</root>
<logger name="Machine" additivity="false">
<level value="ALL" />
<appender-ref ref="MachineLog" />
</logger>
<appender name="AppLog" type="log4net.Appender.RollingFileAppender">
<file value="C:\DNC\Suivi\Logs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="MachineLog" type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString" value="C:\DNC\Suivi\%property{Machine}\%property{ID}-logs.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
With your current configuration, you can't do it. As you have seen, log4net will create the log files when XmlConfigurator.Configure() is called.
In order to this, you will have to do at least some work programmatically, once you know where the file will be - it's quite easy to set the file name at runtime so you can have a placeholder file name in the config, then set the correct name at runtime when the values are known. This does mean that the empty placeholder file will exist - the only way around that would be to create the whole appender at runtime.

Block logging for assemblies having separate loggers in log4net

I have two log files written from my application, which have multiple assemblies in the following manner:
Assembly1 log -> MainLog.log
Assembly2 log -> MainLog.log
Assembly3 log -> MainLog.log
Assembly4 log -> MainLog.log
Assembly5 log -> SubLog.log
Assembly6 log -> SubLog.log
The log files get written correctly, but the problem here is, the SubLog entries get written into the MainLog file as well. I don't want this kind of logging duplication to happen. This is my log4net configuration.
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs//MainLog.txt" />
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<rollingStyle value="Size" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%M %date [%thread] %-5level %logger [%ndc] - %message %C, %F, %l, %L %M %newline" />
</layout>
</appender>
<appender name="NewForEveryRun" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="Logs//SubLog-%processid.txt"/>
<appendToFile value="true" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<logger name="Assembly5">
<level value="DEBUG"/>
<appender-ref ref="NewForEveryRun"/>
</logger>
<logger name="Assembly6">
<level value="DEBUG"/>
<appender-ref ref="NewForEveryRun"/>
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
For Assembly5 and Assembly6, I've specified the appender NewForEveryRun. In addition to this, I've kept the root, so that the rest of the assemblies are logged according to configuration specified in the RollingLogFileAppender appender. I guess, the root element is logging all the assemblies by default, even if I mention different logger for other assemblies. Is there any way to block the logging of Assembly5 and Assembly6 from root?
Got it! The loggers should look like this:
<logger name="Assembly5" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="NewForEveryRun"/>
</logger>
<logger name="Assembly6" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="NewForEveryRun"/>
</logger>
Setting additivity as false is the solution. No more duplicate logging :)

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

Configure log4net for asp.net MVC3 project

Ok, so I'm understood how to configure the log4Net in my application, But now
First I want to improve the configuration by differencing the level of the logs if the application it's a release or a debug, how can I do this?.
Second, If I had a folder in my project called LOG how can I set the configuration, to not used the physical folder of my application??
for example Instead of:
<file value="C:\physicalpath\LOG\Log.log" />
used
<file value="\LOG\Log.log" />
or
<file value="%some_variable%\LOG\Log.log" />
The documenation is straight forward:
file: the full or relative path to the log file.
So all you need to have is the full path like C:\physicalpath\LOG\Log.log or the ralative one, this needs to start with the dot char . like .\App_Data\Log4Net.Logs
you can also use the folder name in the file attribute, then you must use the datePattern attribute to specify the file name, for example:
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value=".\\App_Data\\Log4Net.Logs\\backend"/>
<datePattern value=".yyyy-MM-dd'.log'"/>
<appendToFile value="true"/>
<maximumFileSize value="256KB"/>
<maxSizeRollBackups value="2"/>
<rollingStyle value="Date"/>
<staticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline"/>
</layout>
</appender>
Also remember to add the
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
So you can avoid that log4net lock the file and you can't used it to append your messages.
If you're not used to log4net, don't forget to add the <root> node, this is the the one that let's log4net know what you want to use and not the <appender> nodes, for example, you can have 10 <appender> nodes and use only one, the <root> node is then only configured with the one you want to use...
here is a full configuration with 2 Mongo Appenders and 1 File Appender, the <root> specifies that only the file appender is in use:
<log4net>
<appender name="MongoAppender" type="log4net.Appender.MongoDBAppender, log4mongo-net">
<!-- MongoDB 1 connection options -->
<host value="staff.mongohq.com"/>
<port value="10077"/>
<databaseName value="myApp_2011"/>
<collectionName value="logs_net"/>
<userName value="myself"/>
<password value="123456"/>
</appender>
<appender name="MongoAppenderAppHarbor" type="log4net.Appender.MongoDBAppender, log4mongo-net">
<!-- MongoDB 2 connection options -->
<host value="staff.mongohq.com"/>
<port value="10048"/>
<databaseName value="d1741d63-46b1-4a44-9c49-8c28cecae36b"/>
<collectionName value="logs_net"/>
<userName value="myself"/>
<password value="123456"/>
</appender>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<!-- Local file options -->
<file value=".\\App_Data\\Log4Net.Logs\\backend"/>
<datePattern value=".yyyy-MM-dd'.log'"/>
<appendToFile value="true"/>
<maximumFileSize value="256KB"/>
<maxSizeRollBackups value="2"/>
<rollingStyle value="Date"/>
<staticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level %thread %logger - %message%newline"/>
</layout>
</appender>
<root>
<!--
<level value="DEBUG" />
<appender-ref ref="MongoAppender" />
<appender-ref ref="MongoAppenderAppHarbor" />
-->
<appender-ref ref="FileAppender"/>
</root>
</log4net>
Remember maybe you should to add in Global.asax.cs the follow code:
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
//...more code
}
Below is a sample app.config section, with the Path being defined through the <file value="somepath"/> tag.
This link is a good resource for documentation
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\SOMEPATH\\SOMELOG.log"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger – %message%exception%newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>

Categories