首页 > 其他 > 详细

Log类设计

时间:2018-01-17 23:58:55      阅读:296      评论:0      收藏:0      [点我收藏+]
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类设计

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!