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

前言

          系统异常监控可以说是重中之重,系统不可能一直运行良好,开发和运维也不可能24小时盯着系统,系统抛异常后我们应当在第一时间收到异常信息。在Asp.net Core里我使用拦截器和中间件两种方式来监控异常。全局异常监控的数据最好还是写入数据库,方便查询。

配置NLog

640?wx_fmt=jpeg

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返回值。640?wx_fmt=jpeg640?wx_fmt=jpeg640?wx_fmt=jpeg

中间件

定义中间件,定义中间件时先导入日志命名空间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 

640?wx_fmt=jpeg

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/313173.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Deeplab 在Qt Creator下编译报错undefined reference to Mat_xxx

https://github.com/TheLegendAli/DeepLab-Context/issues/8 1. 修改工程根目录下的CMakeLists.txt 末尾加上 find_package(MATIO REQUIRED) include_directories(${MATIO_INCLUDE_DIR}) 2. 修改src/caffe/CMakeLists.txt target_link_libraries(caffe proto...) 中加…

在.Net Core 3.0中尝试新的System.Text.Json API

.NET Core 3.0提供了一个名为System.Text.Json的全新命名空间&#xff0c;它支持reader/writer&#xff0c;文档对象模型&#xff08;DOM&#xff09;和序列化程序。在此博客文章中&#xff0c;我将介绍它如何工作以及如何使用。 获取JSON库 如果以.NET Core为目标,请安装.NET …

caffe blob操作

http://blog.luoyetx.com/2015/10/reading-caffe-2/ Blob 在 Caffe 中扮演了重要的角色&#xff0c;用于存储数据和网络参数&#xff0c;同时也在 CPU 和 GPU 之间做了数据同步。Blob 原本在 Caffe 中被表示为一个 4 维数组 (num x channel x height x width)&#xff0c;现在可…

extern __shared__

http://blog.csdn.net/openhero/article/details/3890578 首先是讲一下shared的memory的两种使用方法&#xff0c;然后讲解一下shared memory的bank conflict的问题&#xff0c;这个是shared memory访问能否高效的问题所在&#xff1b; Shared memory的常规使用&#xff1a;…

SiteServer CMS 新版本 V6.13(2019年11月1日发布)

欢迎来到 SiteServer CMS V6.13 版本&#xff0c;经过两个月的连续迭代开发&#xff0c;V6.13版本新增了几项重要功能&#xff0c;我们希望你会喜欢&#xff0c;一些关键的亮点包括&#xff1a;。新增功能及BUG 修复经过两个月的连续迭代开发&#xff0c;V6.13 版本新增了部分功…

CUDA的global内存访问的问题

http://blog.csdn.net/OpenHero/article/details/3520578 关于CUDA的global内存访问的问题&#xff0c;怎么是访问的冲突&#xff0c;怎样才能更好的访问内存&#xff0c;达到更高的速度。下面先看几张图&#xff0c;这些图都是CUDA编程手册上的图&#xff0c;然后分别对这些…

C# 8 新特性 - 异步流 Asynchronous Streams

异步流 Asynchronous Streams例子 这是一个很简单的控制台程序。它有一个NumberFactory&#xff0c;它可以根据传递的参数来产生一串数字&#xff08;IEnumerable<int>&#xff09;。然后在这个程序中把每个数字都打印出来&#xff0c;同时在前边显示出当前的线程ID。 这…

__syncthreads()

http://www.cnblogs.com/dwdxdy/p/3215136.html __syncthreads()是cuda的内建函数&#xff0c;用于块内线程通信. __syncthreads() is you garden variety thread barrier. Any thread reaching the barrier waits until all of the other threads in that block also reach i…

互联网50周年!这有它的一张“出生证明”

2019 年 10 月 29 日是互联网的 50 周年&#xff0c;50 年前(1969 年 10 月 29 日)&#xff0c;加州大学洛杉矶分校的计算机将一个只有两个字母(LO)的数据包发送到斯坦福研究所的计算机上&#xff0c;这是互联网史上的第一个数据包&#xff0c;从此开启互联网时代的第一步。 当…

Eltwise_layer简介

http://www.voidcn.com/blog/thy_2014/article/p-6117416.html common_layer&#xff1a; ArgMaxLayer类&#xff1b; ConcatLayer类&#xff1a; EltwiseLayer类&#xff1b; FlattenLayer类&#xff1b; InnerProductLayer类&#xff1b; MVNLayer类&#xff1b; SilenceLaye…

PowerBI 秒级实时大屏展示方案 全面助力双十一

双十一来了&#xff0c;你准备好了吗&#xff1f;不管你是否准备完毕&#xff0c;我们带来了全网首发的 PowerBI 秒级实时大屏展示方案&#xff0c;你可以直接用来展示双十一的实时状况。我们一步步来说明这个套件模板教程。真实效果功能如下&#xff1a;全实时展示 双十一 当天…

caffe基本函数

http://blog.csdn.net/seven_first/article/details/47378697目录 主要函数 caffe_cpu_gemm 函数caffe_cpu_gemv 函数caffe_axpy 函数caffe_set 函数caffe_add_scalar 函数caffe_copy 函数caffe_scal 函数caffeine_cup_axpby 函数caffe_add caffe_sub caffe_mul caffe_div 函数…

优化 .net core 应用的 dockerfile

优化 .net core 应用的 dockerfileIntro在给 .net core 应用的写 dockerfile 的时候一直有个苦恼&#xff0c;就是如果有很多个项目&#xff0c;在 dockerfile 里写起来就会很繁琐&#xff0c;有很多项目文件要 copy&#xff0c;dockerfile 还不支持直接批量复制项目文件&#…

Ubuntu 上不了网

http://askubuntu.com/questions/441619/how-to-successfully-restart-a-network-without-reboot-over-ssh 指令&#xff1a; sudo service network-manager restart

C# 8 新特性 - 静态本地方法

从C# 8 开始&#xff0c;本地方法就可以是静态的了。 与其他的本地方法不同&#xff0c;静态的本地方法无法捕获任何本地状态量。 直接看例子&#xff1a; 这段代码里有两个本地方法&#xff0c;他们分别对实例的一个字段和方法里的一个本地变量进行了修改操作&#xff0c;也就…

Caffe使用其他人代码CUDA不兼容问题

只要将别人的上层代码使用即可&#xff0c;底层CUDA还是用自己的版本。

​.NET手撸2048小游戏

前言2048是一款益智小游戏&#xff0c;得益于其规则简单&#xff0c;又和 2的倍数有关&#xff0c;因此广为人知&#xff0c;特别是广受程序员的喜爱。本文将再次使用我自制的“准游戏引擎” FlysEngine&#xff0c;从空白窗口开始&#xff0c;演示如何“手撸” 2048小游戏&…

cannot import caffe

遇到问题“cannot import caffe”首先make pycaffe&#xff0c; 如果不行&#xff0c;分别尝试&#xff1a; 1. export PYHTONPATH/path/to/caffe/python:$PYTHONPATH 注&#xff1a;以上方法只能在当前terminal中生效&#xff0c;如需长久生效&#xff0c;需修改~/.bashrc …

自行实现高性能MVC

wcf虽然功能多、扩展性强但是也面临配置忒多&#xff0c;而且restful的功能相当怪异&#xff0c;并且目前没法移植。asp.net core虽然支持webapi&#xff0c;但是功能也相对繁多、配置复杂。就没有一个能让码农们安安心心的写webapi&#xff0c;无需考虑性能、配置、甚至根据问…

caffe matio问题

http://blog.csdn.net/houqiqi/article/details/46469981 注&#xff1a;如果指令行模式实在解决不了/lib/libcaffe.so: undefined reference to Mat_VarReadDataLinear问题&#xff0c;可以尝试在QT下进行训练和测试。 1&#xff0c; 下载matio(http://sourceforge.NET/pro…