bahadir arslan
bahadir arslan

Reputation: 4762

Injecting NLog to Web Api Controllers With Autofac

I am trying to add NLog my Web Api project with Autofac. But i have problems.

After installed NLog package from NuGet, i added following files to my project. (Because of following different posts and examples it might be confused)

ILogger.cs

 public interface ILogger {
        void Debug(string message);
        void Trace(string message);
        void Info(string message);
        void Warning(string message);
        void Error(string message);
        void Error(string message, Exception exception);
        void Fatal(string message);
        void Fatal(string message, Exception exception);
    }

LoggerAttribute.cs

[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Class)]
    public class LoggerAttribute : Attribute {
        public readonly string Name;

        public LoggerAttribute(string name) {
            Name = name;
        }
    }

NLogger.cs

public class NLogger : ILogger {
        private readonly NLog.Logger logger;

        public NLogger(Type loggerType) {
             logger = LogManager.GetLogger(loggerType.FullName);
        }

        public void Debug(string message) {
            logger.Debug(message);
        }

        public void Trace(string message) {
            logger.Trace(message);
        }

        public void Info(string message) {
            logger.Info(message);
        }

        public void Warning(string message) {
            logger.Warn(message);
        }

        public void Error(string message) {
            logger.Error(message);
        }

        public void Error(string message, Exception exception) {
            logger.ErrorException(message, exception);
        }

        public void Fatal(string message) {
            logger.Fatal(message);
        }

        public void Fatal(string message, Exception exception) {
            logger.FatalException(message, exception);
        }
    }

NLogModule.cs

public class NLogModule : Module {
        protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration) {
            registration.Preparing += OnComponentPreparing;
        }


        static void OnComponentPreparing(object sender, PreparingEventArgs e) {
            var typePreparing = e.Component.Activator.LimitType;

            // By default, the name supplied to the logging instance is the name of the type in which it is being injected into.
            string loggerName = typePreparing.FullName;

            //If there is a class-level logger attribute, then promote its supplied name value instead as the logger name to use.
            var loggerAttribute = (LoggerAttribute)typePreparing.GetCustomAttributes(typeof(LoggerAttribute), true).FirstOrDefault();
            if(loggerAttribute != null) {
                loggerName = loggerAttribute.Name;
            }

            e.Parameters = e.Parameters.Union(new Parameter[]
        {
            new ResolvedParameter(
                (p, i) => p.ParameterType == typeof (ILogger),
                (p, i) =>
                {
                    // If the parameter being injected has its own logger attribute, then promote its name value instead as the logger name to use.
                    loggerAttribute = (LoggerAttribute)
                    p.GetCustomAttributes(typeof(LoggerAttribute),true).FirstOrDefault();
                    if (loggerAttribute != null)
                    {
                        loggerName = loggerAttribute.Name;
                    }

                    // Return a new Logger instance for injection, parameterised with the most appropriate name which we have determined above.
                    return LogManager.GetLogger(loggerName);
                }),

            // Always make an unamed instance of Logger available for use in delegate-based registration e.g.: Register((c,p) => new Foo(p.TypedAs<Logger>())
            new TypedParameter(typeof(ILogger), LogManager.GetLogger(loggerName))
        });
        }
    }

TestController.cs

 public class TestController : ApiController
    {
        private IUnitOfWork uow;
        private ILogger _logger;
        // GET api/test
        public TestController(IUnitOfWork unitOfWork, ILogger logger) {
            uow = unitOfWork;
            _logger = logger;
        }
...
    }

Bootstrapper.cs

var builder = new ContainerBuilder();

    // Register the Web API controllers.
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

    // Register other dependencies.

    builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerApiRequest();
    builder.RegisterType<NLogger>().As<ILogger>().InstancePerApiRequest();
    builder.RegisterModule(new NLogModule());
    builder.Register((c, p) => new TestController(p.TypedAs<UnitOfWork>(), p.TypedAs<NLogger>()));
    // Build the container.
    var container = builder.Build();

    // Create the depenedency resolver.
    var resolver = new AutofacWebApiDependencyResolver(container);

    // Configure Web API with the dependency resolver.
    GlobalConfiguration.Configuration.DependencyResolver = resolver;

I couldn't make it work and every change causes different errors.

Current code cause error below.

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Sequence contains no elements</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at Autofac.ParameterExtensions.ConstantValue[TParameter,TValue](IEnumerable`1 parameters, Func`2 predicate) at Autofac.ParameterExtensions.TypedAs[T](IEnumerable`1 parameters) at TestAPI.Bootstrapper.<SetAutofacWebAPIServices>b__0(IComponentContext c, IEnumerable`1 p) in d:\Projects\RadoreAPIs\TestAPI\Bootstrapper.cs:line 32 at Autofac.Builder.RegistrationBuilder.<>c__DisplayClass1`1.<ForDelegate>b__0(IComponentContext c, IEnumerable`1 p) at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
</StackTrace>
</Error>

Upvotes: 4

Views: 4905

Answers (1)

Peter Lillevold
Peter Lillevold

Reputation: 33910

First, your NLogModule will provide instances of ILogger so the NLogger (builder.RegisterType<NLogger>().As<ILogger>()) registration should not be necessary (and might cause trouble). Remove it.

Next, have you got the module to work without the fancy [Logger] attribute support?

Third, instead of using e.Component.Activator.LimitType as the "declaring type", use p.Member.DeclaringType from within the parameter lambda. Read up on my answer to this related question.

Upvotes: 7

Related Questions