How to log MethodName when wrapping Log4net?

Claus Thomsen picture Claus Thomsen · Oct 1, 2008 · Viewed 43.7k times · Source

I have wrapped Log4net in a static wrapper and want to log

loggingEvent.LocationInformation.MethodName
loggingEvent.LocationInformation.ClassName

However all I get is the name of my wrapper.

How can I log that info using a forwardingappender and a static wrapper class like

Logger.Debug("Logging to Debug");
Logger.Info("Logging to Info");
Logger.Warn("Logging to Warn");
Logger.Error(ex);
Logger.Fatal(ex);

Answer

Magnus Johansson picture Magnus Johansson · Aug 15, 2010

What about the %M and %C variables? http://logging.apache.org/log4net/log4net-1.2.11/release/sdk/log4net.Layout.PatternLayout.html

Usage, something like:

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%date [%thread] %-5level %logger [%M %C] - %message%newline" />
</layout>

Doesn't that do what you are after?