大家好,欢迎来到IT知识分享网。
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); }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/34581.html