Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log MethodName when wrapping Log4net?

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); 
like image 493
Claus Thomsen Avatar asked Oct 01 '08 11:10

Claus Thomsen


People also ask

How do I use multiple Appenders in log4net?

You can't log to separate appenders - you need to configure different loggers, and attach the appropriate appender to each one. Then log different messages to the different loggers.

Is log4net structured logging?

log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.

What is rolling file Appender in log4net?

"When set to Once the log file will be rolled when the appender is configured. This effectively means that the log file can be rolled once per program execution. So, when you set your RollingFileAppender to "once", then every time you execute your program, a new log file will be created.


1 Answers

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?

like image 89
Magnus Johansson Avatar answered Sep 20 '22 03:09

Magnus Johansson