关闭

WPF log4Helper

60人阅读 评论(0) 收藏 举报
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ZYG.Alatin.Common
{
    /// /// <summary>
    /// 类说明:日志帮助类
    /// 创建人: pqw
    /// 创建时间: 2016-08-16
    /// 最后修改人
    /// 最后修改时间:
    public class LogHelper
    {
        private static log4net.ILog logger = null;//写日志实体
        /// <summary>
        /// 构造函数
        /// </summary>
        static LogHelper()
        {
            //Appender1 错误日志配置
            log4net.Filter.LevelRangeFilter levfilter = new log4net.Filter.LevelRangeFilter();
            levfilter.LevelMax = log4net.Core.Level.Fatal;
            levfilter.LevelMin = log4net.Core.Level.Error;
            levfilter.ActivateOptions();
            //Appender1 
            log4net.Appender.FileAppender appender1 = new log4net.Appender.FileAppender();
            appender1.AppendToFile = true;
            appender1.File = string.Format("{0}\\alatin\\runlog\\ErrorLog[{1}].log", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Now.ToString("yyyy-MM-dd"));
            appender1.ImmediateFlush = true;
            appender1.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
            appender1.Name = "ErrorAppender";
            appender1.AddFilter(levfilter);
            //Appender2  调试日志配置
            log4net.Filter.LevelRangeFilter levfilter2 = new log4net.Filter.LevelRangeFilter();
            levfilter2.LevelMax = log4net.Core.Level.Debug;
            levfilter2.LevelMin = log4net.Core.Level.Debug;
            levfilter2.ActivateOptions();
            log4net.Appender.FileAppender appender2 = new log4net.Appender.FileAppender();
            appender2.AppendToFile = true;
            appender2.File = string.Format("{0}\\alatin\\runlog\\DebugLog[{1}].log", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Now.ToString("yyyy-MM-dd"));
            appender2.ImmediateFlush = true;
            appender2.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
            appender2.Name = "DebugAppender";
            appender2.AddFilter(levfilter2);
            ///Appender3  所有日志配置
            log4net.Appender.FileAppender appender3 = new log4net.Appender.FileAppender();
            appender3.AppendToFile = true;
            appender3.File = string.Format("{0}\\alatin\\runlog\\AllLog[{1}].log", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DateTime.Now.ToString("yyyy-MM-dd"));
            appender3.ImmediateFlush = true;
            appender3.LockingModel = new log4net.Appender.FileAppender.MinimalLock();
            appender3.Name = "AllAppender";
            ///layout 
            log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("[%date [%thread] %-5level] %newline %message%newline%newline");
            layout.Header = "------ New session ------" + Environment.NewLine;
            layout.Footer = "------ End session ------" + Environment.NewLine;
            // 
            appender1.Layout = layout;
            appender2.Layout = layout;
            appender3.Layout = layout;
            appender1.ActivateOptions();
            appender2.ActivateOptions();
            appender3.ActivateOptions();
            // 
            log4net.Repository.ILoggerRepository repository = log4net.LogManager.CreateRepository("MyRepository");
            log4net.Config.BasicConfigurator.Configure(repository, appender1);
            log4net.Config.BasicConfigurator.Configure(repository, appender2);
            log4net.Config.BasicConfigurator.Configure(repository, appender3);
            logger = log4net.LogManager.GetLogger(repository.Name,"Admin");
            //logger.Fatal("Fatal");
        }
        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Debug(string message)
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug(message);
            }
        }
        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Debug(System.Exception ex)
        {
            if (logger.IsDebugEnabled)
            {
                logger.Debug(ex.Message.ToString() + "/r/n" + ex.Source.ToString() + "/r/n" + ex.TargetSite.ToString() + "/r/n" + ex.StackTrace.ToString());
            }
        }
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void Error(string message)
        {
            if (logger.IsErrorEnabled)
            {
                logger.Error(message);
            }
        }
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Error(System.Exception ex)
        {
            if (logger.IsErrorEnabled)
            {
                if (ex.StackTrace != null)
                {
                    logger.Error(ex.Message.ToString() + "/r/n" + ex.Source.ToString() + "/r/n" + ex.TargetSite.ToString() + "/r/n" + ex.StackTrace.ToString());
                }
            }
        }
        /// <summary>
        /// 致命错误日志 暂时不用
        /// </summary>
        /// <param name="message">日志内容</param>
        private static void Fatal(string message)
        {
            if (logger.IsFatalEnabled)
            {
                logger.Fatal(message);
            }
        }
        /// <summary>
        /// 信息日志 暂时不用
        /// </summary>
        /// <param name="message">日志内容</param>
        private static void Info(string message)
        {
            if (logger.IsInfoEnabled)
            {
                logger.Info(message);
            }
        }
        /// <summary>
        /// 警告日志 暂时不用
        /// </summary>
        /// <param name="message">日志内容</param>
        private static void Warn(string message)
        {
            if (logger.IsWarnEnabled)
            {
                logger.Warn(message);
            }
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:85次
    • 积分:20
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档
    阅读排行
    评论排行