log4net watching function does not work if create appender in code? - log4net

I want to set a Rolling Appender dynamic in code rather than in config file. So I have a function that will return log4net.ILog
<Assembly: log4net.config.XmlConfigurator(ConfigFile:="Log4Net.config", Watch:=True)>
Public Function genLogXMLCfg(cls As Object) As log4net.ILog
Dim clsName As String = cls.ToString()
Dim rollAppen As New RollingFileAppender()
rollAppen.Name = clsName + "Appender"
rollAppen.RollingStyle = RollingFileAppender.RollingMode.Composite
rollAppen.File = "logs\\"
rollAppen.DatePattern = "'" + clsName.ToLower() + "'" + "yyyyMMdd'.log'"
rollAppen.LockingModel = New log4net.Appender.FileAppender.MinimalLock()
rollAppen.AppendToFile = True
rollAppen.StaticLogFileName = False
rollAppen.MaxSizeRollBackups = 3
rollAppen.MaximumFileSize = "100KB"
Dim layout As New log4net.Layout.PatternLayout("%d [%t] %-5p %c (line:%L) - %m%n")
rollAppen.Layout = layout
layout.ActivateOptions()
rollAppen.ActivateOptions()
Dim logg As ILog = LogManager.GetLogger(clsName)
Dim l As log4net.Repository.Hierarchy.Logger = DirectCast(logg.Logger, log4net.Repository.Hierarchy.Logger)
l.AddAppender(rollAppen)
l.Repository.Configured = True
Return logg
End Function
Because I want to dynamic generate rolling appender from code but at the same time I want to set the logger from config file, so I have following config file call (Log4Net.config)
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="log4net.Config" value="log4net.config"/>
<add key="log4net.Config.Watch" value="True"/>
</appSettings>
<log4net>
<logger name="ConsoleApplication1.LogTest2">
<level value="WARN" />
</logger>
<logger name="ConsoleApplication1.testCls">
<level value="INFO" />
</logger>
</log4net>
Now I test the function by following code:
Dim cls As LogTest2 = New LogTest2()
Dim tCls As testCls = New testCls()
Dim l4n As log4net.ILog
Dim l4n2 As log4net.ILog
l4n = cls.genLogXMLCfg(tCls.GetType().ToString())
l4n2 = cls.genLogXMLCfg(cls.GetType().ToString())
l4n.Debug("... Here is a debug log -2.")
l4n.Info("... and an Info log.")
System.Threading.Thread.Sleep(3000)
l4n.Warn("... and a warning 1.")
l4n.Debug("... Here is a debug log -1.")
l4n.Warn("... and a warning 2.")
l4n.Warn("... and a warning 3.")
l4n2.Warn("l4n2 cls and a warning -1.")
l4n.Warn("... and a warning 4.")
l4n.Warn("... and a warning 5.")
Console.Write(" ... ... ... ")
System.Threading.Thread.Sleep(3000)
l4n.Warn("... and a warning 6.")
l4n.Debug("... Here is a debug log 1.")
l4n.Warn("... and a warning 7.")
l4n2.Debug("l4n2 cls Here is a debug log.")
l4n2.Info("l4n2 cls and an Info log.")
l4n.Fatal("... and a fatal .")
l4n2.Fatal("l4n2 and a fatal .")
l4n.Debug("... Here is a debug log 2.")
l4n.Warn("... and a warning 8.")
l4n.Error("... and an error.")
l4n.Debug("... Here is a debug log 3.")
l4n.Fatal("... and a fatal .")
l4n.Debug("... Here is a debug log 4.")
l4n2.Debug("l4n2 cls Here is a debug log.")
l4n2.Info("l4n2 cls and an Info log.")
l4n2.Warn("l4n2 cls and a warning.")
l4n2.Error("l4n2 cls and an error.")
l4n2.Fatal("l4n2 cls and a fatal .")
After that, two log file generated in logs folder call
**consoleapplication1.logtest220130610.log** and **consoleapplication1.testcls20130610.log**
The content are:
**consoleapplication1.logtest220130610.log**
2013-06-10 18:41:38,802 [9] WARN ConsoleApplication1.LogTest2 (line:182) - l4n2 cls and a warning -1.
2013-06-10 18:41:41,819 [9] FATAL ConsoleApplication1.LogTest2 (line:193) - l4n2 and a fatal .
2013-06-10 18:41:41,831 [9] WARN ConsoleApplication1.LogTest2 (line:206) - l4n2 cls and a warning.
2013-06-10 18:41:41,833 [9] ERROR ConsoleApplication1.LogTest2 (line:207) - l4n2 cls and an error.
2013-06-10 18:41:41,836 [9] FATAL ConsoleApplication1.LogTest2 (line:208) - l4n2 cls and a fatal .
**consoleapplication1.testcls20130610.log**
2013-06-10 18:41:35,767 [9] INFO ConsoleApplication1.testCls (line:176) - ... and an Info log.
2013-06-10 18:41:38,792 [9] WARN ConsoleApplication1.testCls (line:178) - ... and a warning 1.
2013-06-10 18:41:38,795 [9] WARN ConsoleApplication1.testCls (line:180) - ... and a warning 2.
2013-06-10 18:41:38,800 [9] WARN ConsoleApplication1.testCls (line:181) - ... and a warning 3.
2013-06-10 18:41:38,804 [9] WARN ConsoleApplication1.testCls (line:183) - ... and a warning 4.
2013-06-10 18:41:38,806 [9] WARN ConsoleApplication1.testCls (line:184) - ... and a warning 5.
2013-06-10 18:41:41,809 [9] WARN ConsoleApplication1.testCls (line:187) - ... and a warning 6.
2013-06-10 18:41:41,813 [9] WARN ConsoleApplication1.testCls (line:189) - ... and a warning 7.
2013-06-10 18:41:41,817 [9] FATAL ConsoleApplication1.testCls (line:192) - ... and a fatal .
2013-06-10 18:41:41,823 [9] WARN ConsoleApplication1.testCls (line:195) - ... and a warning 8.
2013-06-10 18:41:41,826 [9] ERROR ConsoleApplication1.testCls (line:196) - ... and an error.
2013-06-10 18:41:41,828 [9] FATAL ConsoleApplication1.testCls (line:199) - ... and a fatal .
Everything are OKAY. But....
If I change the config file during the program running, let say I change the level from INFO to FATAL
<logger name="ConsoleApplication1.testCls">
<level value="FATAL" /> <!-- INFO to FATAL -->
</logger>
the two log file now are:
**consoleapplication1.logtest220130610.log**
2013-06-10 19:05:20,880 [9] WARN ConsoleApplication1.LogTest2 (line:182) - l4n2 cls and a warning -1.
**consoleapplication1.testcls20130610.log**
2013-06-10 19:05:17,848 [9] INFO ConsoleApplication1.testCls (line:176) - ... and an Info log.
2013-06-10 19:05:20,872 [9] WARN ConsoleApplication1.testCls (line:178) - ... and a warning 1.
2013-06-10 19:05:20,875 [9] WARN ConsoleApplication1.testCls (line:180) - ... and a warning 2.
2013-06-10 19:05:20,877 [9] WARN ConsoleApplication1.testCls (line:181) - ... and a warning 3.
2013-06-10 19:05:20,884 [9] WARN ConsoleApplication1.testCls (line:183) - ... and a warning 4.
2013-06-10 19:05:20,886 [9] WARN ConsoleApplication1.testCls (line:184) - ... and a warning 5.
I wonder that why the watch function cannot update log properly and seem will affect other log file even I only change one logger data? May I know the reason? What wrong of my code

When you change your config file will your program is running. Log4net will reread you config file because of the Watch:=True. However your genLogXMLCfg is not executed. So your configuration of your appenders in not configured after you save the new config. There is no appender, so you do not see any new entries in your logs.

Related

ERROR: Caused by: Unknown XML node, expected "testExecutions" but got "testsuite" at line 1 by sonar run for NodeJs application

my sonar project template
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths = unit_test_results.xml
sonar.testExecutionReportPaths = unit_test_results.xml
my package.json includes
"test": "nyc --all --reporter=text --reporter=lcov _mocha --recursive --reporter=xunit --reporter-options output=unit_test_results.xml src/** controller/**",
The output file looks like
<testsuite name="Mocha Tests" tests="1840" failures="0" errors="2" skipped="8" timestamp="Mon, 18 Oct 2021 11:39:52 GMT" time="16.335">
<testcase classname="xxxxxxxxxx" name="xxxxxxxxx" time="0.011"/>
<testcase classname="yyyyyyyyyy" name="yyyyyyyyyyy" time="0.001"/>
</testsuite>
When sonar is running getting below error
2021-10-18T10:11:09.5161270Z 1 INFO: Parsing /runner/_work/yy/yy/unit_test_results.xml
2021-10-18T10:11:09.5162390Z 1 INFO: ------------------------------------------------------------------------
2021-10-18T10:11:09.5163293Z 1 INFO: EXECUTION FAILURE
2021-10-18T10:11:09.5164400Z 1 INFO: ------------------------------------------------------------------------
2021-10-18T10:11:09.5165097Z 1 INFO: Total time: 9.105s
2021-10-18T10:11:09.5165893Z 1 INFO: Final Memory: 18M/527M
2021-10-18T10:11:09.5166873Z 1 INFO: ------------------------------------------------------------------------
2021-10-18T10:11:09.9107082Z 1 ERROR: Error during SonarQube Scanner execution
2021-10-18T10:11:09.9113544Z 1 ERROR: Error during parsing of generic test execution report '/runner/_work/yy/yy/unit_test_results.xml'. Look at the SonarQube documentation to know the expected XML format.
2021-10-18T10:11:09.9118281Z 1 ERROR: Caused by: Unknown XML node, expected "testExecutions" but got "testsuite" at line 1
seems like i need to convert my unit_test_results.xml file to the below format. is that possible
<?xml version="1.0" encoding="UTF-8"?>
<testExecutions version="1">
<file path="<long path>/__tests__/api.test.js">
<testCase name="Feature when called with proper params then does somthing" duration="5"/>
<testCase name="Feature when called with wrong paramg then does nothing" duration="1"/>
</file>
</testExecutions>

Frequent reports of jgroups dropped messages during a performance test

Jgroup version : 3.4.4
Config :
UDP(mcast_addr=228.6.7.8;mcast_port=22222;ip_ttl=8;mcast_send_buf_size=150000;mcast_recv_buf_size=80000):PING(timeout=2000;num_initial_members=3):MERGE2(min_interval=5000;max_interval=10000):FD_SOCK:VERIFY_SUSPECT(timeout=1500):pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):UNICAST(timeout=5000):pbcast.STABLE(desired_avg_gossip=20000):FRAG(frag_size=4096;down_thread=false;up_thread=false):pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;shun=false;print_local_addr=false):pbcast.STATE_TRANSFER
During a performance test(four node JGroup cluster) frequent reports (WARN severity) of dropped / failed messages from jgroups.
These reports also occasionally occur during traffic (and even sometimes when idle).
WARN messages :
WARN [tid=bpsp-XYZ01-150615142241421-618331378-0-2] [bpsp-XYZ01-4296] org.jgroups.protocols.pbcast.NAKACK2 - JGRP000011: bpsp-XYZ01-4296: dropped message 330 from non-member bpsp-XYZ03-31792 (view=[bpsp-XYZ02-58715|3260] (15) [bpsp-XYZ02-58715, bpsp-XYZ04-33866, bpsp-XYZ04-35133, bpsp-XYZ01-1551, bpsp-XYZ02-17088, bpsp-XYZ02-2701, bpsp-XYZ02-50162, bpsp-XYZ02-19697, bpsp-XYZ01-8027, bpsp-XYZ01-32523, bpsp-XYZ01-4296, bpsp-XYZ01-10112, bpsp-XYZ04-10116, bpsp-XYZ04-48624, bpsp-XYZ04-16847])
WARN [tid=bpsp-XYZ01-150615142241421-618331378-0-1b] [bpsp-XYZ01-28987] org.jgroups.protocols.pbcast.NAKACK2 - JGRP000011: bpsp-XYZ01-28987: dropped message 1447 from non-member bpsp-XYZ03-54278 (view=[bpsp-XYZ02-33248|3071] (3) [bpsp-XYZ02-33248, bpsp-XYZ01-28987, bpsp-XYZ04-38112])
WARN [tid=bpsp-XYZ01-150615142241421-618331378-0-2a] [bpsp-XYZ01-46462] org.jgroups.protocols.pbcast.NAKACK2 - JGRP000011: bpsp-XYZ01-46462: dropped message 4146 from non-member bpsp-XYZ03-39195 (view=[bpsp-XYZ04-59688|3045] (3) [bpsp-XYZ04-59688, bpsp-XYZ01-46462, bpsp-XYZ02-14036])
WARN [tid=bpsp-XYZ01-150615142241421-618331378-0-5] [XYZ01-34208] org.jgroups.protocols.pbcast.GMS - bpsp-XYZ01-34208: failed to collect all ACKs (expected=3) for view [bpsp-XYZ03-20005|3047] after 2000ms, missing 1 ACKs from bpsp-XYZ02-33303
WARN [tid=bpsp-XYZ01-150615142241421-618331378-0-12] [bpsp-XYZ01-36922] org.jgroups.protocols.pbcast.NAKACK2 - JGRP000011: bpsp-XYZ01-36922: dropped message 541 from non-member bpsp-XYZ02-55864 (view=[bpsp-XYZ04-32626|3097] (3) [bpsp-XYZ04-32626, bpsp-XYZ01-36922, bpsp-XYZ03-58090])
Appreciate your idea on this.
Thanks

log4net log file get log with a lot of other information which we dint write

I am using log4net feature in my windows based .net application, which interact with different 3rd party com dll. so i could see the log file get log with a lot of other information which we dint write for logging.
so i just want know if any configuration i missed so that it could log only those information which we intent to log.
details
application : c# windows based 3.5 .net framework log4net version: 1.2
sample
2013-11-26 05:16:11,481 [15] INFO - TKS.DataDriver.DataDriver - LoadWorkBook : start LoadWorkBook operation
2013-11-26 05:16:13,653 [15] INFO - TKS.DataDriver.MsExcel.Common - inside method IsFileLocked
2013-11-26 05:16:19,028 [15] INFO - TKS.DataDriver.DataDriver - MergeTestCaseEntities : Merging all Test Case Entities.
2013-11-26 05:16:19,028 [15] INFO - TKS.DataDriver.DataDriver - LoadWorkBook : monitoringFlag: False MonitorQTPstatusOnOff: ON
2013-11-26 05:16:19,028 [15] INFO - TKS.DataDriver.DataDriver - LoadWorkBook : Asyncronus call for status check of the QTP.
2013-11-26 05:16:19,028 [15] INFO - TKS.DataDriver.DataDriver - DataLoadTestSet : DataLoad method call completed and result is : True
2013-11-26 05:16:19,028 [15] INFO - LogCatReplay - CProxy_IQTFuncLibMgrEvents::FireHelper Start. DispID = 4 Func=CProxy_IQTFuncLibMgrEvents<class CQTFuncLibMgr>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionengine\qtfunclibmgrcp.h Line=16 ThreadID=4636
2013-11-26 05:16:19,028 [15] INFO - LogCatReplay - UnitExecutionEngine: CProxy_IQTASUnitExecutionEngineEvents::FireHelper Start. DispID = 7 Func=CProxy_IQTASUnitExecutionEngineEvents<class CQTASUnitExecutionEngine>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionengine\qtasunitexecutionenginecp.h Line=15 ThreadID=4636
2013-11-26 05:16:19,028 [15] INFO - LogCatReplay - UnitExecutionEngine: CProxy_IQTASUnitExecutionEngineEvents::FireHelper End Func=CProxy_IQTASUnitExecutionEngineEvents<class CQTASUnitExecutionEngine>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionengine\qtasunitexecutionenginecp.h Line=39 ThreadID=4636
2013-11-26 05:16:19,043 [16] INFO - TKS.DataDriver.DataDriver - MonitorQTPstatus kick ON Now.
2013-11-26 05:16:19,059 [1] INFO - HP.QTP.QEEE.ExecutionMediator.Execution.ExecutionStatusResolver - Instance 0: Runtime status is updated to Done(micGeneral)
2013-11-26 05:16:19,059 [15] INFO - LogCatReplay - CProxy_IQTFuncLibMgrEvents::FireHelper Start. DispID = 5 Func=CProxy_IQTFuncLibMgrEvents<class CQTFuncLibMgr>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionengine\qtfunclibmgrcp.h Line=16 ThreadID=4636
2013-11-26 05:16:19,059 [15] INFO - LogCatReplay - UnitExecutionEngine: CProxy_IQTASUnitExecutionEngineEvents::FireHelper Start. DispID = 8 Func=CProxy_IQTASUnitExecutionEngineEvents<class CQTASUnitExecutionEngine>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionengine\qtasunitexecutionenginecp.h Line=15 ThreadID=4636
2013-11-26 05:16:19,059 [15] INFO - LogCatReplay - cCProxy_IQTTestExecutionEngineEvents::FireHelper Start. DispID = 16 Func=CProxy_IQTTestExecutionEngineEvents<class CQTTestExecutionEngine>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionengine\qttestexecutionenginecp.h Line=15 ThreadID=4636
2013-11-26 05:16:19,059 [1] INFO - LogCatReplay - ExecutionSite: CProxy_IQTTestExecutionEngineEvents::FireHelper Start. DispID = 16 Func=CProxy_IQTTestExecutionEngineEvents<class CQTTestExecutionSite>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionsite\qttestexecutionsitecp.h Line=17 ThreadID=880
2013-11-26 05:16:19,059 [1] INFO - LogCatReplay - ExecutionSite: CProxy_IQTTestExecutionEngineEvents::FireHelper End Func=CProxy_IQTTestExecutionEngineEvents<class CQTTestExecutionSite>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionsite\qttestexecutionsitecp.h Line=41 ThreadID=880
2013-11-26 05:16:19,059 [15] INFO - LogCatReplay - ExecutionEngine: CProxy_IQTTestExecutionEngineEvents::FireHelper End Func=CProxy_IQTTestExecutionEngineEvents<class CQTTestExecutionEngine>::FireHelper File=e:\ft\qtp\win32_release\11.52.467.0_clean\qtp\backend\executionengine\app\qtexecutionengine\qttestexecutionenginecp.h Line=52 ThreadID=4636
If you are using the standard method of declaring the logger, eg
internal static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
then in your log4net configuration file, you can disable logging except for your own code based on the namespaces of your classes.
For example, if you only wanted to log the lines from TKS.DataDriver, then your config file could contain something like
<root>
<level value="OFF" />
<appender-ref ref="RollingFileAppender" />
</root>
<logger name="TKS.DataDriver">
<level value="INFO" />
</logger>
You can add other namespaces if you need them or just use "TKS" etc.

Log4J - disable logging for specific class - multiple appenders

I setup Log4J to use multiple appenders. This is working fine. However, I want to set the default log level to one appender to DEBUG, and restrict logging for a specific class to INFO and above. What I came up with works for the FA appender, but not the DCSLOAD and DCSPROC appenders. What am I doing wrong?
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger = CA, FA
# CA is set to be a ConsoleAppender.
log4j.appender.CA=org.apache.log4j.ConsoleAppender
# CA uses PatternLayout.
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{3} %x - %m%n
# FA uses PatternLayout.
log4j.appender.FA=org.apache.log4j.RollingFileAppender
log4j.appender.FA.File=${catalina.base}/logs/dcs.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{3} %x - %m%n
log4j.appender.FA.MaxFileSize=1024KB
log4j.appender.FA.MaxBackupIndex=100
# DCSPROC, DCSLOAD and DCSDB are all File Appenders to different targets
log4j.appender.DCSPROC=org.apache.log4j.RollingFileAppender
log4j.appender.DCSPROC.File=${catalina.base}/logs/dcs_process.log
log4j.appender.DCSPROC.layout=org.apache.log4j.PatternLayout
log4j.appender.DCSPROC.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{3} %x - %m%n
log4j.appender.DCSPROC.MaxFileSize=1024KB
log4j.appender.DCSPROC.MaxBackupIndex=100
log4j.appender.DCSLOAD=org.apache.log4j.RollingFileAppender
log4j.appender.DCSLOAD.File=${catalina.base}/logs/dcs_business.log
log4j.appender.DCSLOAD.layout=org.apache.log4j.PatternLayout
log4j.appender.DCSLOAD.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{3} %x - %m%n
log4j.appender.DCSLOAD.MaxFileSize=1024KB
log4j.appender.DCSLOAD.MaxBackupIndex=100
log4j.appender.DCSDB=org.apache.log4j.RollingFileAppender
log4j.appender.DCSDB.File=${catalina.base}/logs/dcs_database.log
log4j.appender.DCSDB.layout=org.apache.log4j.PatternLayout
log4j.appender.DCSDB.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{3} %x - %m%n
log4j.appender.DCSDB.MaxFileSize=1024KB
log4j.appender.DCSDB.MaxBackupIndex=100
# set default logging level for outputs
log4j.logger.CA = DEBUG
log4j.logger.FA = DEBUG
log4j.logger.DCSDB = DEBUG, DCSDB
log4j.logger.DCSPROC = DEBUG, DCSPROC
log4j.logger.DCSLOAD = DEBUG, DCSLOAD
log4j.additivity.DCSDB=false
log4j.additivity.DCSPROC=false
log4j.additivity.DCSLOAD=false
#This works
log4j.logger.LoginServlet=INFO, FA
#These two do not
log4j.logger.LoginServlet=INFO, DCSLOAD
log4j.logger.LoginServlet=INFO, DCSPROC
change theseļ¼š
log4j.logger.DCSDB = DEBUG, DCSDB
log4j.logger.DCSPROC = DEBUG, DCSPROC
log4j.logger.DCSLOAD = DEBUG, DCSLOAD
to
log4j.logger.DCSDB = DEBUG
log4j.logger.DCSPROC = DEBUG
log4j.logger.DCSLOAD = DEBUG

How to log a message when the BufferingForwardingAppender is flushed?

I have a BufferingForwardingAppender configured to forward the last 10 messages to a RollingFileAppender when an ERROR occurs. Can I get a custom message to appear in the log before each batch of messages?
I want to indicate that this is a new ERROR event and its context, so that the log is readable?
My log4net configuration is kinda like this:
<appender name="ErrorBufferingAppender" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="10" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR" />
</evaluator>
<appender-ref ref="ErrorFileAppender" />
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="logs\Errors.txt"/>
<!-- other important parameters -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-4thread] %-5level %logger{1} - %message%newline"/>
</layout>
</appender>
And my desired output is kinda like this
---------- ERROR ----------
2011-05-09 16:59:12,327 [8 ] INFO AppLogger - DiskStorageMonitor.StartMonitoring()
2011-05-09 16:59:12,331 [8 ] INFO AppLogger - DiskStorageMonitor.Initializing File System Watcher...
2011-05-09 16:59:12,341 [8 ] INFO AppLogger - DiskStorageMonitor - LocalSlideDataStorageDrive: C:
2011-05-09 16:59:12,370 [8 ] INFO AppLogger - Transition: From 'MinimumDiskSpaceQuotaState' to 'IdleState'.
2011-05-09 16:59:26,697 [8 ] INFO AppLogger - Transition: From 'IdleState' to 'SlideHolderMacroState'.
2011-05-09 16:59:26,702 [8 ] INFO AppLogger - Transition: From 'SlideHolderMacroState' to 'WaitingForMacroCmdResponseState'.
2011-05-09 16:59:26,781 [12 ] INFO YStageManager - Moving to: -1.25
2011-05-09 16:59:26,782 [14 ] INFO XStageManager - Moving to: -142
2011-05-09 16:59:30,800 [12 ] ERROR RecoveringErrorHandler - An error occurred. Retrying... (snipped exception message & stacktrace)
---------- ERROR ----------
2011-05-09 16:59:30,808 [12 ] WARN MacroCameraRecoverySteps - An error occurred while calling the macro camera. Resetting and retrying...
2011-05-09 16:59:30,809 [12 ] INFO MacroCameraManager - Resetting the Macro Camera.
2011-05-09 16:59:30,886 [12 ] ERROR GetMacroImageCommandHandler - Macro image processing failed for the slide in position 0. (snipped exception message & stacktrace)
I've got a solution that will probably work for you but it is a bit of a work-around. Basically, set two appenders to write to the same file. When you write an error (which will trigger the Buffer to clear to your log file), have the other appender write the "-------Error--------" text first with a newline before the text.
You will have to play around with it a bit to get it to work, but this should be a workable solution. Apache does allow multiple appenders to write to the same file. Just make sure you put the +MinimalLock on your appenders so they don't lock the file. Here is a SO link that explains how two appenders can write to the same file:
Can Log4net have multiple appenders write to the same file?
Thanks to BiggsTRC for the great answers. This is my working log4net configuration and sample output.
This appender responds to any ERROR message by writing the context "header" in the log. The log is minimally locked to allow this and the buffering appender to write to the file. The ignoresException tag set to false actually tells log4net that this layout will write the exception (rather than it writing automatically below the message). But really I don't want it to, that's coming in the other appender.
<appender name="ErrorSectionAppender" type="log4net.Appender.FileAppender">
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<file value="logs\Errors.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<ignoresException value="false" />
<conversionPattern value="%newline---------- ERROR # %date ----------%newline" />
</layout>
</appender>
This appender buffers all messages until an ERROR message appears, then it dumps the last 10 (bufferSize) messages and the error message to the ErrorFileAppender.
<appender name="ErrorBufferingAppender" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="10" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR" />
</evaluator>
<appender-ref ref="ErrorFileAppender" />
</appender>
This is the actual error file appender. The conversion pattern is standard. Notice the minimal file lock here as well.
<appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\Errors.txt"/>
<appendToFile value="true"/>
<staticLogFileName value="true"/>
<rollingStyle value="Size"/>
<maxFileSize value="1048576"/>
<maxSizeRollBackups value="-1"/>
<countDirection value="1"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-4thread] %-5level %logger{1} - %message%newline"/>
</layout>
</appender>
The output of these appenders looks like this
---------- ERROR # 2011-05-13 13:51:22,632 ----------
2011-05-13 13:51:04,937 [9 ] INFO OmnyxScannerLogger - Transition: From 'No State' to 'CommunicationSyncState'.
2011-05-13 13:51:05,363 [12 ] DEBUG IMacroImageProcessor - hello, 10
2011-05-13 13:51:05,369 [19 ] INFO XStageManager - Connecting
2011-05-13 13:51:05,369 [12 ] INFO ZStageManager - Connecting
2011-05-13 13:51:05,369 [16 ] INFO YStageManager - Connecting
2011-05-13 13:51:05,393 [9 ] INFO OmnyxScannerLogger - Transition: From 'CommunicationSyncState' to 'SystemInitializeState'.
2011-05-13 13:51:08,528 [17 ] INFO YStageManager - Homing
2011-05-13 13:51:08,539 [12 ] INFO ZStageManager - Homing
2011-05-13 13:51:22,632 [14 ] ERROR RecoveringErrorHandler - An error occurred. Retrying...
Omnyx.Scanner.CliContracts.ScannerException: Injected: The stage failed to home.
at Omnyx.Scanner.Simulation.ErrorInjection.ErrorInjectionInterceptor.Intercept(IInvocation invocation) in D:\Simulation\ErrorInjection\ErrorInjectionInterceptor.cs:line 42
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IXStageManagerProxy.Home()
---------- ERROR # 2011-05-13 13:51:33,758 ----------
2011-05-13 13:51:29,034 [9 ] INFO OmnyxScannerLogger - DiskStorageMonitor.StartMonitoring()
2011-05-13 13:51:29,037 [9 ] INFO OmnyxScannerLogger - DiskStorageMonitor.Initializing File System Watcher...
2011-05-13 13:51:29,046 [9 ] INFO OmnyxScannerLogger - DiskStorageMonitor - LocalSlideDataStorageDrive: C:
2011-05-13 13:51:29,062 [9 ] INFO OmnyxScannerLogger - -- LocalSlideDataStorageOverhead: 0
2011-05-13 13:51:29,075 [9 ] INFO OmnyxScannerLogger - Transition: From 'MinimumDiskSpaceQuotaState' to 'IdleState'.
2011-05-13 13:51:31,727 [9 ] INFO OmnyxScannerLogger - Transition: From 'IdleState' to 'SlideHolderMacroState'.
2011-05-13 13:51:31,732 [9 ] INFO OmnyxScannerLogger - Transition: From 'SlideHolderMacroState' to 'WaitingForMacroCmdResponseState'.
2011-05-13 13:51:31,792 [12 ] INFO YStageManager - Moving to: -1.25
2011-05-13 13:51:31,793 [20 ] INFO XStageManager - Moving to: -142
2011-05-13 13:51:33,758 [12 ] ERROR RecoveringErrorHandler - An error occurred. Retrying...
Omnyx.Scanner.CliContracts.ScannerException: Injected: The macro camera failed.
at Omnyx.Scanner.Simulation.ErrorInjection.ErrorInjectionInterceptor.Intercept(IInvocation invocation) in D:\Simulation\ErrorInjection\ErrorInjectionInterceptor.cs:line 42
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IMacroCameraManagerProxy.TakeTopLitAndBackLitPictures()

Resources