Net Core集成Exceptionless分布式日志功能以及全局异常过滤 (2)

既然有了接口,那么当然得实现它了

/// <summary> /// lzhu /// 2018.7.19 /// Exceptionless日志实现 /// </summary> public class ExceptionlessLogger : ILoggerHelper { /// <summary> /// 记录trace日志 /// </summary> /// <param>信息来源</param> /// <param>日志内容</param> /// <param>添加标记</param> public void Trace(string source,string message, params string[] args) { if (args != null && args.Length > 0) { ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Trace).AddTags(args).Submit(); } else { ExceptionlessClient.Default.SubmitLog(source, message, LogLevel.Trace); } } /// <summary> /// 记录debug信息 /// </summary> /// <param>信息来源</param> /// <param>日志内容</param> /// <param>标记</param> public void Debug(string source, string message, params string[] args) { if (args != null && args.Length > 0) { ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Debug).AddTags(args).Submit(); } else { ExceptionlessClient.Default.SubmitLog(source, message, LogLevel.Debug); } } /// <summary> /// 记录信息 /// </summary> /// <param>信息来源</param> /// <param>日志内容</param> /// <param>标记</param> public void Info(string source, string message, params string[] args) { if (args != null && args.Length > 0) { ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Info).AddTags(args).Submit(); } else { ExceptionlessClient.Default.SubmitLog(source, message, LogLevel.Info); } } /// <summary> /// 记录警告日志 /// </summary> /// <param>信息来源</param> /// <param>日志内容</param> /// <param>标记</param> public void Warn(string source, string message, params string[] args) { if (args != null && args.Length > 0) { ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Warn).AddTags(args).Submit(); } else { ExceptionlessClient.Default.SubmitLog(source, message, LogLevel.Warn); } } /// <summary> /// 记录错误日志 /// </summary> /// <param>信息来源</param> /// <param>日志内容</param> /// <param>标记</param> public void Error(string source, string message, params string[] args) { if (args != null && args.Length > 0) { ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Error).AddTags(args).Submit(); } else { ExceptionlessClient.Default.SubmitLog(source, message, LogLevel.Error); } } }

当然实现好了,可别忘了依赖注入哦

//注入ExceptionlessLogger服务 services.AddSingleton<ILoggerHelper, ExceptionlessLogger>();

这时候该写一个全局异常过滤器了

/// <summary> /// lzhu /// 2018.7.19 /// 定义全局过滤器 /// </summary> public class GlobalExceptionFilter : IExceptionFilter { private readonly ILoggerHelper _loggerHelper; //构造函数注入ILoggerHelper public GlobalExceptionFilter(ILoggerHelper loggerHelper) { _loggerHelper = loggerHelper; } public void OnException(ExceptionContext filterContext) { _loggerHelper.Error(filterContext.Exception.TargetSite.GetType().FullName, filterContext.Exception.ToString(), MpcKeys.GlobalExceptionCommonTags, filterContext.Exception.GetType().FullName); var result = new BaseResult() { errcode = ResultCodeAddMsgKeys.CommonExceptionCode,//系统异常代码 errmsg= ResultCodeAddMsgKeys.CommonExceptionMsg,//系统异常信息 }; filterContext.Result = new ObjectResult(result); filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; filterContext.ExceptionHandled = true; } }

全局过滤器写好了,怎么让它生效呢,客观别急啊,上正菜

//添加验证 services.AddMvc(options=> { options.Filters.Add<GlobalExceptionFilter>(); }).AddFluentValidation();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyyjwx.html