Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac injection to custom Web-Api FilterAttribute

I have a custom ExceptionFilter that logs all uncaught exceptions inside Web-Api controllers. I would like to use Autofac, to inject ILog configurations to it.

My question is how to do so ? Autofac site has almost no explanation on how to do so.

CustomFilter:

public class ApiControllerErrorFilterAttribute : ExceptionFilterAttribute
{
    private static readonly ILog log = LogManager.GetLogger("ApiLog");
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        log.Error("Error: ", actionExecutedContext.Exception);
    }
}

p.s. For clarification, I also have a custom Filter for normal controllers and I was able to successfully configure it.

Filter for normal controllers:

public class ControllerErrorFilterAttribute : HandleErrorAttribute
{
    public ICustomLogSettings Log { get; set; }
    public override void OnException(ExceptionContext filterContext)
    {
        Log.GetLogger.Error("Error: ", filterContext.Exception);
    }
}

Log Configuration:

    builder.Register(c => new BaseLog()).As<ICustomLogSettings>().InstancePerRequest();
    builder.RegisterFilterProvider();
like image 290
Stralos Avatar asked Aug 25 '14 10:08

Stralos


People also ask

What is Autofac dependency injection?

Autofac is an addictive IoC container for . NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular . NET classes as components.

Is Autofac needed in .NET core?

Autofac is the most widely used DI/IoC container for ASP.NET, and it is fully compatible with.NET Core as well. . NET Core has a built-in dependency injection framework that is ready to use. Even though the default DI may provide sufficient functionality, there are several limitations when using it.


1 Answers

Some of these examples may help, especially:

builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

builder.Register(c => new MyWebApiFilter())
    .AsWebApiActionFilterFor<ValuesController>()
    .InstancePerApiRequest();

From that, it shows me that you can register a filter that gets resolved just like any other service, this means you should be able to do:

public class ApiControllerErrorFilterAttribute : ExceptionFilterAttribute
{
    private readonly ILog _log

    public ApiControllerErrorFilterAttribute(ILog log)
    {
        _log = log;
    }

    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
        _log.Error("Error: ", actionExecutedContext.Exception);
    }
}

And then be able to register it like so:

builder.RegisterType<ApiControllerErrorFilterAttribute>()
    .AsWebApiActionFilterFor<ValuesController>()
    .InstancePerApiRequest();

Then of course, you need to set up registration for resolving ILog, which will be very similar to this part of the wiki.

like image 159
Jim Bolla Avatar answered Sep 28 '22 09:09

Jim Bolla