我正在测试一个 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:
我能想到的另一个选项是有一个基本控制器,它接受所有控制器都继承自的 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:
这篇关于ASP.Net MVC 6 中的全局错误日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!