Asp.net Core全局异常监控和记录日志
前言
? ? ? ? ? 系統異常監控可以說是重中之重,系統不可能一直運行良好,開發和運維也不可能24小時盯著系統,系統拋異常后我們應當在第一時間收到異常信息。在Asp.net Core里我使用攔截器和中間件兩種方式來監控異常。全局異常監控的數據最好還是寫入數據庫,方便查詢。
配置NLog
NLog配置文件
<?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>注入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方法,當系統發生未捕獲的異常時就會觸發這個方法。這里全局異常信息最好能放入數據庫里,方便后臺查詢,再就是拋異常后最好能給負責人發郵件和發送報警短信,也可以直接撥打電話。
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方法里注入全局異常處理攔截器。
public void ConfigureServices(IServiceCollection services) {services.AddControllersWithViews();//注入全局異常處理services.AddMvc(option =>{option.Filters.Add(typeof(GlobalExceptionFilter));}); }? ? ?OK,定義了攔截器后,我們自己拋一個未捕獲的異常試試。如圖,都會返回統一的JSON返回值。
中間件
定義中間件,定義中間件時先導入日志命名空間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方法里注冊中間件。
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對象更直觀些。
參考鏈接
https://www.cnblogs.com/suizhikuo/p/8822352.html
原文鏈接:https://www.cnblogs.com/sword-successful/p/11771858.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總?http://www.csharpkit.com?
總結
以上是生活随笔為你收集整理的Asp.net Core全局异常监控和记录日志的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .Net轻量状态机Stateless
- 下一篇: SiteServer CMS 新版本 V