Asp.net Core全局异常监控和记录日志

时间:2019-10-31
本文章向大家介绍Asp.net Core全局异常监控和记录日志,主要包括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返回值。

如果未使用全局异常捕获,则直接抛出如下异常

         客户端抛出异常后,可查看磁盘写入日志,这里看到我关注的系统编号,主机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方法里注册中间件。

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/viter/p/10013195.html
https://www.jianshu.com/p/cab597211136

原文地址:https://www.cnblogs.com/sword-successful/p/11771858.html