当前位置 : 主页 > 网络编程 > net编程 >

Log4net工具类

来源:互联网 收集:自由互联 发布时间:2023-09-06
using log4net;using log4net.Config;using System;using System.Collections.Generic;using System.Configuration;using System.IO;namespace SARC.Infrastructure.Util{ public static class LogUtil { private const string _ROOTLOOGER__NAME = "ROOT"; p
using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;

namespace SARC.Infrastructure.Util
{
    public static class LogUtil
    {
        private const string _ROOTLOOGER__NAME = "ROOT";
        private static readonly Dictionary<string, ILog> _Loggers = new Dictionary<string, ILog>();
        private static readonly object _LockerForAddLogger = null;

        static LogUtil()
        {
            _LockerForAddLogger = new object();
            _loadFromFile();
        }

        #region 取logger

        // 使用程序入口自定义的log4net配置文件
        private static void _loadFromFile()
        {
            string log4NetConfigPath = AppConfigUtil.GetAppConfig("log4net_config");
            // app/web.config file has a "log4net_config" configure value
            if (!string.IsNullOrEmpty(log4NetConfigPath))
            {
                log4NetConfigPath = FileUtil.CheckAndFillUpPath(log4NetConfigPath);
                XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetConfigPath));
            }
            else
            {
                var type = typeof(LogUtil);
                var stream = type.Assembly.GetManifestResourceStream(type, "log4net_config");
                XmlConfigurator.Configure(stream);
            }
        }

        private static ILog _initialLogger(string logName)
        {
            ILog logger;
            if (logName == _ROOTLOOGER__NAME)
            {
                Type tp = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType; 
                logger = LogManager.GetLogger(tp); 
            }
            else
            {
                logger = LogManager.Exists(logName); 
            }
            if (logger == null)
            {
                throw new ConfigurationErrorsException(logName + " does not exist.");
            }
            return logger;
        }
        #endregion

        // 取得 "log4net.config" 文件里的ROOT logger
        public static ILog GetLogger(string loggerName)
        {
            if (!_Loggers.ContainsKey(loggerName))
            {
                lock (_LockerForAddLogger)
                {
                    if (!_Loggers.ContainsKey(loggerName))
                    {
                        ILog log = _initialLogger(loggerName);
                        _Loggers.Add(loggerName, log);
                        _writeLevel(log, loggerName);
                    }
                }
            }

            return _Loggers[loggerName];
        }

        private static void _writeLevel(ILog logger, string loggerName)
        {
            logger.FatalFormat("[Begin {0} in {1}]", loggerName, AppDomain.CurrentDomain.BaseDirectory);
            logger.Fatal("【Log4NetDefault.config】开启的级别:\r\nFATAL:"
                + logger.IsFatalEnabled +
                "\r\nERROR:" + logger.IsErrorEnabled +
                "\r\nWARN:" + logger.IsWarnEnabled +
                "\r\nINFO:" + logger.IsInfoEnabled +
                "\r\nDEBUG:" + logger.IsDebugEnabled);
        }

        /// <summary>
        /// 默认ILog,文件为Main.log
        /// </summary>
        public static ILog Default
        {
            get { return GetLogger(_ROOTLOOGER__NAME); }
        }
    }
}

调用

LogUtil.Default.Info("");

LogUtil.Default.Error("");

LogUtil.Default.Warn("");

LogUtil.Default.Debug("");

【本文转自:香港高防服务器 http://www.558idc.com/hkgf.html 复制请保留原URL】
上一篇:c#设计模式-创建型模式 之 原型模式
下一篇:没有了
网友评论