WPF log4Helper

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);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值