Logging to two different log files with log4net - log4net

I am attempting to log error messages as well as debug messages to two different files. I am using two different file appenders to do this.
Here is my app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="DebugFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="DebugLog.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date %level %logger - %message %newline" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="ErrorLog.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date %level %logger %type %exception %line %location - %message %newline" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="DebugFileAppender" />
</root>
<logger name="ErrorFileAppender" >
<level value="ERROR"/>
<appender-ref ref="ErrorFileAppender"/>
</logger>
The debug messages work fine but when I alter the program to contain an exception error, the error is not written to the file.
Here is the code I use to catch the exception:
catch (Exception e)
{
logger.Error(e);
}

Modify the root to contain the following:
<root>
<appender-ref ref="ErrorFileAppender"/>
<appender-ref ref="DebugFileAppender" />
</root>
moved from comment for clarity

Related

log4Net filtering for multiple outputs confusion

Trying to understand the log4net filtering.
After reading the docs some aspects are still elusive to me.
For example I have this config file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start" />
<footer value="Service Logging -- End" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="WindowsService1.exe" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
<!-- Specify the level for specific categories (“namespace.class”)-->
<logger name="WindowsService1.MyMonitor">
<level value="WARN" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
My expectations were:
The RollingFileAppender has a range of [INFO:FATAL] The Root has a
Level of DEBUG so it would let DEBUG and "greater" pass. Which
means all of RollingFileAppender's range would go through.
The LogFileAppender has no range specified so it would use Root's of DEBUG or greater.
And the EventLogAppender would only send event messages of WARN or greater to the Windows Event System - and - they would only
be messages from the class "WindowsService1.MyMonitor".
However it appears that all the messaging gets filtered to the WARN level!
Including my rolling log files and the log file "log-file.txt"
This seems like it should be simple - don't know why I'm so confused by it.
Hope someone can enlighten me.
Please feel free to use crayons and pictures because I think I need them.
Thanks.
UPDATE
In my Program.cs I have:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
And in each class where logging is done:
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
UPDATE 2
At a different computer now. Had to recreate everything and used log4net debugging to assist.
But still can't get the desired output.
Basically want the WARN and above messages to go to one file and the Event logger. And those messages would be a subset of all the INFO and above messages.
The INFO and above messages - superset - would go into the rolling appender and overwrite themselves eventually. But the warnings and errors would always be maintained.
So here is a rework of all the files for a simple service. If I set WARN on the appender-ref ref=EventLogAppender then all the INFOs are suppressed.
Program.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
// Ref: http://www.c-sharpcorner.com/UploadFile/naresh.avari/develop-and-install-a-windows-service-in-C-Sharp/
//
// use 'sc' to install/delete the service
// run 'sc' as administrator
// sc create "MySillyService" binpath= "C:\whatever\something.exe"
// sc delete "MySillyService"
namespace SimpleService1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
Service1.cs
namespace SimpleService1
{
public partial class Service1 : ServiceBase
{
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private Timer timer1;
private int counter = 0;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
timer1.Interval = 4000;
timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
timer1.Enabled = true;
log.Info("my service is started");
}
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
counter++;
if (counter%2 == 0)
{
log.Info(String.Format("Counter({0:D}) is now even", counter));
}
else
{
log.Error(String.Format("Counter({0:D}) is now even", counter));
}
}
protected override void OnStop()
{
timer1.Enabled = false;
log.Info("my service was stopped");
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<!--
Ref: http://mitch-wheat.blogspot.com/2007/04/log4net-net-logging-tool.html
I keep this config file in a separate directory and copy it with a
post-build event:
copy /Y $(SolutionDir)Logger\Log4Net.config $(TargetDir)Log4Net.config
-->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start - " />
<footer value="Service Logging -- End - " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<applicationName value="Service1" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
<!-- Specify the level for specific categories (�namespace.class�)-->
<logger name="SimpleService1.Service1">
<level value="WARN" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
Thanks for your help
Finally figured out the nuances to get this working as desired. Here's the config for anyone interested.
<?xml version="1.0" encoding="utf-8" ?>
<!--
I keep this config file in a separate directory and copy it with a
post-build event:
copy /Y $(SolutionDir)Logger\Log4Net.config $(TargetDir)Log4Net.config
-->
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\CurrentLog" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %-22.22c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<header value="Service Logging -- Start - " />
<footer value="Service Logging -- End - " />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc]
<%property{auth}> - %message%newline" />
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
<!-- Put whatever name you want to display in the event log
for 'applicationName value=XX'
-->
<applicationName value="ServiceSkippy" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger
[%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- put namespace of service application in following name -->
<logger name="ServiceWithLog4Net">
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="EventLogAppender" />
</logger>
</log4net>
-Skippy

Problems with Log4net multiple logger to multiple files

log4net version: 1.2.13.0
Runtime Version: v4.0.30319
I've got some Problems with using multiple Loggers and Appenders.
I have a root Logger with 3 Appenders which work well but when im configuring multiple other loggers log4net seems to only register the appenders from the last logger specified in the app.config.
If i put TaskSyncLogger as last logger entry TaskSyncAppender gets recognized
If i put CalendarSyncLogger as last logger entry CalendarSyncLogger gets recognized
only the 3 appenders from the root logger and the appender from the last logger entry is listed in hierarchy.GetAppenders()
Hierarchy hierarchy = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
var appenders = hierarchy.GetAppenders();
my app.config
<appender name="MainFileAppender" type="log4net.Appender.FileAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger- %message%newline %exception" />
</layout>
<file value="Logs\logs.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline %exception" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline %exception" />
</layout>
</appender>
<appender name="TaskSyncAppender" type="log4net.Appender.FileAppender">
<appendToFile value="true"/>
<file value="Logs\logsTask.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger- %message%newline %exception" />
</layout>
<!--
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
<filter type="log4net.Filter.DenyAllFilter" />
</filter>
-->
</appender>
<appender name="CalendarSyncAppender" type="log4net.Appender.FileAppender">
<appendToFile value="true"/>
<file value="Logs\logsCalendar.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger- %message%newline %exception" />
</layout>
<!--
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
<filter type="log4net.Filter.DenyAllFilter" />
</filter>
-->
</appender>
<root>
<level value="All" />
<appender-ref ref="MainFileAppender" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<logger Name="CalendarSyncLogger" additivity="true">
<level value="All"/>
<appender-ref ref="CalendarSyncAppender"/>
</logger>
<logger Name="TaskSyncLogger" additivity="true">
<level value="All"/>
<appender-ref ref="TaskSyncAppender"/>
</logger>
further the TaskSyncAppender isn't writing anything the File gets created but it remains empty although the the MainFile Appender is writing the output.
im getting it like
public const string TaskSyncLogger = "TaskSyncLogger";
private static readonly ILog logTask = LogManager.GetLogger(LoggingStrings.TaskSyncLogger);
I can reproduce this, and while I cannot be confident in saying it is definitely a bug, it is certainly unexpected.
You can work around it by adding this last logger to your file:
<logger Name="WillNotBeUsed">
<level value="Off"/>
<appender-ref ref="CalendarSyncAppender"/>
<appender-ref ref="TaskSyncAppender"/>
</logger>
After that all five appenders are loaded.

log4net logs header and footer twice

This is my configuration:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="Logs\StubLog.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]
" />
<param name="Footer" value="[Footer]
" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\n" />
<param name="Footer" value="[Footer]\n\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
</root>
<logger name="NHibernate">
<level value="WARN" />
</logger>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
And here is the log:
[Header]
[Footer]
[Header]
2012-10-04 14:22:01,203 [1] INFO Stub.Program Enter application
2012-10-04 14:22:01,328 [1] DEBUG Stub.FrmMain Test
2012-10-04 14:22:03,265 [1] INFO Stub.Program Exit application
[Footer]
Where does the first pair of Header / Footer coming from? Can I eliminate them?
Putting XmlConfigurator.Configure() at more than one places may result in having more than one header and footer pair.
XmlConfigurator.Configure() should be defined only at one place in the solution. Better approach is to put it in AssemblyInfo.cs

How to log different log level to different log appender in log4net

I have a WinForm that it display log information but I want it to display to only INFO level but I want the log in the log file to contain DEBUG level as well.
Below is my configuration:
<?xml version="1.0" encoding="utf-8" ?>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="${ALLUSERSPROFILE}\\Application Data\\logs\\Gateway" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<param name="StaticLogFileName" value="false" />
<datePattern value=".yyyyMMdd.lo\g" />
<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 [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="DEBUG" />
<backColor value="Blue" />
</mapping>
<mapping>
<level value="INFO" />
<backColor value="Green" />
</mapping>
<mapping>
<level value="WARN" />
<backColor value="Yellow" />
</mapping>
<mapping>
<level value="ERROR" />
<backColor value="Red" />
</mapping>
<mapping>
<level value="FATAL" />
<backColor value="Red, HighIntensity" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RichTextBoxAppender" type="Gateway.RichTextBoxAppender,Gateway" >
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n"/>
<param name="Footer" value="[Footer]\r\n"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
<mapping>
<level value="DEBUG" />
<textColorName value="DarkGreen" />
</mapping>
<mapping>
<level value="INFO" />
<textColorName value="ControlText" />
</mapping>
<mapping>
<level value="WARN" />
<textColorName value="Blue" />
</mapping>
<mapping>
<level value="ERROR" />
<textColorName value="Red" />
<bold value="true" />
<pointSize value="10" />
</mapping>
<mapping>
<level value="FATAL" />
<textColorName value="Black" />
<backColorName value="Red" />
<bold value="true" />
<pointSize value="12" />
<fontFamilyName value="Lucida Console" />
</mapping>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="WARN" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
<appender-ref ref="RichTextBoxAppender" />
</root>
<logger name ="Gateway" additivity="false">
<level value="INFO" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
<appender-ref ref="RichTextBoxAppender" />
</logger>
</log4net>
Right now it is set to INFO for both LogFileAppender and RichTextBoxAppender. I tried taking out the LogFileAppender in Gateway logger and move to another logger and set to "DEBUG" level for this new logger but it is still doing one or the other. How do I configure it so that the LogFileAppender has DEBUG level and RichTextBoxAppender has INFO level?
You should be able to add a filter to both of your appenders.
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
This way one appender will only log to a certain level while the other to a different level even though they are defined by the same logger.
I agree with #gwhitake that you can use the level range filter. I also want to add, however, that you can use the LevelMatch filter if you just want to select one level. This filter allows you to add another filter at the end of it as well so that you could create a filter that had two or more levels even if they aren't next to each other in the order.
For example, the following filter will capture only DEBUG and ERROR messages (just as an example):
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG"/>
</filter>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
The one key here is that we need to have the DenyAllFilter at the end. This tells the logger that if it reaches this line, don't log it. That way it will log DEBUG messages, ERROR messages, and nothing else.
There are a lot of ways to manipulate the filter to get exactly what you want. Here is an article (full disclaimer: I wrote it) that shows you how to do a number of different things with the filter besides just level range:
http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx

Why does log4net only log errors if my <root> level is INFO?

I obviously don't understand log4net. My root log level is configured as follows:
<root>
<level value="ERROR"/>
<appender-ref ref="FileAppender" />
</root>
My log initialization looks as follows:
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
And my actual logging call looks as follows:
LOG.Error("Error submitting Registration.", exc);
Here's the funny part. This log statement ONLY works if I set the root log level to INFO. Why does the log level ERROR not work (and I only want errors, not Info, Debug etc...)?
<root>
<level value="ALL" />
...
</root>
Someone of my log4net-Configfile:
<appender name="FileAppender_FullLog" type="log4net.Appender.FileAppender">
<file value="Log_Full.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
</layout>
</appender>
<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
<file value="Log.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
..
</appender>
<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
<level value="ALL" />
<appender-ref ref="FileAppender_FullLog" />
<appender-ref ref="FileAppender_SmallLog" />
<appender-ref ref="SmtpAppender" />
</root>
Edit:
<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
<file value="Log.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
<level value="ALL" />
<appender-ref ref="FileAppender_SmallLog" />
</root>
The better way ist to use "< filter type="log4net.Filter.LevelRangeFilter">..." because you can change this setting for every appender if you want this in feature.
With the help of our clever architect I figured this one out...
I used to have my log initialization in assemblyInfo.cs (but this was wrong).
I moved the log initialization to my global.asax in app_startup:
protected void Application_Start(Object sender, EventArgs e)
{
string configFilePath = Server.MapPath("~/Config/Log4Net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
}
The server.MapPath bit is very important, otherwise log4net tries to read its configuration from the system32\inetsrv folder.
I think my previous logging worked from time to time due to the order in which the assemblies were loaded, or something like that. Either way, this solution works.

Resources