Log4net日志记录组件的使用步骤详解和下载

2019-05-20 13:25:27王振洲

        /// </summary>
        /// <param name="configPath"></param>
        /// <returns></returns>
        public static ILog GetInstance(string configPath)
        {
            logHelper = new LogHelper(configPath);

            return log;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configPath"></param>
        private LogHelper(string configPath)
        {
            if (!string.IsNullOrEmpty(configPath))
            {
                log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath));
            }
            else
            {
                log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
        }

    }
}

通过封装类写日志的方法如下

 private static readonly ILog logs = LogHelper.GetInstance(); //LogManager.GetLogger(typeof(TEST));
        protected void Button2_Click(object sender, EventArgs e)
        {
                logs.Fatal("Excption:这里就是要提示的LOG信息");
        }

 

Log4net下载地址:http://xiazai.jb51.net/tools/201302/tools/log4net-1.2.11-src.zip