Method Name in log4net C#

后端 未结 2 530
星月不相逢
星月不相逢 2021-02-10 14:51

I created a c# wrapper for log4net.

It has Debug() and Error() methods.

I want to log the method name which logs the record, but If I try to use the %method conv

2条回答
  •  滥情空心
    2021-02-10 15:19

    Scrap that wrapper.

    Configuration

    To initialize logging you can instead configure it quite easily in your startup project.

    1. Click on the arrow next to your "Properties" on your startup project in Solution explorer
    2. Double click on assembly info

    3 Add the following:
    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

    Screenshot:

    Usage

    Now in your classes simply add:

    public class YourClass
    {
        private ILog _logger = LogManager.GetLogger(typeof(YourClass));
    
        // [....]
    }
    

    And in your log4net.config you can now use the logger property in the output:

    
        
    
    

    Which will print the namespace and type name on every log line (-7 and -40 pads the names so that I get straight columns).

    The other great thing is that you can also use a filter on the namespace (to make all database classes log to "databases.log" etc).

    
      
      
      
      
      
      
      
        
      
    
      
      
        
      
      
    
    

    You can also use %type{1} instead if %logger to get only the class name.

提交回复
热议问题