Singleton logger, static logger, factory logger ... how to register?

I wrap templates and apply the Application Application Log Block for an application written in .NET.

I want to be able to subclass the registrar (i.e. provide logging for a specific domain).

What is the best way to do this?

For example, I have a static Logger class at the moment, but this does not allow me to specialize it for registration in a domain.

For example,

Log(MyDomainObj obj, string msg)

+9
c # logging
Aug 03 '09 at 12:46
source share
3 answers

Check out NLog . They use a pattern like this:

 private static Logger myDomainLogger = LogManager.GetCurrentClassLogger(); 

You can then specialize the output based on the class to which myDomainLogger belongs.

More details:

 class MyDomain { private static Logger _logger = LogManager.GetCurrentClassLogger(); private void SomeFunc() { _logger.Trace("this is a test"); } } 

Then, in your output, you can output it “MyDomain.SomeFunc” as part of the “this is test” message.

+12
Aug 03 '09 at 12:56
source share

Also check log4net . I never found that an EL record was as flexible as log4net. I chose log4net since I was already familiar with using log4j.

 protected readonly log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 

By doing this, I can get the following logs:

2009-07-15 09: 48: 51 674 [4420] DEBUG SampleNamespace.SampleClass [(null)] - An example of the message you want to display

+5
Aug 03 '09 at 16:10
source share

You can even do better. Write a shell class that wraps either Nlog, or log4net, or something else. Then you can use this wrapper class (perhaps use an interface if you really want to separate things) in your code. Thus, if you decide to change the log class, you only need to change one class and not edit all your classes.

+4
Nov 15 '11 at 9:20
source share



All Articles