系統異常監控能夠說是重中之重,系統不可能一直運行良好,開發和運維也不可能24小時盯着系統,系統拋異常後咱們應當在第一時間收到異常信息。在Asp.net Core裏我使用攔截器和中間件兩種方式來監控異常。全局異常監控的數據最好仍是寫入數據庫,方便查詢。html
<?xml version="1.0" encoding="utf-8"?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="info" internalLogFile="d:\temp\internal-nlog.txt"> <!-- the targets to write to --> <targets> <!-- write logs to file --> <target xsi:type="File" name="allfile" fileName="d:\temp\nlog-all-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}" /> <!-- another file log, only own logs. Uses some ASP.NET core renderers --> <target xsi:type="File" name="ownFile-web" fileName="d:\temp\nlog-own-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> <!-- write to the void aka just remove --> <target xsi:type="Null" name="blackhole" /> </targets> <!-- rules to map from logger name to target --> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip Microsoft logs and so log only own logs--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> </rules> </nlog>
在Program.cs裏注入NLog依賴,添加依賴前須要導入兩個命名空間Microsoft.Extensions.Logging、 NLog.Web。前端
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .ConfigureLogging(logging=> { logging.ClearProviders(); logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); }) .UseNLog(); }
在Asp.Mvc裏最經常使用的攔截器,在Asp.net Core裏也是支持的。先定義攔截器,再注入攔截器,這裏自定義攔截器實現接口IExceptionFilter,接口會要求實現OnException方法,當系統發生未捕獲的異常時就會觸發這個方法。這裏全局異常信息最好能放入數據庫裏,方便後臺查詢,再就是拋異常後最好能給負責人發郵件和發送報警短信,也能夠直接撥打電話。nginx
public class GlobalExceptionFilter : IExceptionFilter { private IWebHostEnvironment _env; private ILogger<GlobalExceptionFilter> _logger; public GlobalExceptionFilter(IWebHostEnvironment _env,ILogger<GlobalExceptionFilter> _logger) { this._env = _env; this._logger = _logger; } public void OnException(ExceptionContext context) { if (context.Exception.GetType() == typeof(BusException)) { //若是是自定義異常,則不作處理 } else { } //日誌入庫 //向負責人發報警郵件,異步 //向負責人發送報警短信或者報警電話,異步 Exception ex = context.Exception; //這裏給系統分配標識,監控異常確定不止一個系統。 int sysId = 1; //這裏獲取服務器ip時,須要考慮若是是使用nginx作了負載,這裏要兼容負載後的ip, //監控了ip方便定位究竟是那臺服務器出故障了 string ip = context.HttpContext.Connection.RemoteIpAddress.ToString(); _logger.LogError($"系統編號:{sysId},主機IP:{ip},堆棧信息:{ex.StackTrace},異常描述:{ex.Message}"); context.Result = new JsonResult(ResultBody.error(ex.Message)); context.ExceptionHandled = true; } }
在Startup.ConfigureServices方法裏注入全局異常處理攔截器。web
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); //注入全局異常處理 services.AddMvc(option => { option.Filters.Add(typeof(GlobalExceptionFilter)); }); }
OK,定義了攔截器後,咱們本身拋一個未捕獲的異常試試。如圖,都會返回統一的JSON返回值。
若是未使用全局異常捕獲,則直接拋出以下異常
客戶端拋出異常後,可查看磁盤寫入日誌,這裏看到我關注的系統編號,主機ip,堆棧信息和異常描述信息。
數據庫
定義中間件,定義中間件時先導入日誌命名空間Microsoft.Extensions.Logging。服務器
public class GlobalExceptionMiddleware { private readonly RequestDelegate next; private ILogger<GlobalExceptionMiddleware> logger; public GlobalExceptionMiddleware(RequestDelegate next, ILogger<GlobalExceptionMiddleware> logger) { this.next = next; this.logger = logger; } public async Task Invoke(HttpContext context) { try { await next.Invoke(context); } catch (Exception ex) { await HandleExceptionAsync(context, ex); } } private async Task HandleExceptionAsync(HttpContext context, Exception e) { if (e.GetType() == typeof(BusException)) { //若是是自定義異常,則不作處理 } else { } //記日誌 int sysId = 1; string ip = context.Connection.RemoteIpAddress.ToString(); logger.LogError($"系統編號:{sysId},主機IP:{ip},堆棧信息:{e.StackTrace},異常描述:{e.Message}"); string result = System.Text.Json.JsonSerializer.Serialize(ResultBody.error(e.Message)); await context.Response.WriteAsync(result); } }
在Startup.Configure方法裏註冊中間件。mvc
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } //註冊異常處理中間件 app.UseMiddleware<GlobalExceptionMiddleware>(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
中間件這裏處理異常最後向客戶端響應寫入了一個字符串,這是個攔截器處理方式不一樣的地方。固然對客戶端或者前端來講仍是JSON對象更直觀些。app
http://www.javashuo.com/article/p-rropkebi-by.html
http://www.javashuo.com/article/p-vqjgxurg-ca.html
https://www.jianshu.com/p/cab597211136運維