码迷,mamicode.com
首页 > 其他好文 > 详细

Log类设计

时间:2018-01-18 01:07:38      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:ack   info   base   bst   new   rac   oge   logs   ilog   

public interface ILogSaveProvider
{
    bool SaveLog(LogEntity logEntity);
}
 public abstract class LogSaveBaseProvider : ILogSaveProvider
    {
        public bool SaveLog(LogEntity logEntity)
        {
            if (!this.IsSaveLogWithConfiguration(logEntity))
                return false;
            if (!this.ValidatorLogEntity(logEntity))
                return false;
            this.FormatLogContect(logEntity);
            return this.DoSaveLog(logEntity);
        }
        /// <summary>
        /// log是否是配置文件中需要保存的类型
        /// </summary>
        /// <param name="logEntity"></param>
        /// <returns></returns>
        protected virtual bool IsSaveLogWithConfiguration(LogEntity logEntity)
        {
            string logType = ConfigurationManager.AppSettings["LogType"];
            if (logEntity.Type.Equals(logType))
                return true;
            return false;
        }

        /// <summary>
        /// 验证log是否有效
        /// </summary>
        /// <param name="logEntity"></param>
        /// <returns></returns>
        protected virtual bool ValidatorLogEntity(LogEntity logEntity)
        {
            if (logEntity == null || logEntity.Content == null)
                return false;
            return true;
        }

        /// <summary>
        /// 格式化log实体中的信息内容
        /// </summary>
        /// <param name="logEntity"></param>
        protected virtual void FormatLogContect(LogEntity logEntity)
        {
            // 提供程序可以根据自己的需要对日志进行格式化
        }

        /// <summary>
        /// 最终的保存方法
        /// </summary>
        /// <param name="logEntity"></param>
        /// <returns></returns>
        protected abstract bool DoSaveLog(LogEntity logEntity);
    }
    public class LogSaveLocalhostProvider : LogSaveBaseProvider
    {
        protected override bool ValidatorLogEntity(LogEntity logEntity)
        {
            if ( base.ValidatorLogEntity(logEntity))
            {
                if (string.IsNullOrEmpty(logEntity.Content.LogTrackInfo))
                    return false;
            }
            return true;
        }

        protected override void FormatLogContect(LogEntity logEntity)
        {
            logEntity.Content.Message = logEntity.Content.Message.Replace("\\", "--");
        }

        protected override bool DoSaveLog(LogEntity logEntity)
        {
            //开始保存
            return true;
        }
    }
        static void Main(string[] args)
        {
            LogEntity logEntity = new LogEntity() { Type = LogType.Exception, Level = LogLevel.Error, Content = new LogContent { LogTrackInfo = "Program.Main", Message = "字符串不能为空" } };
            ILogSaveProvider saveProvider = new LogSaveLocalhostProvider();
            saveProvider.SaveLog(logEntity);
        }

 

Log类设计

标签:ack   info   base   bst   new   rac   oge   logs   ilog   

原文地址:https://www.cnblogs.com/xuyuchen/p/8306731.html

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