Asp.Net Log4net 配置

一、摘要

  1. NuGet → Log4net
  2. 创建工具类Log4Helper.cs
  3. 创建配置文件Config/Log4net.config
  4. Web.config
  5. AssemblyInfo.cs
  6. 使用

二、详情

  1. NuGet → Log4net
  2. 创建工具类Log4Helper.cs
public static class Log4Helper
    {
        public static void Fatal(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Fatal(message);
            else
                log.Fatal(message, exception);
        }

        public static void Error(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Error(message);
            else
                log.Error(message, exception);
        }

        public static void Warn(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Warn(message);
            else
                log.Warn(message, exception);
        }

        public static void Info(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Info(message);
            else
                log.Info(message, exception);
        }

        public static void Debug(Type type, object message, Exception exception = null)
        {
            ILog log = LogManager.GetLogger(type);
            if (exception == null)
                log.Debug(message);
            else
                log.Debug(message, exception);
        }

        /// 
        /// 取得当前源码的哪一行
        /// 
        /// 
        public static int GetLineNum()
        {
            StackTrace st = new StackTrace(1, true);
            StackFrame stackFrame = st.GetFrame(0);
            int lineNo = 0;
            if (stackFrame != null)
                lineNo = stackFrame.GetFileLineNumber();
            
            return lineNo;
        }

        /// 
        /// 取当前源码的源文件名
        /// 
        /// 
        public static string GetCurSourceFileName()
        {
            StackTrace st = new StackTrace(1, true);
            StackFrame stackFrame = st.GetFrame(0);
            string fileName = "【未知文件名】";
            if (stackFrame != null)
                fileName = stackFrame.GetFileName();

            return fileName;
        }
    }
  1. 创建配置文件 Config/Log4net.config


  
    
      
        
      
      
      
      
      
      
      
        
      
    
    
      
        
      
      
      
      
      
      
      
        
      
    
    
      
        
      
      
      
      
      
      
      
        
      
    
    
      
        
      
      
      
      
      
      
      
        
      
    
    
      
        
      
      
      
      
      
      
      
        
      
    
    
      
        
      
      
      
      
      
      
      
        
      
    
    
      
      
      
      
      
      
    
  

  1. Web.config添加配置如下

  
    
  1. AssemblyInfo.cs中添加代码如下
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Config/log4net.config", Watch = true)]
  1. 使用
Log4Helper.Debug(typeof(HomeController), "Debug");
Log4Helper.Fatal(typeof(HomeController), "Fatal");
Log4Helper.Error(typeof(HomeController), "Error");
Log4Helper.Info(typeof(HomeController), "Info");
Log4Helper.Warn(typeof(HomeController), "Warn");

你可能感兴趣的:(Asp.Net Log4net 配置)