码迷,mamicode.com
首页 > Web开发 > 详细

MVC 框架搭建步奏1日志

时间:2015-06-03 17:24:46      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

 1.日志部分

首先是一个日志接口

 public interface ILogger
    {
        /// <summary>
        /// 检查level级别的日志是否启用
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <returns>如果启用返回true,否则返回false</returns>
        bool IsEnabled(LogLevel level);
        /// <summary>
        /// 记录level级别的日志
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <param name="message">需记录的内容</param>
        void Log(LogLevel level, object message);
        /// <summary>
        /// 记录level级别的日志
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <param name="message">需记录的内容</param>
        /// <param name="exception">异常</param>
        void Log(LogLevel level, Exception exception, object message);
        /// <summary>
        /// 记录level级别的日志
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <param name="format">需记录的内容格式<see cref="M:System.String.Format(System.String,System.Object[])" /></param>
        /// <param name="args">替换format占位符的参数</param>
        void Log(LogLevel level, string format, params object[] args);

 接着是日志工厂接口

 public interface ILoggerFactory
    {
        /// <summary>
        /// 依据LoggerName获取<see cref="T:Rosefinch.Core.Logging.ILogger" />
        /// </summary>
        /// <param name="loggerName">日志名称(例如:log4net的logger配置名称)</param>
        /// <returns><see cref="T:Rosefinch.Core.Logging.ILogger" /></returns>
        ILogger GetLogger(string loggerName);

        /// <summary>
        /// 依据LoggerName获取<see cref="T:Rosefinch.Core.Logging.ILogger" />
        /// </summary>
        /// <returns><see cref="T:Rosefinch.Core.Logging.ILogger" /></returns>
        ILogger GetLogger();
    }

 日志等级枚举

  public enum LogLevel
    {
        Debug,
        Information,
        Warning,
        Error,
        Fatal
    }

 日志的实现

 using log4net;

    /// <summary>
    /// 用Log4Net实现的ILogger
    /// </summary>
    public class Log4NetLogger : ILogger
    {
        private ILog log;

        internal Log4NetLogger(ILog log)
        {
            this.log = log;
        }

        /// <summary>
        /// 检查level级别的日志是否启用
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <returns>如果启用返回true,否则返回false</returns>
        public bool IsEnabled(LogLevel level)
        {
            switch (level)
            {
                case LogLevel.Debug:
                    return this.log.IsDebugEnabled;
                case LogLevel.Information:
                    return this.log.IsInfoEnabled;
                case LogLevel.Warning:
                    return this.log.IsWarnEnabled;
                case LogLevel.Error:
                    return this.log.IsErrorEnabled;
                case LogLevel.Fatal:
                    return this.log.IsFatalEnabled;
                default:
                    return false;
            }
        }

        /// <summary>
        /// 记录level级别的日志
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <param name="message">需记录的内容</param>
        public void Log(LogLevel level, object message)
        {
            if (!this.IsEnabled(level))
            {
                return;
            }
            switch (level)
            {
                case LogLevel.Debug:
                    this.log.Debug(message);
                    return;
                case LogLevel.Information:
                    this.log.Info(message);
                    return;
                case LogLevel.Warning:
                    this.log.Warn(message);
                    return;
                case LogLevel.Error:
                    this.log.Error(message);
                    return;
                case LogLevel.Fatal:
                    this.log.Fatal(message);
                    return;
                default:
                    return;
            }
        }

        /// <summary>
        /// 记录level级别的日志
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <param name="message">需记录的内容</param>
        /// <param name="exception">异常</param>
        public void Log(LogLevel level, System.Exception exception, object message)
        {
            if (!this.IsEnabled(level))
            {
                return;
            }
            switch (level)
            {
                case LogLevel.Debug:
                    this.log.Debug(message, exception);
                    return;
                case LogLevel.Information:
                    this.log.Info(message, exception);
                    return;
                case LogLevel.Warning:
                    this.log.Warn(message, exception);
                    return;
                case LogLevel.Error:
                    this.log.Error(message, exception);
                    return;
                case LogLevel.Fatal:
                    this.log.Fatal(message, exception);
                    return;
                default:
                    return;
            }
        }

        /// <summary>
        /// 记录level级别的日志
        /// </summary>
        /// <param name="level">日志级别<seealso cref="T:Rosefinch.Core.Logging.LogLevel" /></param>
        /// <param name="format">需记录的内容格式<see cref="M:System.String.Format(System.String,System.Object[])" /></param>
        /// <param name="args">替换format占位符的参数</param>
        public void Log(LogLevel level, string format, params object[] args)
        {
            if (!this.IsEnabled(level))
            {
                return;
            }
            switch (level)
            {
                case LogLevel.Debug:
                    this.log.DebugFormat(format, args);
                    return;
                case LogLevel.Information:
                    this.log.InfoFormat(format, args);
                    return;
                case LogLevel.Warning:
                    this.log.WarnFormat(format, args);
                    return;
                case LogLevel.Error:
                    this.log.ErrorFormat(format, args);
                    return;
                case LogLevel.Fatal:
                    this.log.FatalFormat(format, args);
                    return;
                default:
                    return;
            }
        }

 日志工厂的实现

 public class Log4NetFactory:ILoggerFactory
    {

        private static bool _isConfigLoaded;
        /// <summary>
        /// 构造函数(默认加载"~/Config/log4net.config"作为log4net配置文件)
        /// </summary>
        public Log4NetFactory()
            : this("~/Config/log4net.config")
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configFilename">
        ///     <remarks>
        ///         <para>log4net配置文件路径,支持以下格式:</para>
        ///         <list type="bullet">
        ///             <item>~/config/log4net.config</item>
        ///             <item>~/web.config</item>
        ///             <item>c:\abc\log4net.config</item>
        ///         </list>
        ///     </remarks>
        /// </param>
        public Log4NetFactory(string configFilename)
        {
            if (!_isConfigLoaded)
            {
                if (string.IsNullOrEmpty(configFilename))
                {
                    configFilename = "~/Config/log4net.config";
                }
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(WebUtility.GetPhysicalFilePath(configFilename));
                if (!fileInfo.Exists)
                {
                    throw new ApplicationException(string.Format("log4net配置文件 {0} 未找到", fileInfo.FullName));
                }

                XmlConfigurator.Configure(fileInfo);
                _isConfigLoaded = true;
            }
        }
        /// <summary>
        /// 依据LoggerName获取<see cref="T:Rosefinch.Core.Logging.ILogger" />
        /// </summary>
        /// <param name="loggerName">日志名称(例如:log4net的logger配置名称)</param>
        /// <returns><see cref="T:Rosefinch.Core.Logging.ILogger" /></returns>
        public ILogger GetLogger(string loggerName)
        {
            return new Log4NetLogger(LogManager.GetLogger(loggerName));
        }


        public ILogger GetLogger()
        {
            return new Log4NetLogger(LogManager.GetLogger("Rosefinch"));
        }

 

MVC 框架搭建步奏1日志

标签:

原文地址:http://www.cnblogs.com/LiYiPeng2015/p/4549435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!