问题
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:
- Downloaded from Nuget: Castle Windsor NLog integration
- Downloaded from Nuget: NLog Configuration
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" />
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.
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; } } } }
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