I'm trying to get log4net integration for Castle Windsor working. I wrote my class with an public property of type ILogger
and took the configuration in my app.config like following.
<configuration>
<configsections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configsections>
<castle>
<facilities>
<facility id="logging" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging" loggingApi="log4net" />
</facilities>
<components>
<component id="form1" type="WinFormsActiveRecordSample.Form1, WinFormsActiveRecordSample" />
</components>
</castle>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="main.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd.MM.yy HH:mm:ss} %-5level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
In my eyes this should be working, but it doesn't. When I set loggingApi="console"
it logs correctly. When I change it to log4net it does nothing. The log4net configuration was taken from another project where the block is working. What do I have to do that the log file is used? Must there be a special log4net configuration?
Thanks for any hint
Boris
Following recent Castle conventions (correct me if wrong), I have solved it this way:
I have simply adapted the method provided here: http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Five-Adding-logging-support.ashx
and used the overload of UseLog4Net() that accepts an configuration file parameter.
Move your log4net configuration to a separate file log4net.config, then refer that file from the facility configuration:
If you want to have your log4net configuration in a section of your app.config:
then register the facility as:
Note that you can use the following in recent Castle versions:
This will use Log4net for logging and search for the log4net config section in the application configuration file.
I was missing the following NuGet package.
Castle.Windsor-log4net
Fixed it.
This is the entire configuration for the sample given here Home » MicroKernel/Windsor » Getting Started » Part 1 - The basics
The mistake I did was (as you can see the sections commented in the config file), tried to assign the Logger property once again in my class by registering the component in castle. this is not required as Castle does this automatically for you.
Cheers Badal
You can use App.config for your log4net configuration without creating a custom logging factory. Simply provide the App.config file as an argument to the
LoggingFacility
constructor:Unlike Mauricio Scheffer's answer, the default factory does
ConfigureAndWatch
. This works just fine for me with the App.config file, though I'm not running on IIS or anything else that restricts permissions.I'm doing this in code because you can't reliably use the Windsor Xml configuration to load log4net configuration from App.config. This is because the location of App.config can be modified when creating a new
AppDomain
.Using my solution means the log file configuration will be compiled into your code. But you can mitigate this by using a Windsor Installer to configure logging, and specify the installer (or installer assembly) from the App.config file:
...
If in the future (maybe in your test cases) you have to load the logging configuration from a different file, and can't or don't want to recompile, simply change the Xml to point to Windsor Installers in a different assembly: