Log4Net Class Logger level per Appender-ref - c#

I am trying to separate out a Performance Logger into a separate log using Log4Net...
I setup my new Appender
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\ServiceLayer.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="_yyyy.MM.dd.lo\g" />
<maximumFileSize value="5MB" />
<maxSizeRollBackups value="10" />
<PreserveLogFileNameExtension value="true" />
<layout type="log4net.Layout.PatternLayout">
<!-- <conversionPattern value="%level %thread %logger - %message%newline" /> -->
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %5level [%thread][%property{bms.log.username}::%property{bms.log.businessRequest}(%property{bms.log.activityLogID})] - %message%newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="PerfFile" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\Perf.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="_yyyy.MM.dd-HH-mm.lo\g" />
<maximumFileSize value="5MB" />
<maxSizeRollBackups value="24" />
<PreserveLogFileNameExtension value="false" />
<layout type="log4net.Layout.PatternLayout">
<!-- <conversionPattern value="%level %thread %logger - %message%newline" /> -->
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %5level [%thread][%property{bms.log.username}::%property{bms.log.businessRequest}(%property{bms.log.activityLogID})] - %message%newline" />
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
The perf appender is the new appender I added...
I also set up a BufferForwardingAppender
<appender name="BMSDefaultAppender" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="128" />
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN" />
</evaluator>
<appender-ref ref="RollingFile" />
From there I set my Root
<root>
<level value="ERROR" />
<appender-ref ref="BMSDefaultAppender" />
</root>
Once I had this setup, I get ERRORs logged to my rolling log file, however I want to capture some info level data into my perf file...
<logger name="BMS.LTD.Activity.Workflow" additivity="false">
<level value="INFO" />
<appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Activity.Workflow.Rules" additivity="false">
<level value="WARN" />
<appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Persistence.DataSourceDataProviders" additivity="false">
<level value="INFO" />
<appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Persistence.DataSourceDataProviders.DataSourceDataCache" additivity="false">
<level value="WARN" />
<appender-ref ref="PerfFile" />
</logger>
I now get my Info and Warn in my Perf log but I also get my errors there too, is it possible to get my ERRORs in my main log and my info levels in my perf logs?

To control what log events get into each file, Add LevelRangeFilters to your appenders:
<!-- log error and above - add to the RollingFile appender config -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR"/>
<levelMax value="FATAL"/>
</filter>
and
<!-- log INFO and below - add to the PerfFile appender config -->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="INFO"/>
</filter>

Related

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 configuration for each project C#

I have 3 Class Lib projects as (ClassLib1.dll, ClassLib2.dll, ClassLib3.dll).
One Windows service project. WindowsService1.
Log for net is configure for Windows Service project and log4net is in config file as
<log4net>
<root>
<level value="all" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\{App_Name}\\{App_Name_Log_File_}" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
</log4net>
Windows service is referring calling 3 Class Lib projects.
How can I configure log4 net for these 3 class lib so that I can have a log file in the separate folder as below?
For Main windows service
C:\XX\Logs\Win_Servive\Win_Service_Log_File_.log
For Class Lib 1
C:\XX\Logs\ClassLib1\ClassLib1_Log_File_.log
For Class Lib 2
C:\XX\Logs\ClassLib2\ClassLib2_Log_File_.log
For Class Lib 3
C:\XX\Logs\ClassLib3\ClassLib3_Log_File_.log
You can achieve this by configuring three different appenders on the web.config file, then loading three different loggers by name:
<appender name="Class1Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
<appender name="Class2Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
<appender name="Class3Appender" type="log4net.Appender.RollingFileAppender" additivity="false">....</appender>
Then use the appropriate logger:
ILog class1Log = LogManager.GetLogger("Class1Appender");
ILog class2Log = LogManager.GetLogger("Class2Appender");
ILog class3Log = LogManager.GetLogger("Class3Appender");
Final log4net config as below
<log4net>
<root>
<level value="all" />
</root>
<logger name="WinService">
<appender-ref ref="WinService" />
</logger>
<logger name="ClassLibrary1">
<appender-ref ref="ClassLib1" />
</logger>
<logger name="ClassLibrary2">
<appender-ref ref="ClassLib2" />
</logger>
<logger name="ClassLibrary3">
<appender-ref ref="ClassLib3" />
</logger>
<appender name="WinService" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\WinService\\WinService_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
<appender name="ClassLib1" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\ClassLib1\\ClassLib1_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
<appender name="ClassLib2" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\ClassLib2\\ClassLib2_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>
<appender name="ClassLib3" type="log4net.Appender.RollingFileAppender" additivity="false">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="C:\\XX\\Logs\\ClassLib3\\ClassLib3_" />
<countDirection value="1" />
<datePattern value="yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss,fff} %-level [%thread] [%M %C] - %message%newline" />
</layout>
</appender>

Log4net not writing logs to specified path

I am working with log for net i have below LOg4Net.config file code.But It is not writing logs to defined path .Am i doing anything wrong.Shall i miss anything??
Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net debug="true">
<appender name="InfoRollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Temp\Logs\FileTransferService\Info.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2MB" />
<staticLogFileName value="true" />
<!--<threshold value="INFO" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"></levelMin>
<levelMax value="WARN"></levelMax>
</filter>
<filter class="log4net.Filter.DenyAllFilter"/>
</appender>
<appender name="ErrorRollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Temp\Logs\FileTransferService\Error.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="2MB" />
<staticLogFileName value="true" />
<!--<threshold value="ERROR" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR"></levelMin>
<levelMax value="FATAL"></levelMax>
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="InfoRollingLogFileAppender" />
<appender-ref ref="ErrorRollingLogFileAppender" />
<appender-ref ref="trace" />
</root>
<!--<logger name="CustomLogger">
<level value="ALL" />
<appender-ref ref="InfoRollingLogFileAppender" />
<appender-ref ref="ErrorRollingLogFileAppender" />
<appender-ref ref="trace" />
</logger>-->
</log4net>
</configuration>
Assembly Info Code:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
Output Directory :
Copy always

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);

Categories