First of all, I have seen a lot of answers and tips in others topics (most similar: Log4Net: Multiple loggers), but there is no applicable answer.
I want to have 2 loggers with different file appenders and restrict each to write into root logger. It is Console app. Whole code below:
using System;
using System.Diagnostics;
using System.Linq;
using log4net;
namespace Test_log4net
{
class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger("Async");
logger.Info("started async");
Console.WriteLine("Logger: {0}", (logger as log4net.Core.LogImpl).Logger.Name);
Console.WriteLine("Appenders: {0}", string.Join(", ", (logger as log4net.Core.LogImpl).Logger.Repository.GetAppenders().ToList().Select(appendr => appendr.Name)));
ILog logger2 = LogManager.GetLogger("Sync");
logger2.Info("started sync"); //changed: from logger -> to logger2 on 10/21/2014
Console.WriteLine("Logger: {0}", (logger2 as log4net.Core.LogImpl).Logger.Name);
Console.WriteLine("Appenders: {0}", string.Join(", ", (logger2 as log4net.Core.LogImpl).Logger.Repository.GetAppenders().ToList().Select(appendr => appendr.Name)));
Console.ReadKey();
}
}
}
And App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<root>
<level value="All"/>
</root>
<appender name="FileInfoAppenderA" type="log4net.Appender.RollingFileAppender">
<file value="D:\\temp\\AsyncTest.log"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="100MB"/>
<staticLogFileName value="true"/>
<datePattern value="yyyyMMdd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d - %m%n"/>
</layout>
</appender>
<appender name="FileInfoAppenderS" type="log4net.Appender.RollingFileAppender">
<file value="D:\\temp\\SyncTest.log"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="100MB"/>
<staticLogFileName value="true"/>
<datePattern value="yyyyMMdd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d - %m%n"/>
</layout>
</appender>
<logger Name="Sync" additivity="false">
<level value="INFO"/>
<appender-ref ref="FileInfoAppenderS"/>
</logger>
<logger Name="Async" additivity="false">
<level value="INFO"/>
<appender-ref ref="FileInfoAppenderA"/>
</logger>
</log4net>
</configuration>
And console output:
Logger: Async
Appenders: FileInfoAppenderA
Logger: Sync
Appenders: FileInfoAppenderA
Files have been created, but both of them are empty.
When I specify appenders in root, like:
<root>
<level value="All"/>
<appender-ref ref="FileInfoAppenderA"/>
<appender-ref ref="FileInfoAppenderS"/>
</root>
then, console:
log4net:ERROR [RollingFileAppender] Attempted to append to closed appender named [FileInfoAppenderS]
Logger: Async
Appenders: FileInfoAppenderA, FileInfoAppenderS
Logger: Sync
Appenders: FileInfoAppenderA, FileInfoAppenderS
And only in AsyncTest.log:
2014-04-11 17:26:58,142 - started async
2014-04-11 17:26:58,151 - started sync
What I am doing wrong?
**UPD (10/21/2014):** With latest log4net available via Nuget I have following console output:
Logger: Async
Appenders: FileInfoAppenderA
Logger: Sync
Appenders: FileInfoAppenderA
And both of files (AsyncTest.log, SyncTest.log) are empty.
**UPD (08/4/2015):** Solution is to use lower case when setting the attributes for everything in log4net section. So, I just should have changed following lines in app.config:
......
<logger name="Sync" additivity="false">
......
<logger name="Async" additivity="false">
......
Note the difference: attribute 'name' is in lower case.