Root log all level - c#

My root logger catches all levels of log except off. My level is set to INFO.
I searched if the level was changed somewhere, but I found nothing.
When I put the logger IBatisNet.DataMapper.Commands.DefaultPreparedCommand to DEBUG, the log is created in a text file by SpecificAppender. But, the log also appears in the console even if the root is set to INFO.
This is my code:
<?xml version="1.0" encoding="utf-8" ?>
<!-- This section contains the log4net configuration settings -->
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!-- Define some output appenders -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="d:/logs/vol/vol.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="500KB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,fff} [%t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<appender name="IBatisAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="d:/logs/vol/IBatis.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="3" />
<param name="MaximumFileSize" value="1000KB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,fff} [%t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,fff} [%t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<appender name="IBatisBufferingAppender" type="log4net.Appender.BufferingForwardingAppender" >
<bufferSize value="60"/>
<appender-ref ref="IBatisAppender" />
</appender>
<appender name="LogBufferingAppender" type="log4net.Appender.BufferingForwardingAppender" >
<bufferSize value="60"/>
<appender-ref ref="RollingLogFileAppender" />
</appender>
<appender name="SpecificAppender" type="log4net.Appender.RollingFileAppender">
<file value="d:/logs/vol/" />
<appendToFile value="true"/>
<datePattern value="Volu\me\trieSpeci\fic_yyyy-MM-dd.lo\g"/>
<rollingStyle value="Date"/>
<maxSizeRollBackups value="30"/>
<appendToFile value="true"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:SS,fff} [%t] %-5p %c{1} - %m%n" />
</layout>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="No -->"/>
<acceptOnMatch value ="true" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="Statement Id: [vol.Delete"/>
<acceptOnMatch value ="false" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="] Types: ["/>
<acceptOnMatch value ="false" />
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="Statement Id: [vol."/>
<acceptOnMatch value ="true" />
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogBufferingAppender" />
</root>
<logger name="PROGvolBatch.Services.CIService">
<level value="INFO" />
<appender-ref ref="SpecificAppender"/>
</logger>
<logger name="IBatisNet.DataMapper.Commands.DefaultPreparedCommand">
<level value="DEBUG" />
<appender-ref ref="SpecificAppender"/>
</logger>
<logger name="IBatisNet.DataMapper.Configuration.Cache.CacheModel">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
<logger name="IBatisNet.DataMapper.LazyLoadList">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
<logger name="IBatisNet.DataMapper.SqlMapSession">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
<logger name="IBatisNet.Common.Transaction.TransactionScope">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
<logger name="IBatisNet.DataAccess.DaoSession">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
<logger name="IBatisNet.DataAccess.Configuration.DaoProxy">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
<logger name="IBatisNet.DataMapper.Configuration.Statements.PreparedStatementFactory">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
<logger name="IBatisNet.DataMapper.Commands.IPreparedCommand">
<level value="OFF" />
<appender-ref ref="IBatisBufferingAppender" />
</logger>
</log4net>
I tried changing the level of the root to something else, but I always got the same result.

This appears to be the way log4net works. When your config file is processed it behaves as if it has been configured like:
<logger name="IBatisNet.DataMapper.Commands.DefaultPreparedCommand" additivity="false">
<level value="DEBUG" />
<appender-ref ref="SpecificAppender"/>
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogBufferingAppender" />
</logger>
Fortunately there is a workaround and that is to set a level filter on your appenders, e.g. for the ConsoleAppender:
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,fff} [%t] %-5p %c{1} - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<acceptOnMatch value="true" />
</filter>
</appender>
It might also be better to rename the appender to something more descriptive like InfoConsoleAppender. Also, if you want to be able to log some loggers to console at debug level, then you may need another un-filtered ConsoleAppender.

Related

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 Class Logger level per Appender-ref

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>

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

Two different FileAppender writing same logs message in two log files

I am getting a problem, when I configured two appenders and try to write a log message to one of the apender this also write the same message to another appenders file. Below is the configuration section,
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="ErrorLog" />
<appender-ref ref="PayWinSrvLog" />
</root>
<appender name="ErrorLog" type="log4net.Appender.FileAppender">
<param name="File" value="E:\Webhome\FimFolders\BankWinserviceService\Error_Log.txt" />
<param name="AppendToFile" value="true" />
<maxSizeRollBackups value="0" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="2MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<appender name="PayWinSrvLog" type="log4net.Appender.FileAppender">
<param name="File" value="E:\Webhome\FimFolders\BankWinserviceService\PayWinSrv_Log.txt" />
<param name="AppendToFile" value="true" />
<maxSizeRollBackups value="0" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="2MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
and this is the only code I am using in Start method of my windows service project.
protected override void Start()
{
ErrorLog = LogManager.GetLogger("ErrorLog");
PayWinSrvLog = LogManager.GetLogger("PayWinSrvLog");
XmlConfigurator.Configure();
ErrorLog.Error("Error Message."); // this also get printed in PayWinSrv_Log.txt
}
I also include the line
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
excuse me for my English.
You're getting confused with Appenders and Loggers. A log itself can have multiple appenders (e.g. you could have an SmtpAppender and a FileAppender for an error log, but you may want a separate FileAppender for normal messages). Since you only configure the root logger, using <root>..., then that is the only log that is ever returned using LogManager.GetLogger, which in your configuration has both FileAppenders.
To configure multiple, separate logs, use <logger tags, and then specify the appenders for each log:
<logger name="ErrorLog">
<level value="INFO" />
<appender-ref ref="ErrorLog" />
</logger>
<logger name="PayWinSrvLog">
<level value="INFO" />
<appender-ref ref="PayWinSrvLog" />
</logger>

I would like to create two log files - one with "DEBUG" another with "INFO" logs only

I have here this log4net config:
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\logfolder\logfile.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="20MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>
This is working well, but I would like to create another logfile with only "INFO" input. Is it possible to log in two different files, without changing anything in my C# code, just changing the config file?
Or lets just make the question simple: What do I have to add exactly to my config file?
And also, I have another issue: if I choose "INFO" instead of "DEBUG", I get INFO + ERROR logs. can I change this somehow, not to get ERROR logs, just INFO? Thank you.
You can configure second appender with filter specified, and add it to your root logger.
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="AnotherLogFileAppender" />
</root>
<appender name="LogFileAppender"
type="log4net.Appender.RollingFileAppender">
...
</appender>
<appender name="AnotherLogFileAppender"
type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\logfolder\anotherlogfile.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="20MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern"
value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
</log4net>
Also consider to use new style of log4net configuration, rather than old one with param tags:
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="AnotherLogFileAppender" />
</root>
<appender name="LogFileAppender"
type="log4net.Appender.RollingFileAppender">
<file value="D:\logfolder\logfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="20MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
<appender name="AnotherLogFileAppender"
type="log4net.Appender.RollingFileAppender">
<file value="D:\logfolder\anotherlogfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="20MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO"/>
</filter>
</appender>
</log4net>

Categories