Log4net, how to log a verbose message?

前端 未结 6 861
醉酒成梦
醉酒成梦 2020-12-14 09:53

I can log info messages without a problem, but can\'t figure out how to log verbose messages. Any help would be welcomed.

My problem is:

loggingEvent.Level

6条回答
  •  清歌不尽
    2020-12-14 10:37

    You can add a Verbose (or Trace level) to log4net by using extension methods. This is what I'm using:

    public static class ILogExtentions
    {
        public static void Trace(this ILog log, string message, Exception exception)
        {
            log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
                log4net.Core.Level.Trace, message, exception);
        }
    
        public static void Trace(this ILog log, string message)
        {
            log.Trace(message, null);
        }
    
        public static void Verbose(this ILog log, string message, Exception exception)
        {
            log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
                log4net.Core.Level.Verbose, message, exception);
        }
    
        public static void Verbose(this ILog log, string message)
        {
            log.Verbose(message, null);
        }
    
    }
    

    Usage example:

    public class ClientDAO
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ClientDAO));
    
        public void GetClientByCode()
        {
            log.Trace("your verbose message here");
            //....
        }
    }
    

    Source:

    http://www.matthewlowrance.com/post/2010/07/14/Logging-to-Trace-Verbose-etc-with-log4net.aspx

提交回复
热议问题