Ninject logger using NLog
Asked Answered
S

4

16

I've just started learning Ninject but have come across a problem with the logger. I've currently got a controller that has a service and logger injected into the constructor like so:

public ToolsController(IToolsService toolsService, ILogger logger)
{
    logger.Info("ToolsController Created");
    this.toolsService = toolsService;
    this.logger = logger;
}

The problem is on the logger.Info line (for example) in the constructor which seems to use the wrong logger, so the logger name it prints out is incorrect.

Tools.IGeocodeImporter: ToolsController Created 

Below is how it is setup to get the logger name:

kernel.Bind<ILogger>().To<Logger>().WithConstructorArgument("name", x => x.Request.ParentContext.Request.Service.FullName);

Any advice would be appreciated.

Scaler answered 6/12, 2012 at 16:47 Comment(3)
What logger name would you have expected?Legatee
The class name of wherever the logger is. Something on the lines of: ToolsController: ToolsController CreatedScaler
See also: blog.tonysneed.com/2011/10/09/…Cup
A
21

I use the following:

.Bind<ILog>().ToMethod(p => LogManager.GetLogger(
                   p.Request.Target.Member.DeclaringType));

To have the logger with the name of the class. I'm using Log4net, but I think the idea can be applied to any log as well: in fact binding to a method open you to any solution in order to create the needed instance.

Aile answered 6/12, 2012 at 17:6 Comment(1)
This answer helped me in making the correct code here: https://mcmap.net/q/721879/-ninject-logger-using-nlogDela
B
9

I use a ninject extension called Ninject.Extensions.Logging.NLog2, which can be found on NuGet or GitHub. It handles the binding of NLog, so you don't have to do anything else than just add it to your project and remove any bindings to NLog that you have right now.

Bearable answered 19/12, 2012 at 7:28 Comment(4)
Do you know if there are any tutorials available with this library?Eyebright
Check this link out blog.tonysneed.com/2011/10/09/…Milkwhite
Here is the link to the extension wiki github.com/ninject/ninject.extensions.logging/wikiGuardant
There's a new version, usable with Ninject 4.5.8+. Ninject.Extensions.Logging.NLog4.Lacquer
H
1

As Felice mentioned you can use the same method, However for Nlog you need a little bit change which is :

.Bind<ILogger>().ToMethod(p => NLog.LogManager.GetCurrentClassLogger(
                   p.Request.Target.Member.DeclaringType));
Handwork answered 27/2, 2018 at 5:53 Comment(2)
Seems to be working for me - certainly gets the logger constructed.Cretic
But unfortunately, this produces logger which name is constant and belongs to the class where this statement is written.Dela
R
1

I think at this moment the best way is to install extensions for your logger (for NLog):

Install-Package Ninject.Extensions.Logging.NLog4

and then in you code:

 using var kernel = new Ninject.StandardKernel(
                 new NinjectSettings(){ LoadExtensions = false }, 
                 new NLogModule());

new NinjectSettings(){LoadExtensions = false} - this element is required to properly load NLogModule.

and now you are ready to get new instance of ILogger:

public class PublicPropertyLoggerClass
{
    [Inject]
    public Ninject.Extensions.Logging.ILogger Logger { get; set; }
}

But if you don't like installing new nuget packages, you can still use:

_kernel.Bind<ILogger>().ToMethod(
          p => LogManager.GetLogger(
               p.Request.Target?.Member.DeclaringType.FullName 
                   ?? typeof(App).GetType().FullName));

Where the App is your startup class.

Answers based on: https://github.com/ninject/Ninject.Extensions.Logging/wiki/Using

Resurgent answered 19/1, 2021 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.