asp.net core全局异常过滤并监控系统BUG将异常信息记录到日志
添加:
using Dw.Util.Helper; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Dw.SysWeb.Models { /// <summary> /// 全局异常过滤器 /// </summary> public class HttpGlobalExceptionFilter : IExceptionFilter { public void OnException(ExceptionContext context) { //监控系统BUG,将异常信息记录到日志 NLogHelper.WriteDebug("控制器:" + context.ActionDescriptor.RouteValues["controller"]+"/"+ context.ActionDescriptor.RouteValues["action"] + "发生异常,异常信息为:" + context.Exception.Message.ToString()); } } }
启用:
services.AddMvc(option => { option.Filters.Add<HttpGlobalExceptionFilter>(); });
效果: