Motivation
Castle Windsor comes with what it calls a “Logging Facility“. What the latter allows you to do is very quickly enable logging using a variety of ways but what is most interesting is the fact that the “Logging Facility” provides the necessary plumbing for swapping one type of logger for another. So for instance, you can change from log4net to NLog very easily by simply changing one line of code.
In fact, that’s what I had to do i.e. swap out log4net for NLog because I had started logging using the former but then found out that the latter might possibly be better.
While there are examples of how to use the “Logging Facility” with log4net, I haven’t found any with dealt specifically with NLog integration and Castle Windsor in detail. While the resources provided earlier do help and can be used to figure out how to get NLog integration, the information needed aggregating which this post attempts to address.
Set up Castle Windsor NLog integration
This is very straighforward if you use Nuget. Simply add the package “Castle Windsor NLog” to your project.
Also via Nuget install both “NLog Configuration” and “NLog Schema for Intellisense(TM)” to your project.
The former is how you will configure NLog i.e. where to create the log file, etc while the latter will provide intellisense while editing the configuration file.
So here’s how you might want to configure logging:
<?xml version="1.0" encoding="utf-8" ?> | |
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<!-- | |
See http://nlog-project.org/wiki/Configuration_file | |
for information on customizing logging rules and outputs. | |
--> | |
<targets> | |
<!-- add your targets here --> | |
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" | |
layout="${longdate}|${level:uppercase=true}|${logger}|${message}" /> | |
</targets> | |
<rules> | |
<!-- add your logging rules here --> | |
<logger name="*" minlevel="Trace" writeTo="f" /> | |
</rules> | |
</nlog> |
The next step is to ensure that CastleWindsor will use NLog to perform logging:
container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.NLog) .WithConfig("NLog.config"));
If let’s say you want to use log4net instead then instead simply use (assuming you’ve got the “log4.net.config” present):
container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net) .WithConfig("log4net.config"));
Now according to best practice advice to set up logging:
private ILogger _logger = NullLogger.Instance; | |
public ILogger Logger | |
{ | |
get { return _logger; } | |
set { _logger = value; } | |
} |
Finally, log whatever you want e.g.:
public ActionResult Send() | |
{ | |
_logger.Info("In Send method"); | |
... | |
} |
I’ve put together the source code here.
I’m a newbie in IoC-Frameworks. I’m trying to put up a demo-implementation similar to above example. Could I have more code about the calling class and its instantiation, please?
Somehow I’m missing the point: If I just create a class (called MyBiz) with the Logger-properties and instantiated it, the logger-property-value remains NullLogger and IsLevel-properites are false. Does The setting Logger-property need to be done explicitely or does Castle this for me? If yes, Then I suppose, the MyBiz needs to be somehow “registered” to. How then?
Hi Tomarikawa,
I will put up an example on GitHub and you will be able to see how it work from there. However, while waiting ensure you’ve got:
container.AddFacility(f => f.LogUsing(LoggerImplementation.NLog)
.WithConfig(“NLog.config”))
in your bootstrap code.
After I put up the example you’ll see better how it works.
David
Hi Tomarikawa,
Here is an example I’ve put together https://github.com/DavidSSL/CastleWindsorWithNlog?source=c. This should hopefully help you. Give me a shout if you have any issue. Also note that the code I’ve got is not how I would normally do things because everything is currently in one file . I’ve done it that way to keep things simple. Anyway, let me know how you get on. Oh and in case you’re wondering, the log file will be in bin/Debug/logs.
David
Great David, Your Example on https://github.com/DavidSSL/CastleWindsorWithNlog?source=c Works like a charm. The solution to my problem was quite what I expected, although I would not have figured it out myself.
Now it’s up to me, “the lazy bone” to read Castle-Documentation. Just code-examples are obviously not enough…
Thanks and all the best,
Tommi