Second Log4net logger not writing to unique file - c#

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.

Related

How to prevent duplicating event log message by setting app.config in log4net?

I set the app.config for log4net as below in order to log some message every changed status.
I input additivity="false" after googling, but the duplicated message is still logged.
Would you please advice me to correct the below config so that duplicated message can be disapprearing?
<appender name="APP_TRACE_ONCE_LOG_EVENTREF" type="log4net.Appender.RollingFileAppender" additivity="false">
<File value="C:\Edwards\HMI\Test\TraceOnce\"/>
<AppendToFile value="true"/>
<MaxSizeRollBackups value="100"/>
<DatePattern value="TraceOnce_dd.MM.yyyy.'log'"/>
<MaximumFileSize value="512KB"/>
<RollingStyle value="Composite"/>
<StaticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="[%-5p][%d]-[%m]%n"/>
</layout>
</appender>
<logger name="APP_TRACE_ONCE_LOG_CONFIG">
<level value="ALL"/>
<appender-ref ref="APP_TRACE_ONCE_LOG_EVENTREF"/>
</logger>

C# Log4Net Writing To Both Appenders

I am using C# to write an in house application and we've been trying to figure out a resolution to this issue for a couple of days now. We're using Log4Net v1.2.15.0 compiling against .Net v4.5.2. I have two rolling file appenders, EventsLogger and SitrepLogger which are both set to write to the network. I have confirmed that this does work as intended however when I call one appender (doesn't matter which) to write out, it writes to both EventsLogger and SitrepLogger files. Here is the relevant data from my app.config
<log4net>
<appender name="EventsLogger" type="log4net.Appender.RollingFileAppender">
<file name="File" value="\\TS-WXLF41\Project\Ambushed\${USERNAME}\EventsLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="1MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="StaticLogFileName" value="false"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="_MM-dd-yy.\tx\t" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%newline%date{HH:mm:ss tt} %message" />
</layout>
</appender>
<appender name="SitrepLogger" type="log4net.Appender.RollingFileAppender">
<file name="File" value="\\TS-WXLF41\Project\Ambushed\${USERNAME}\logs\sitrep" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="1MB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<param name="StaticLogFileName" value="false"/>
<param name="RollingStyle" value="Date"/>
<param name="DatePattern" value="_MM-dd-yy.\tx\t"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%newline%date{HH:mm:ss tt} %message" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="EventsLogger" />
<appender-ref ref="SitrepLogger" />
</root>
</log4net>
In my Program's entry point constructor I call log4net.Config.XmlConfigurator.Configure(); which I am pretty sure is working as intended. I do not call Configure anywhere else in the code except from there. In the members are of my entry point class I call private static readonly log4net.ILog logger = log4net.LogManager.GetLogger("SitrepLogger"); to fetch the logger that I need for my Program (entry point) class. In my other classes I use the EventsLogger so I call private static readonly log4net.ILog eventslogger = log4net.LogManager.GetLogger("EventsLogger"); in the members are of those classes. I do not call GetLogger more than once per class and I am sure I invoking the correct appender by calling eventslogger.Info()
This used to work fine (using multiple appenders to write to different files) for us but we've made many many changes since then, which we have tried reverting to with no success. No errors, no warnings and no messages on compilation. Thank you for everything in advance! :)
You are confusing appenders and loggers. Your application is creating loggers "SitrepLogger" and "EventsLogger". These both inherit the configuration of the root logger, and write to appenders with the same names "SitrepLogger" and "EventsLogger".
Try the following configuration, which I think will give you what you want:
<log4net>
<appender name="EventsAppender" ... >
...
</appender>
<appender name="SitrepAppender" ... >
...
</appender>
<root>
<level value="ALL" />
</root>
<logger name="EventsLogger" additivity="False">
<appender-ref ref="EventsAppender" />
</logger>
<logger name="SitrepLogger" additivity="False">
<appender-ref ref="SitrepAppender" />
</logger>
</log4net>

Remove log4net system properties from output

I want to use LogicalThreadContext to pass some context information in my WCF service. I need to pass different properties. In C# I has code
LogicalThreadContext.Properties["MyProperty"] = 1;
In log4net config I have
<log4net>
<appender name="RollingLogFileAppenderSize" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Log.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%2t] [%property] %level %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="RollingLogFileAppenderSize" />
</root>
</log4net>
And in log I got
2015-11-03 16:24:36,313 [10] [{MyProperty=1, log4net:Identity=, log4net:UserName=User, log4net:HostName=User}] INFO - Info
I don't want to have system properties log4net:Identity, log4net:UserName and log4net:HostName in log. How to do this? I can write config like this
conversionPattern value="%d [%2t] [%property{MyProperty}] %level %m%n"
But I have several properties in code and I want to see only properties that I added. Code
LogicalThreadContext.Properties.Remove("log4net:UserName");
doesn't work.
I found that it's posible to remove only log4net:HostName property with code
GlobalContext.Properties.Remove(LoggingEvent.HostNameProperty).
log4net:Identity and log4net:UserName cannot be removed because of CreateCompositeProperties method in log4net.Core.LoggingEvent class https://github.com/apache/log4net/blob/trunk/src/Core/LoggingEvent.cs.
It adds these properties without any conditions and so it's imposible to remove them for the last log4net version.
I had the same problem.
GlobalContext.Properties.Clear();
Worked for me.

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.

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