WinForm log4net使用

1.引入log4net.dll

2.创建log4net.config



  
  
  
  
  
    
      
      
      
      
      
      
      
        
        
        
      
    
    
      
      
      
      
      
      
      
        
        
        
      
    
    
      
      
      
      
      
      
      
        
        
        
      
    
    
    
      
      
    
    
      
      
    
    
      
      
    
  
  

  

3.AssemblyInfo.cs中添加如下

// 指定log4net 的配置文件
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

4.log4net.config 右键 - 属性 - 高级 

复制到输出目录 - 如果较新则复制

5. private static ILog log = LogManager.GetLogger("");

6.新建LogHelper类

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AttachmentsExport
{
    public class LogHelper
    {
        /// 
        /// 普通日志
        /// 
        /// 日志内容
        public static void Info(string message)
        {
            ILog log = LogManager.GetLogger("Info");
            if (log.IsInfoEnabled)
            {
                log.Info(message);
            }
        }
        /// 
        /// 错误日志带异常
        /// 
        /// 错误日志
        public static void Error(string message, Exception ex)
        {
            ILog log = LogManager.GetLogger("Error");
            if (log.IsErrorEnabled)
            {
                log.Error(message, ex);
            }
        }

        /// 
        /// 错误日志不带异常
        /// 
        /// 错误日志
        public static void Error(string message)
        {
            ILog log = LogManager.GetLogger("Error");
            if (log.IsErrorEnabled)
            {
                log.Error(message);
            }
        }

        public static void Warning(string message)
        {
            ILog log = LogManager.GetLogger("Warning");
            if (log.IsWarnEnabled)
            {
                log.Warn(message);
            }
        }
    }
}

你可能感兴趣的:(c#,c#)