Log4net.dll记录日志的使用

参考学习网址:
http://blog.csdn.net/weifangyh/article/details/8524380
http://blog.csdn.net/dragon_ton/article/details/71601339
https://www.cnblogs.com/kissazi2/p/3392094.html
https://www.cnblogs.com/zeroone/p/3606160.html

1.在AssemblyInfo中添加

[assembly: log4net.Config.XmlConfigurator()]

2.新建一个控制台应用程序,在项目中引用log4net.dll,然后修改app.config:



  
    

2.新建一个loggerhelper类

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

namespace TestLog4Net
{
    public static class LoggerHelper
    {
        private static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// 
        /// 获取当前 Ilog 对象
        /// 
        public static ILog GetILog
        {
            get
            {
                return log;
            }
        }
        /// 
        /// 记录错误日志
        /// 
        /// 
        /// 
        public static void Error(object message, Exception ex)
        {
            log.Error(message, ex);
        }
        /// 
        /// 记录严重错误
        /// 
        /// 
        /// 
        public static void Fatal(object message, Exception ex)
        {
            log.Fatal(message, ex);
        }
        /// 
        /// 记录一般信息
        /// 
        /// 
        public static void Info(object message)
        {
            log.Info(message);
        }
        /// 
        /// 记录调试信息
        /// 
        /// 
        public static void Debug(object message)
        {
            log.Debug(message);
        }
        /// 
        /// 记录警告信息
        /// 
        /// 
        public static void Warn(object message)
        {
            log.Warn(message);
        }
    }
}

3.使用loggerhelper类

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

namespace TestLog4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            
            LoggerHelper.Error("这里存在错误",null);
            Console.ReadKey();
        }
    }
}

测试结果:

Log4net.dll记录日志的使用_第1张图片
测试结果

你可能感兴趣的:(Log4net.dll记录日志的使用)