Nlog 3.1 with Castle Windsor not logging

喜欢而已 提交于 2019-12-24 00:35:36

问题


I'm trying to use NLog (3.1) with Windsor Castle Facility, but it's not working for me (no errors, nothing happens)

These are my steps so far:

  1. Downloaded from Nuget: Castle Windsor NLog integration
  2. Downloaded from Nuget: NLog Configuration
  3. Updates nlog config like this:

    <target xsi:type="File" name="f" fileName="d:\nlog.log"
            layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
    

    <logger name="*" minlevel="Trace" writeTo="f" />
    

  4. Added Windsor Installer

     public class LoggingInstaller : IWindsorInstaller
     {
            public void Install(IWindsorContainer container, IConfigurationStore store)
            {
                container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog).WithConfig("NLog.config"));
            }
    }
    

    Which I'm calling it (I've checked that a breakpoint in there is being hit.

  5. Added the logger class like this:

    namespace WebApi.App_Start
    {
        public class MyLogger
        {
            private ILogger logger = NullLogger.Instance;
    
            public ILogger Logger
            {
                get { return logger; }
                 set { logger = value; }
            }
        }
    }
    
  6. Using it in a controller like this:

    new MyLogger().Logger.Info("New Request Created.");
    

But I don't see the file created.

Any step missing?

Thanks in advance. Guillermo.


回答1:


You need to put the Logger property to every class where you want to log something and the instances of the class has to be created/managed by Windsor.

So you need to add it to your controller:

public class MyController : Controller
{
    private ILogger logger = NullLogger.Instance;

    public ILogger Logger
    {
        get { return logger; }
        set { logger = value; }
    }

    public ActionResult MyAction() 
    {
         Logger.Info("New Request Created.");
    }
}

It was not working with the MyLogger because that class was not managed/created by Windsor so it was not injected your Logger property. But because the controller isntances are created by Windsor and ILogger inejtion is wokring in them: Windsor Tutorial - Part Five - Adding logging support



来源:https://stackoverflow.com/questions/27583610/nlog-3-1-with-castle-windsor-not-logging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!