Log4Net Dilemma

I have a C # solution containing several C # projects. I plan to add an entry to it. This logging should be available in all projects and it is preferable to use log4Net with file file calendars.

With the above statement, I could think of two ways of doing this.

  • Initialize the registrar at the entry point (program class) of the solution and obtain a journal instance and use it as a member variable for each class that needs to be logged.

  • Add another project, Utilities, and define the log class using static logging methods. This class must be initialized at the entry point (program class) of the solution.

What could be the best solution?

+7
source share
5 answers

I have the same situation. What we did is use 1 application configuration for all projects and use links to link to it.

In app.config for your application, you set the log4net configuration section

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

And later install Appender:

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

And in every class that you want to write, you put a line like this:

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

Then each class captures the same log (singleton). Will this work for you?

+6
source

In my projects, I wrote a wrapper around LOG4NET to (theoretically) exchange it with other logging frameworks like NLOG without breaking my code.

My own logging class is available through static singleton. He initializes this singleton.

(BTW: I posted the source code in Code Project a while ago, maybe this gives you some inspiration)

0
source

If you are thinking of initializing something at the entry point each time, then you are writing yourself a case for injecting dependencies. Using a DI library (such as Unity, Castle, or some other flavor that you like), you can either inject a constructor or inject properties to automatically initialize your log class (or Log4Net).

Alternatively, you can use the DI kernel to make it single, so you only have one instance of this file.

0
source

Building the Queso response, we had .dlls that were imported with reflection at runtime. To make them use Log4Net, we created a log4net.config file with all the relevant configuration sections. We also used the same line of code to initialize the log in each class that references Queso. A separate configuration allowed us to use it throughout the application domain.

EDIT we also had to make a modification in the appsettings file to allow this.

http://haacked.com/archive/2005/03/07/ConfiguringLog4NetForWebApplications.aspx

0
source

Option 1 is the way to go.

If you use static methods, you lose the ability to use hierarchical loggers. This feature allows you to customize the log output in different ways for individual classes or even for all subsystems, for example. YourNameSpace.Security . This tutorial describes this topic in detail (other chapters also read well).

Building a wrapper is certainly not a bad idea, but it is not strictly necessary. This allows several things: change the log structure, use DI, add additional levels of logs (e.g. log.Verbose ()) or various overloads for logging ...

Btw. I would initialize the loggers as follows:

 ILog log = LogManager.GetLogger(typeof(YourClass)); 
0
source

All Articles