Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttribute如何用于记录器(c#)

时间:2016-11-04 15:03:42

标签: c# logging actionfilterattribute

我上课了:

 public class GeoSalesLogger : ActionFilterAttribute
  {
       private Stopwatch timer;
       private Request Argument;

    public ILogger logger { get; set; }

    public GeoSalesLogger()
    {
    }

    public GeoSalesLogger(ILogger<GeoSalesLogger> logger)
    {
        logger = logger;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext){}
    public override void OnActionExecuted(ActionExecutedContext filterContext){}
  }

接下来我打电话给ather课程[GeoSalesLogger]

问题:如何在这里添加ILogger,因为我无法在构造函数中添加它。我也尝试使用autofuc

builder.RegisterType<GeoLogger>();
builder.RegisterFilterProvider();

DependencyResolver.Current.GetService<ILogger>();

如何在没有静电的情况下使用ILogger?

1 个答案:

答案 0 :(得分:0)

您需要使用ActionFilterAttribute来解决ActionExecutingContext的记录器。

public override void OnActionExecuting(ActionExecutingContext context)
{
    var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<GeoSalesLogger>>();
    ...
}