tags:

views:

95

answers:

1

I am implementing a custom XML formatter for log4.net

   public class ISDSApplicationEventsLayout : XmlLayoutBase<br>
           {<br>
        protected override void FormatXml(...)<br>
          {<br>
              //Location Info <br>
              writer.WriteStartElement("Method");<br>
              writer.WriteString(**loggingEvent.LocationInformation.MethodName**);<br>
              writer.WriteEndElement();<br>
          }<br>
      }<br>

The problem is ... now when I call log method from my log wrapper class... called logging

public static void logEvent(string message)
{
    log.Info(isdsLog); 
}

I get the output....

  <Method>logEvent</Method>

How is it possible to have the method name that called logEvent, rather than logEvent as the method name?

Thank you

Question Update:

If this above seems a bit complicated - what I am really asking is : How do you keep the context of the method that called the wrapping logging function in log4net...

example... method doWork()... calls -> logging wrapper --> calls log4net....

How do you make the methodname = doWork and NOT logging wrapper function....

+1  A: 

I don't think you can easily fix this with out-of-the-box log4net. If we take a look at the ILog.Info method in the LogImpl class, that you are calling:

 virtual public void Info(object message) 
 {
  Logger.Log(ThisDeclaringType, m_levelInfo, message, null);
 }

When a message is logged, log4net will walk the stacktrace of the current call in order to find the method that initiated the log operation. To do this Log4net uses the "ThisDeclaringType" type as the boundary of the search, the first call "above" calls within that type is chosen as the initiating method.

In your case, the first method encountered is the logEvent method. If you dropped the logEvent wrapper and used the logging methods directly you will get the desired information.

Peter Lillevold