在ASP.Net MVC 6全局错误记录 [英] Global Error Logging in ASP.Net MVC 6

查看:227
本文介绍了在ASP.Net MVC 6全局错误记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了一个MVC的Web 6阿比希望实施登录到一个全球性的错误处理程序。只是保证没有错误走不出系统没有被记录下来。我创建了一个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:


  1. 请不要喜欢看HTTP上下文拉DI

  2. 请不要对产生该错误(也许我可以以某种方式的背景下得到它)控制器很多方面。

我能想到的另一种选择是有一个接受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:从您的previous后看起来就像你用 ExceptionHandlerMiddleware 玩(<一个href=\"https://github.com/aspnet/Diagnostics/blob/1.0.0-rc2/src/Microsoft.AspNetCore.Diagnostics/ExceptionHandler/ExceptionHandlerMiddleware.cs\"相对=nofollow>来源&放大器; <一href=\"https://github.com/aspnet/Diagnostics/blob/1.0.0-rc2/src/Microsoft.AspNetCore.Diagnostics/ExceptionHandler/ExceptionHandlerExtensions.cs\"相对=nofollow>扩展源)...怎么样使用...关于它的一些信息:?

Regarding #2: From your previous post looks like you were playing with ExceptionHandlerMiddleware(source & extension source)...how about using that?...some info regarding it:


  • 该中间件是通用的,并适用于任何中间件哪些
    后注册等等之类的控制器/行动的任何概念
    信息是特定于MVC其中中间件不会知道的。

  • 该中间件不处理格式化写入异常。你可以
    写自己的缓冲中间件在这里你可以修改响应
    身体是一个缓冲流(MemoryStream的),并让MVC层
    写它的响应。在格式化器写入异常的情况下,
    你可以捕捉它,并发送带有详细信息的500错误响应。

这篇关于在ASP.Net MVC 6全局错误记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆