Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4Net dilemma

I have a C# solution containing multiple C# projects. I am planning to add logging in it. This logging should be available in all the projects and preferably use log4Net with rolling file logs.

With the above said premise, I could think of two ways to do that.

  1. Initialize logger in entry point (Program class) of the solution & Get the logger instance & use it as a member variable for every class that needs logging.

  2. Add another project, Utilities & define a Logging class with static logging methods. This class should be initialized in entry point (Program class) of the solution.

What could be the best possible solution?

like image 799
vrrathod Avatar asked Mar 16 '11 18:03

vrrathod


1 Answers

I have a similar situation. What we've done is use 1 app config for all the projects and use links to reference it.

In the app.config for your app you set the log4net config Section

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

And later set the Appender:

<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
...

And in each class you want to log you put a line similar to this:

private static readonly ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

Then each class grabs the same logger (singleton). Would that work for you?

like image 74
Queso Avatar answered Oct 27 '22 05:10

Queso