Global Error Logging in ASP.Net MVC 6(ASP.Net MVC 6 中的全局错误日志记录)
问题描述
我正在测试一个 MVC 6 Web Api,并希望将日志记录到一个全局错误处理程序中.只是保证没有错误会在没有被记录的情况下离开系统.我创建了一个 ExceptionFilterAttribute 并在启动时全局添加:
I'm testing out an MVC 6 Web Api and wanted to implement logging into a global error handler. Just guaranteeing no errors get out of the system without being logged. I created an ExceptionFilterAttribute and added it globally in the startup:
public class AppExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
//Notice pulling from HttpContext Application Svcs -- don't like that
var loggerFactory = (ILoggerFactory)context.HttpContext.ApplicationServices.GetService(typeof (ILoggerFactory));
var logger = loggerFactory.Create("MyWeb.Web.Api");
logger.WriteError(2, "Error Occurred", context.Exception);
context.Result = new JsonResult(
new
{
context.Exception.Message,
context.Exception.StackTrace
});
}
}
现在在启动时,我将这个过滤器添加到:
Now in the startup, I'm adding this filter in:
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new AppExceptionFilterAttribute());
});
这一切似乎都是蛮力...有没有更好的方法可以使用 MVC 6 到达这里?
This all seems kind of brute force...is there a better way to get here using MVC 6?
我不喜欢或不确定使用这种方法的事情:
Things I don't like or am unsure about with this approach:
- 不喜欢从 http 上下文中提取 DI
- 没有太多关于引发错误的控制器的上下文(也许我可以通过某种方式从上下文中得到它).
我能想到的另一个选项是有一个基本控制器,它接受所有控制器都继承自的 ILoggerFactory.
The other option I can think of is having a base controller that accepts an ILoggerFactory that all controllers inherit from.
想知道是否有某种诊断中间件可以插入日志记录...
Was wondering if there was some kind of diagnostics middleware that would allow logging to be inserted...
推荐答案
你的问题有 2 个部分.1) DI 可注入过滤器 2) 全局错误处理.
You question has 2 parts. 1) DI injectable filters 2) Global error handling.
关于#1:您可以为此目的使用ServiceFilterAttribute
.示例:
Regarding #1: You can use ServiceFilterAttribute
for this purpose.
Example:
//Modify your filter to be like this to get the logger factory DI injectable.
public class AppExceptionFilterAttribute : ExceptionFilterAttribute
{
private readonly ILogger _logger;
public AppExceptionFilterAttribute(ILoggerFactory loggerfactory)
{
_logger = loggerFactory.CreateLogger<AppExceptionFilterAttribute>();
}
public override void OnException(ExceptionContext context)
{
//...
}
}
<小时>
//Register your filter as a service (Note this filter need not be an attribute as such)
services.AddTransient<AppExceptionFilterAttribute>();
<小时>
//On the controller/action where you want to apply this filter,
//decorate them like
[ServiceFilter(typeof(AppExceptionFilterAttribute))]
public class HomeController : Controller
{
....
}
您应该能够从传递的 ExceptionContext
中获取控制器的详细信息.
You should be able to get the details of the controller from the ExceptionContext
that is passed.
关于 #2:从您之前的帖子看来,您正在使用 ExceptionHandlerMiddleware
(来源 & 扩展源)...如何使用它?...有关它的一些信息:
Regarding #2: From your previous post looks like you were playing with ExceptionHandlerMiddleware
(source & extension source)...how about using that?...some info regarding it:
- 这个中间件是通用的,适用于任何中间件在它之后注册,因此任何概念,如控制器/动作info 是特定于 MVC 的,中间件不会知道.
- 此中间件不处理格式化程序写入异常.你可以编写您自己的缓冲中间件,您可以在其中修改响应body 是一个缓冲流(MemoryStream)并让 MVC 层写下对它的回应.在格式化程序写入异常的情况下,您可以捕获它并发送带有详细信息的 500 错误响应.
这篇关于ASP.Net MVC 6 中的全局错误日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.Net MVC 6 中的全局错误日志记录
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01