Quartz scheduler logs via log4net coming in wrong log file - log4net

I have two projects: one (MyApp.QuartzImplementation) where all initialization and quartz job related working is done while another project (MyApp.XYZApi) do other stuff.
I have separate log4net logs files for each project. The problem I am having is that the scheduler related logs are coming in my main project (MyApp.XYZApi) logs though I have initialized the scheduler from another project (MyApp.QuartzImplementation).
Sample logs which are coming in Main project log files are
2018-05-10 17:01:06.034 INFO TANG_DM N1 16 StdSchedulerFactory.Log Quartz scheduler 'DBScheduler' initialized
2018-05-10 17:01:06.050 INFO TANG_DM N1 16 StdSchedulerFactory.Log Quartz scheduler version: 3.0.4.0
2018-05-10 17:01:06.087 DEBUG TANG_DM N1 16 SimpleSemaphore.Log Lock 'TRIGGER_ACCESS' is desired by: 75af30b2-9658-4b61-b2ca-6bf3d74f6486
2018-05-10 17:01:06.087 DEBUG TANG_DM N1 16 SimpleSemaphore.Log Lock 'TRIGGER_ACCESS' is being obtained: 75af30b2-9658-4b61-b2ca-6bf3d74f6486
2018-05-10 17:01:06.103 DEBUG TANG_DM N1 16 SimpleSemaphore.Log Lock 'TRIGGER_ACCESS' given to: 75af30b2-9658-4b61-b2ca-6bf3d74f6486
2018-05-10 17:01:06.119 DEBUG TANG_DM N1 15 DeviceDataManager.StartOtpExpiryJob Sleeping For 1 Hours
2018-05-10 17:01:06.188 DEBUG TANG_DM N1 16 SQL.Log Prepared SQL: UPDATE QRTZ_TRIGGERS SET TRIGGER_STATE = #newState WHERE SCHED_NAME = 'DBScheduler' AND (TRIGGER_STATE = #oldState1 OR TRIGGER_STATE = #oldState2)
2018-05-10 17:01:06.266 DEBUG TANG_DM N1 16 SQL.Log Prepared SQL: UPDATE QRTZ_TRIGGERS SET TRIGGER_STATE = #newState WHERE SCHED_NAME = 'DBScheduler' AND (TRIGGER_STATE = #oldState1 OR TRIGGER_STATE = #oldState2)
2018-05-10 17:01:06.272 INFO TANG_DM N1 16 JobStoreTX.Log Freed 0 triggers from 'acquired' / 'blocked' state.
2018
My log4net config looks like following
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="MyAppXYZApi" type="log4net.Appender.RollingFileAppender" >
<file value="..\\Logs\\XYZ_DM_N1" />
<encoding value="utf-8" />
<appendToFile value="true" />
<datePattern value="_yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="10MB"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %p XYZ_DM N1 %t %property{ExternalIP} %property{ContextId} %property{ContextValue} %c{1}.%M %property{SessionId} %m%n" />
</layout>
</appender>
<appender name="QuartzScheduling" type="log4net.Appender.RollingFileAppender" >
<file value="..\\Logs\\XYZ_QuartzScheduling_N1" />
<encoding value="utf-8" />
<appendToFile value="true" />
<datePattern value="_yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="10MB"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %p XYZ_QuartzScheduling N1 %t %property{ExternalIP} %property{ContextId} %property{ContextValue} %c{1}.%M %property{SessionId} %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="MyAppXYZApi" />
</root>
<logger additivity="false" name="MyApp.XYZApi">
<level value="ALL"/>
<appender-ref ref="MyAppXYZApi" />
</logger>
<logger additivity="false" name="MyApp.QuartzImplementation">
<level value="ALL"/>
<appender-ref ref="QuartzScheduling" />
</logger>
</log4net>
Please let me know if i can do any things for scheduler logs of quartz to be written to particular log file
When i have added %logger as suggested following are the quartz namespaces in the logs
Quartz.Util.DBConnectionManager
Quartz.Impl.StdSchedulerFactory
Quartz.Simpl.TaskSchedulingThreadPool
Quartz.Core.SchedulerSignalerImpl
Quartz.Impl.AdoJobStore.StdAdoDelegate
Quartz.Core.QuartzScheduler
Quartz.Impl.AdoJobStore.SimpleSemaphore
Quartz.Impl.AdoJobStore.MisfireHandler
Quartz.Core.QuartzSchedulerThread
Quartz.SQL
Quartz.Impl.AdoJobStore.JobStoreTX
Can i some how put all of them in one single appender

It looks like you need to change
<logger additivity="false" name="MyApp.QuartzImplementation">
to
<logger additivity="false" name="Quartz">

Related

Configuring Log4Net for specific classes and overreaching error handling

I am using Log4Net within my project, and would like to configure it so that debug/info messages go to files specific to each class, and all error messages go to a central file.
Within my application, I have several "workers". I would like each worker to have its own log file to capture debug messages. If I get a debug message that isn't captured on a specific worker, I would like it to be logged to a central debug file. I would also like to have central error logging, where all errors from any of the workers are logged in the same file.
I am having difficulty figuring out the XML configuration to do this.
Here is the closest I have come:
<log4net>
<appender name="DebugLog" type="log4net.Appender.RollingFileAppender">
<file value="logs\debug.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="ddMMyyyy" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
<file value="logs\error.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="ddMMyyyy" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline%stacktrace{1}%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<appender name="Worker1" type="log4net.Appender.RollingFileAppender">
<file value="logs\Worker1.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="ddMMyyyy" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="Worker2" type="log4net.Appender.RollingFileAppender">
<file value="logs\Worker2.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="ddMMyyyy" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="Worker3" type="log4net.Appender.RollingFileAppender">
<file value="logs\3.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="ddMMyyyy" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="DebugLog" />
<appender-ref ref="ErrorLog" />
</root>
<logger name="Worker1">
<level value='DEBUG'/>
<appender-ref ref="Worker1" />
</logger>
<logger name="Worker2">
<level value='DEBUG'/>
<appender-ref ref="Worker2" />
</logger>
<logger name="Worker3">
<level value='DEBUG'/>
<appender-ref ref="Worker3" />
</logger>
</log4net>
The first issue I have is that all messages written to Worker1's class, are written to the Debug.log file, and the Worker1.log file. I don't want them written to Debug.log if they are already written to Worker1.log.
I can get around this by changing the section, by adding additivity = false. In this case, it writes to the Worker1.log, and not the Debug.log file.
However, doing this prevents errors from being written to Error.log
What happens is that if I set additivity = "false" and log an error, it gets written to Worker1.log, but does not get written to Error.log.
Is there any way to have the additivity apply to just the debug/info messages, so that I can have Debug messages in Worker1.log, Error messages in Error.log, and any debug messages that aren't specific to a logger in the debug.log file?
You are right about setting additivity="false" to isolate the debug messages to Worker1.log.
To get the error messages of Worker1 back into Error.log you also have to include the ErrorLog appender in the Worker1 logger declaration, as shown below.
<logger name="Worker1" additivity="false">
<level value='DEBUG'/>
<appender-ref ref="Worker1" />
<appender-ref ref="ErrorLog" />
</logger>
Via this setup you overrule the rules as defined in root to use a custom Worker1 Appender for debug messages and the same Error Appenderfor error messages.

windsor castle - log4net - RollingFileAppender - Issue - Information is written twice in the file

Issue: All the information that is logged is written twice in the file.
Please find the below configuration that I use:
I could observe the issue only when multiple loggers were introduced.
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="MemoryAppender"/>
<appender-ref ref="EventsFileAppender" />
</root>
<logger name="FileLogger">
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="MemoryAppender"/>
</logger>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\log-file.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="FileLogger" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline" />
</layout>
</appender>
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="FileLogger" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<logger name="EventsLogger">
<level value="DEBUG" />
<appender-ref ref="EventsFileAppender" />
</logger>
<appender name="EventsFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\ObjectServerEvents\ObjectServerEvents-file.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<staticLogFileName value="true" />
<filter type="log4net.Filter.LoggerMatchFilter">
<LoggerToMatch value="EventsLogger" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level - %message%newline" />
</layout>
</appender>
Can you spot the mistake in this? How to resolve this?
You are adding the appenders twice: once to the root element, and once to each logger.
Remove the appender declarations from either the root element or the loggers, and each event will only be logged once.
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="MemoryAppender"/>
<appender-ref ref="EventsFileAppender" />
</root>
<logger name="FileLogger">
<level value="DEBUG" />
</logger> <!-- etc -->

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

log4net ForwardingAppender threshold

I need to configure logger to write messages to several appenders with different Log Levels.
For ex. I want RefreshDataService to log detailed info to TransportFileAppender and Warnings and Errors to RollingFileAppender.
Also, I'm using buffering to improve logging performance.
I setup FilteredFileLog appender to forward Warnings to FileLog->RollingFileAppender.
But for RefreshDataService logger, all log messages including debug and Info getting logged to FileLog.
Can you help me to configure the RefreshDataService logger to log only Warn+ messages to FileLog? Thanks.
Below is excerpt from my log4net config.
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="${TEMP}\logs\client\${ENV}\client.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="4096KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level [%property{ENV}] [%property{UID}] %logger - %message%newline" />
</layout>
</appender>
<appender name="TransportAppender" type="log4net.Appender.RollingFileAppender">
<file value="${TEMP}\logs\client\${ENV}\client.comm.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="4096KB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="DetailedTransportLog" type="log4net.Appender.BufferingForwardingAppender">
<threshold value="DEBUG" />
<bufferSize value="200" />
<appender-ref ref="TransportAppender" />
</appender>
<appender name="FileLog" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="20" />
<lossy value="false" />
<appender-ref ref="RollingFileAppender" />
</appender>
<appender name="FilteredFileLog" type="log4net.Appender.ForwardingAppender">
<threshold value="WARN" />
<appender-ref ref="FileLog" />
</appender>
<logger name="RefreshDataService" >
<level value="DEBUG" />
<appender-ref ref="FilteredFileLog" />
<appender-ref ref="DetailedTransportLog" />
</logger>
You can use a level range filter on the appender:
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="FATAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
My understanding is that the Threshold property is used tell the appender to flush the buffer when messages of a certain level are logged. This way you can make for instance sure that errors are written immediately to the database.

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