Singleton logger, static logger, factory logger… how to log?

后端 未结 3 1099
南方客
南方客 2021-02-08 12:21

I am wrapping the patterns & practices Enterprise Library Logging Application Block for an application written in .NET.

I want to be able to subclass a logger (i.e t

3条回答
  •  别那么骄傲
    2021-02-08 12:51

    Check out NLog. They use this sort of pattern:

    private static Logger myDomainLogger = LogManager.GetCurrentClassLogger();
    

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

    More detail:

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

    Then in your output you can have it output "MyDomain.SomeFunc" as part of the "this is a test" message.

提交回复
热议问题