<legend id='SFSKf'><style id='SFSKf'><dir id='SFSKf'><q id='SFSKf'></q></dir></style></legend>
  • <small id='SFSKf'></small><noframes id='SFSKf'>

  • <tfoot id='SFSKf'></tfoot>
      <bdo id='SFSKf'></bdo><ul id='SFSKf'></ul>

      <i id='SFSKf'><tr id='SFSKf'><dt id='SFSKf'><q id='SFSKf'><span id='SFSKf'><b id='SFSKf'><form id='SFSKf'><ins id='SFSKf'></ins><ul id='SFSKf'></ul><sub id='SFSKf'></sub></form><legend id='SFSKf'></legend><bdo id='SFSKf'><pre id='SFSKf'><center id='SFSKf'></center></pre></bdo></b><th id='SFSKf'></th></span></q></dt></tr></i><div id='SFSKf'><tfoot id='SFSKf'></tfoot><dl id='SFSKf'><fieldset id='SFSKf'></fieldset></dl></div>

        ASP.Net MVC 6 中的全局错误日志记录

        时间:2023-07-10
        • <bdo id='O00ck'></bdo><ul id='O00ck'></ul>
            <tbody id='O00ck'></tbody>

              <tfoot id='O00ck'></tfoot>
              • <legend id='O00ck'><style id='O00ck'><dir id='O00ck'><q id='O00ck'></q></dir></style></legend>

                  <i id='O00ck'><tr id='O00ck'><dt id='O00ck'><q id='O00ck'><span id='O00ck'><b id='O00ck'><form id='O00ck'><ins id='O00ck'></ins><ul id='O00ck'></ul><sub id='O00ck'></sub></form><legend id='O00ck'></legend><bdo id='O00ck'><pre id='O00ck'><center id='O00ck'></center></pre></bdo></b><th id='O00ck'></th></span></q></dt></tr></i><div id='O00ck'><tfoot id='O00ck'></tfoot><dl id='O00ck'><fieldset id='O00ck'></fieldset></dl></div>
                1. <small id='O00ck'></small><noframes id='O00ck'>

                2. 本文介绍了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:

                  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:从您之前的帖子看来,您正在使用 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 Core 中的子域之间共享会话?

                  相关文章

                  1. <legend id='KsVlN'><style id='KsVlN'><dir id='KsVlN'><q id='KsVlN'></q></dir></style></legend>
                  2. <small id='KsVlN'></small><noframes id='KsVlN'>

                      <bdo id='KsVlN'></bdo><ul id='KsVlN'></ul>
                  3. <tfoot id='KsVlN'></tfoot>

                      <i id='KsVlN'><tr id='KsVlN'><dt id='KsVlN'><q id='KsVlN'><span id='KsVlN'><b id='KsVlN'><form id='KsVlN'><ins id='KsVlN'></ins><ul id='KsVlN'></ul><sub id='KsVlN'></sub></form><legend id='KsVlN'></legend><bdo id='KsVlN'><pre id='KsVlN'><center id='KsVlN'></center></pre></bdo></b><th id='KsVlN'></th></span></q></dt></tr></i><div id='KsVlN'><tfoot id='KsVlN'></tfoot><dl id='KsVlN'><fieldset id='KsVlN'></fieldset></dl></div>