Header and footer coming two times more in log4net logs

与世无争的帅哥 提交于 2019-12-07 14:54:37

问题


Below is the Section of log4net from app.config

   <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,     log4net" />
   </configSections>      
   <log4net debug="true">
     <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="${TMP}\SRG\Logs\Log_%env{USERNAME}_%date{yyyyMMdd}.log" />
      <appendToFile value="true" />
      <bufferSize value="20" />
      <LockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
      <header type="log4net.Util.PatternString" value="[Log Starts]%newline" />        
      <footer type="log4net.Util.PatternString" value="[Log Ends]%newline" />        
      <conversionPattern value="%date [%username] - %message%newline" />
      </layout>
     </appender>  

     <logger name="SRGApplicationDebugLog">
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
     </logger>
   </log4net>  

Problem 1: I am getting header and footer two times extra whenever my application is started, but I need to avoid it.

[Log Starts]
[Log Ends]
[Log Starts]
[Log Ends]
[Log Starts]
2012-11-08 12:25:03,376 [username] - Application started
[Log Ends]

Problem 2: I am not getting from where is the two empty header footer pair is coming.

  1. I am creating logger like below:
_debugLogger = LogManager.GetLogger("SRGApplicationDebugLog");    
XmlConfigurator.Configure(); 
  1. To use Logger:
_debugLogger.DebugFormat(logMessage);    
  1. I have added this line explicitly in AssemblyInfo.cs for log4net
[assembly: XmlConfigurator(Watch = true)]    

Question: Want to fix the problems in bold


回答1:


You don't need

 XmlConfigurator.Configure();

if you have

[assembly: XmlConfigurator(Watch = true)]

Having both will result in the two headers and two footers.

As to why you have three sets, perhaps you are calling XmlConfigurator.Configure() twice.




回答2:


Putting XmlConfigurator.Configure() at more than one places may result in having more >than one header and footer pair.

XmlConfigurator.Configure() should be defined only at one place in the solution. Better >approach is to put it in AssemblyInfo.cs.



来源:https://stackoverflow.com/questions/13286124/header-and-footer-coming-two-times-more-in-log4net-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!