Log类设计

Posted xuyuchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Log类设计相关的知识,希望对你有一定的参考价值。

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类设计的主要内容,如果未能解决你的问题,请参考以下文章

python常用代码片段总结

前端开发常用js代码片段

vs code 自定义代码片段

sublime text 3 添加 javascript 代码片段 ( snippet )

jacript var let const 区别

ES7-Es8 js代码片段