'NLog with AutoFac - How to give logger name

I am using Autofac as DI tool in my project and NLog for the logging.

I am facing a problem to specify the logger name while using NLog with Autofac.

Here is the link to my code.

As you can see, In LoggerService.cs, line: 11

I am creating the instance of logger in constructor. How can I inject the logger object there and also get the logger name as the class name?

Any help will be appreciated.

Update: I have seen the question before. That was about wrong callsite info in logged message. I want to know how to inject logger in class with proper logger name.

Update: Adding relevant code in the question itself.

Global.asax.cs

using Autofac;
using Autofac.Integration.Mvc;
using System.Web.Mvc;
using System.Web.Routing;
using WebApplication2.Utils;

namespace WebApplication2
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            ConfigureAutofac();

            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        private void ConfigureAutofac()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterType<MailService>().As<IMailService>();

            //builder.RegisterModule<NLogModule>();

            builder.RegisterGeneric(typeof(LoggerService<>)).As(typeof(ILoggerService<>)).InstancePerDependency();

            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}

HomeController.cs

using System.Web.Mvc;
using WebApplication2.Utils;

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        public IMailService mailService { get; set; }
        public ILoggerService<HomeController> loggingService;

        public HomeController(IMailService mailService, ILoggerService<HomeController> loggingService)
        {
            this.mailService = mailService;
            this.loggingService = loggingService;
        }

        // GET: Home
        public ActionResult Index()
        {
            loggingService.Debug("Log message from index method");
            loggingService.Info("Some info log");

            mailService.Send();

            return View();
        }
    }
}

ILoggerService.cs

namespace WebApplication2.Utils
{
    public interface ILoggerService<T>
    {
        void Info(string message);

        void Debug(string message);
    }
}

LoggerService.cs

using NLog;

namespace WebApplication2.Utils
{
    public class LoggerService<T> : ILoggerService<T>
    {
        public ILogger logger { get; set; }

        public LoggerService()
        {
            logger = LogManager.GetLogger(typeof(T).FullName);
        }

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

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


Solution 1:[1]

Use RegisterGeneric while registering your services, Like below.

builder.RegisterGeneric(typeof(LoggerService<>)).As(typeof(ILoggerService<>)).InstancePerRequest();

Solution 2:[2]

you can always use LogManager to acquire a logger. looks like this

    private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mw509
Solution 2 Alan_Jin