default log4net configuration - c#

I have the following code:
public Log4NetLogger()
: base(LogLevel.Debug)
{
if(!LogManager.GetRepository().Configured)
{
lock(mLocker)
{
if(!LogManager.GetRepository().Configured)
{
XmlConfigurator.Configure();
}
}
}
mLog4NetInstance = LogManager.GetLogger(typeof(Log4NetLogger));
}
With the following web.config:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\MaMLog_" />
<appendToFile value="true" />
<StaticLogFileAppName value="false" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMMdd_dddd'.log'" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd MMM yyyy HH:mm:ss,fff} [%thread] -() %message%newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<logger name="MyLog1">
<level value="Debug" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
<logger name="MyLog2">
<level value="Debug" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
How does the code know the logger name? If at all?

Since you are using
mLog4NetInstance = LogManager.GetLogger(typeof(Log4NetLogger));
then the logger name will be the value of typeof(Log4NetLogger).FullName which is usually of the form YourNameSpace.Log4NetLogger
Nothing will be logged in this example, because you don't have a logger with that name and you don't have a root section to fall back to , eg
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>

Related

Can I send one log message to a particular appender?

I've got a logger which is using a file appender and a console appender. I've got one line in my code that I only want to go to the console appeander. Is there a way to do that without making a while new logger?
Alternatively, I could just make another logger that only used the console, but I can't seem to even get that done. All examples I can find use class or method names. I am trying to use a name I provided in the app.config.
Here is my appconfig:
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%level [%thread] %date - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="All" />
</filter>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="D:\Something\Log\IntegrationTests\IntegrationTests.log" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maximumFileSize value="250MB" />
<maxSizeRollBackups value="20" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level [%thread] %date - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="All" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
<logger name="consoleOnly">
<level value="ALL" />
<appender-ref ref="Console" />
</logger>
</log4net>
Here is a line where I try to get "consoleOnly" Logger.
ILog log = LogManager.GetLogger("consoleOnly");
log.Debug("Poop");
Is that how I do it?
ILog log = LogManager.GetLogger("consoleOnly");
log.Debug("Poop");
This should work fine. but I think you need to set additivity to false, otherwise it would also log using the root logger.
eg.
<logger name="consoleOnly" additivity="false">
<level value="ALL" />
<appender-ref ref="Console" />
</logger>

Log4net - Logging to wrong appender

I'm trying to have 2 log files, with my application logging to either by specifying a logger name in code.
The problem is it never logs to the file I'm trying to target, instead it always logs to the last declared appender in the config (LogB in this case).
I have my config set up as follows:
<log4net>
<logger name="LogA">
<appender-ref ref="LogA"/>
<level value="DEBUG"/>
</logger>
<logger name="LogB">
<appender-ref ref="LogB"/>
<level value="DEBUG"/>
</logger>
<appender name="LogA" type="log4net.Appender.FileAppender">
<file value="E:\Logs\LogA.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<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="LogB" type="log4net.Appender.FileAppender">
<file value="E:\Logs\LogB.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogA" />
</root>
<root>
<level value="DEBUG" />
<appender-ref ref="LogB" />
</root>
</log4net>
Then in my code when I create a logger:
private static readonly ILog logA = LogManager.GetLogger("LogA");
log.Debug("This should log to LogA");
And in some other class:
private static readonly ILog logB = LogManager.GetLogger("LogB");
log.Debug("This should log to LogB");
But nothing is ever logged to the LogA file, it's always output to the LogB file (or whatever happens to appear last in the config).
What is it I'm missing?
For some reason it didn't appear to like the multiple root nodes. When I used this setup instead (keeping the appender nodes), it worked:
<root>
<level value="ALL" />
<appender-ref ref="SomeRootAppender" />
</root>
<logger additivity="false" name="LogA">
<level value="DEBUG"/>
<appender-ref ref="LogA" />
</logger>
<logger additivity="false" name="LogB">
<level value="DEBUG"/>
<appender-ref ref="LogB" />
</logger>

log4net filtering by <logger> not working

I would like to filter out certain log entries. In my config file I've tried adding the following to my appender with no luck:
<logger name="NHibernate">
<level value="ERROR" />
</logger>
<logger name="NHibernate.SQL">
<level value="ERROR" />
</logger>
<logger name="Castle">
<level value="ERROR" />
</logger>
<filter type="log4net.Filter.LoggerMatchFilter">
<loggerToMatch value="/*repeat for all the above*/" />
<acceptOnMatch value="false" />
</filter>
Here's the whole file, where the two <logger/> sections are is where I tried putting the rest of my filter attempts as well. Only concerned about the RollingFileAppender right now.
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\log\auto.log" />
<appendToFile value="true" />
<maximumFileSize value="1000KB" />
<maxSizeRollBackups value="10" />
<logger name="NHibernate" additivity="false">
<level value="OFF" />
</logger>
<logger name="NHibernate.SQL" additivity="false">
<level value="OFF" />
</logger>
<layout type="log4net.Layout.PatternLayout">
<footer value ="
"/>
<conversionPattern value="%date %level - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
You need to move the logger configuration outside the appenders: loggers reference which appenders to use, but are not part of the appender configuration. Also you should remove the additivity=false as that's redundant.
Womp, I don't know how log4net works.
All those <logger/> tags go right after the opening <log4net> tag like so
<log4net>
<logger name="NHibernate" >
<level value="ERROR" />
</logger>
<logger name="NHibernate.SQL">
<level value="ERROR" />
</logger>
...

Use log4net with separate config file

my AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
i change the structure just like you said
(i just change the <file value="..\Logs\\CurrentLog" /> to <file value="D:\Log\Log.txt" />):
<?xml version="1.0" encoding="UTF-8"?>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\Log\Log.txt" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
and define the log:
private static readonly ILog Log = LogManager.GetLogger("Logger");
but still the log.txt file doesn't created yet!
There's something missing in your config file.
<log4net>
<logger name="default">
<level value="ALL"/>
</logger>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\\CurrentLog" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
And also the declaration:
private static readonly ILog Log = LogManager.GetLogger("default");
Actually, you haven't declared a logger in your config and it has wrong structure.
This is how it should look like:
<?xml version="1.0" encoding="UTF-8"?>
<log4net>
<logger name="Logger">
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</logger>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\\CurrentLog" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
Now you can access it this way:
private static readonly ILog Log = LogManager.GetLogger("Logger");
It definitely works.
Now about your code snippet. I'm not sure about your initialization. I haven't worked with log4net this way:
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
However, you can try declaring logger in root and initialize it:
<?xml version="1.0" encoding="UTF-8"?>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="RollingFileAppender"/>
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="..\Logs\\CurrentLog" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
// ...
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

How to configure multiple logging configurations using log4net?

I am using the following app.config settings for multiple log files. The following are my app.config settings. But when I use "LogFileAppender" settings to write log to a file, it also writing the logs to the Console
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<File value="C:\File1.log" />
<AppendToFile value="true" />
<rollongStyle value="Composite" />
<maximumFileSize value="200MB" />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="%date %messge%newline"/>
</layout>
</appender>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<File value="C:\File2.log" />
<AppendToFile value="true" />
<rollongStyle value="Date" />
<maximumFileSize value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="%date %messge%newline"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="INFO" />
</filter>
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="FATAL" />
</filter>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value ="ERROR"/>
<foreColor value ="Red"/>
</mapping>
<mapping>
<level value ="WARN"/>
<foreColor value ="Yellow"/>
</mapping>
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="%date %messge%newline"/>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="RollingFile" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>
In the C# code, I am using the below method calls.
private static readonly log4net.ILog ilogger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log4net.Config.XmlConfigurator.Configure();
Question:
How can I restrict the Console logging while using the LogFileAppender using the above app.config file?
Add a ForwardingAppender in your configuration between the root logger and the ColoredConsoleAppender, so that you get this configuration
root -> LogFileAppender
root -> RollingFile
root -> ForwardingAppender -> ColoredConsoleAppender
Then in your ForwardingAppender add a filter that will only let pass events that do not originate from the application you don't want; a LoggerMatchFilter would be a good idea since you can exclude loggers originating from the namespace of the desired application.
I found the solution by modifying the app.config as following.
<logger name = "FileLogger" additivity="false">
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</logger>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
In the C# code, call the LogFileAppender explicitly as below
private static readonly log4net.ILog ilogger = log4net.LogManager.GetLogger("FileLogger");

Categories