3

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

Philipp M
  • 1,877
  • 7
  • 27
  • 38
user1805377
  • 73
  • 1
  • 9
  • Where are you configuring your logger? Could you share that piece of code? – Suhas Nov 08 '12 at 09:29
  • updated the post for logger creation. I am doing it in app.config – user1805377 Nov 08 '12 at 09:40
  • Where in your application is the call to `GetLogger` and `XmlConfigurator.Configure()` is located? What kind of application is it? A web app or a console app etc? Sometimes it happens that these calls are placed in a a wrong location and they get called multiple times resulting in unwanted behavior. – Suhas Nov 08 '12 at 09:49
  • GetLogger() and XmlConfigurator.Configure() are called in a static constructor of a 'LogHelper' class. _debugger is a static readonly Ilog variable. – user1805377 Nov 08 '12 at 10:01

2 Answers2

7

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.

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • Thanks a lot. This solved my problem. I removed XmlConfigurator.Configure() which i was using while creating a logger. Now [Log Starts] and [Log Ends] are just coming once. – user1805377 Nov 09 '12 at 05:24
0

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.

user1805377
  • 73
  • 1
  • 9