Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在ExceptionFilter中使用ILogger_.net_Logging_Filter_Asp.net Core_.net Core - Fatal编程技术网

.net 在ExceptionFilter中使用ILogger

.net 在ExceptionFilter中使用ILogger,.net,logging,filter,asp.net-core,.net-core,.net,Logging,Filter,Asp.net Core,.net Core,我想在例外过滤器中使用.NET Core Logger,如下所示: public class GlobalExceptionAttribute : ExceptionFilterAttribute { private readonly ILogger<Inspection> _logger; public GlobalExceptionAttribute(ILogger<Inspection> logger) { _logger =

我想在例外过滤器中使用.NET Core Logger,如下所示:

public class GlobalExceptionAttribute : ExceptionFilterAttribute
{
    private readonly ILogger<Inspection> _logger;
    public GlobalExceptionAttribute(ILogger<Inspection> logger)
    {
        _logger = logger;
    }
    public override void OnException(ExceptionContext context)
    {
        _logger.LogError("Error");
    }
  ... 
}

实际上,我希望记录器由.NET内核DI自动注入。我怎样才能存档

我不会在异常过滤器属性中添加日志记录。我会让异常处理程序进行日志记录。这样,我仍然会有一个日志记录的中心位置,任何异常过滤器都会抛出异常。简而言之,我将实现使我的异常过滤器轻量级,并将日志记录职责分离。我尚未准备好代码,但稍后将尝试发布一些内容。

您是否可以发布特定的错误消息您需要使用ServiceFilter属性。可能的副本。
[GlobalException]
public class Inspection : Controller
{
 ...