0

I created a custom LogFilterAttribute and I want to inject the ILog object into the Logger property. When I debug the project the property Logger of the ActionFilterAttribute is null.

public class LogFilterAttribute : ActionFilterAttribute
{
    /// <summary>
    /// Get or sets the value of the logger object
    /// </summary>
    public ILog Logger { get; internal set; }

    //some loggging with the Logger Property in the overriden methods
}

I register the injection in the NinjectWeCommon.cs in the method RegisterServices.

    kernel.Bind<LogFilterAttribute>().ToSelf().WithPropertyValue("Logger", LogManager.GetLogger(typeof(Global)));

When I'm attached to the debugger in the LogFilterAttribute the property Logger is null. How can I solve this issue?

(The configuration is set in the web.config)

greetings

Briefkasten
  • 1,964
  • 2
  • 25
  • 53
  • Are you applying this as an attribute to controllers/actions or adding it to the global filter collection? – Richard Aug 07 '14 at 09:32
  • I apply this attribute on a webapi controller/action. – Briefkasten Aug 07 '14 at 09:46
  • There appears to be [docs on this very subject](https://github.com/ninject/ninject.web.mvc/wiki/Dependency-injection-for-filters): use [`BindFilter` rather than `Bind`](https://github.com/ninject/ninject.web.mvc/wiki/Filter-configurations). – Richard Aug 07 '14 at 09:51
  • The docu seems to be outdated. I'm using Ninject 3.2.0.0 and the compiler tells me there is no BindFilter method. I tried to follow http://stackoverflow.com/questions/12757419/using-ninject-to-bind-webapi-filter-attributes this. But at the moment I'm not able to fix it. – Briefkasten Aug 07 '14 at 11:10
  • I think I remember reading that filters arent instanciated by the container. However they seem to support property injection by putting a `[Inject]` attribute on the property. See http://stackoverflow.com/questions/4839293/asp-net-mvc3-actionfilterattribute-injection – BatteryBackupUnit Aug 07 '14 at 15:06
  • I think `BindFilter` comes from an additional MVC specific extension to the core NInject. – Richard Aug 07 '14 at 16:18

0 Answers0