Unique log file for each instance of class

前端 未结 4 1896
有刺的猬
有刺的猬 2020-12-14 03:07

I am currently running a windows service that creates multiple instances of a class.

At the top of the service class and every other class in my solution, I have som

4条回答
  •  误落风尘
    2020-12-14 03:26

    log4net has a concept called logger hierarchy.

    A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger. The hierarchy works very much in the same way as the namespace and class hierarchy in .NET. This is very convenient as we shall soon see.

    So you really should be creating your instance specific loggers with . characters instead of _ characters.

    _log = LogManager.GetLogger("DataCollectionClass." + deviceName + "." + DateTime.Now.ToString("MMddyyyy"), typeof(SomeClassTypeHere));
    

    Then in the configuration file reference the logger hierarchy like the following.

    
      
      
        
      
    
    

    Notice how the logger name reference does not contain the fully qualified name used in code. It only references the root of the name. Think of it in the same way you think of namespaces.

提交回复
热议问题