How to retain callsite information when wrapping NLog

后端 未结 8 2181
感情败类
感情败类 2020-11-29 02:33

I have a class that wraps NLog (called NLogger). My logs are saved to my database. The thing I\'m having a problem with is how do I show where the logging occured. I have th

8条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 02:52

    Guys After several days hard work and search.Finally, I just use one simple class built the Nlog Wrapper which can retain the ${callsite} and get the correct logger name when created the instance of Nlog Wrapper. I will put the code as followed with simple comment. As you can see I use Stacktrace to get the right logger name. Use write and writewithex to register the logevnet so that can retain callsite.

      public  class NlogWrapper 
        {  
            private  readonly NLog.Logger _logger; //NLog logger
    
        /// 
        /// This is the construtor, which get the correct logger name when instance created  
        /// 
    
        public NlogWrapper()
            {
            StackTrace trace = new StackTrace();
    
            if (trace.FrameCount > 1)
            {
                _logger = LogManager.GetLogger(trace.GetFrame(1).GetMethod().ReflectedType.FullName);
            }
            else //This would go back to the stated problem
            {
                _logger = LogManager.GetCurrentClassLogger();
            }
        }
        /// 
        /// These two method are used to retain the ${callsite} for all the Nlog method  
        /// 
        /// LogLevel.
        ///  Passed message.
        ///  Exception.
        private void Write(LogLevel level, string format, params object[] args)
        {
            LogEventInfo le = new LogEventInfo(level, _logger.Name, null, format, args);
            _logger.Log(typeof(NlogWrapper), le);
        }
        private void WriteWithEx(LogLevel level, string format,Exception ex, params object[] args)
        {
            LogEventInfo le = new LogEventInfo(level, _logger.Name, null, format, args);
            le.Exception = ex;
            _logger.Log(typeof(NlogWrapper), le);
        }
    
    
        #region  Methods
        /// 
        /// This method writes the Debug information to trace file
        /// 
        /// The message.
        public  void Debug(String message)
            {
                if (!_logger.IsDebugEnabled) return;
    
            Write(LogLevel.Debug, message);
        }  
    
        public  void Debug(string message, Exception exception, params object[] args)
        {
            if (!_logger.IsFatalEnabled) return;
            WriteWithEx(LogLevel.Debug, message, exception);
        }
    
        /// 
        /// This method writes the Information to trace file
        /// 
        /// The message.
        public  void Info(String message)
            {
                if (!_logger.IsInfoEnabled) return;
            Write(LogLevel.Info, message);
        }
    
        public  void Info(string message, Exception exception, params object[] args) 
        {
            if (!_logger.IsFatalEnabled) return;
            WriteWithEx(LogLevel.Info, message, exception);
        }
        /// 
        /// This method writes the Warning information to trace file
        /// 
        /// The message.
        public  void Warn(String message)
            {
                if (!_logger.IsWarnEnabled) return;
              Write(LogLevel.Warn, message); 
            }
    
        public  void Warn(string message, Exception exception, params object[] args)
        {
            if (!_logger.IsFatalEnabled) return;
            WriteWithEx(LogLevel.Warn, message, exception);
        }
    
        /// 
        /// This method writes the Error Information to trace file
        /// 
        /// The error.
        /// The exception.
        //   public static void Error( string message)
        //  {
        //    if (!_logger.IsErrorEnabled) return;
        //  _logger.Error(message);
        //}
    
        public  void Error(String message) 
        {
            if (!_logger.IsWarnEnabled) return;
            //_logger.Warn(message);
            Write(LogLevel.Error, message);
        }
        public void Error(string message, Exception exception, params object[] args)
        {
            if (!_logger.IsFatalEnabled) return;
            WriteWithEx(LogLevel.Error, message, exception);
        }  
    
    
        /// 
        /// This method writes the Fatal exception information to trace target
        /// 
        /// The message.
        public void Fatal(String message)
            {
                if (!_logger.IsFatalEnabled) return;
             Write(LogLevel.Fatal, message);
        }
    
        public void Fatal(string message, Exception exception, params object[] args)
        {
            if (!_logger.IsFatalEnabled) return;
            WriteWithEx(LogLevel.Fatal, message, exception);
        }
    
        /// 
        /// This method writes the trace information to trace target
        /// 
        /// The message.
        /// 
        public  void Trace(string message, Exception exception, params object[] args)  
        {
            if (!_logger.IsFatalEnabled) return;
            WriteWithEx(LogLevel.Trace, message, exception);
        }
        public  void Trace(String message)
            {
                if (!_logger.IsTraceEnabled) return;
                Write(LogLevel.Trace, message);
        }
    
            #endregion
    
        }
    

提交回复
热议问题