NServiceBus and Log4Net external config file

log4net, nservicebus

Solution

If you're using the assembly attribute, use:

SetLoggingLibrary.Log4Net(); // no parameters

This should also work:

SetLoggingLibrary.Log4Net(() => XmlConfigurator.ConfigureAndWatch(log4netConfigFileInfo));

Or this:

XmlConfigurator.ConfigureAndWatch(log4netConfigFileInfo)
SetLoggingLibrary.Log4Net();

Problem

I prefer the external config files for log4net as the log4net can monitor those files and the level can be changed without chaning the app/web.config file. I am struggling a bit to do that in NServiceBus. I am using Here is my external config file ``` <?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="RollingFile" type="log4net.Appender.FileAppender"> <file value="C:\Logs\NServiceBusApplication.log" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="RollingFile" /> </root> </log4net> ``` The easiest way to do so is to place an attribute in AssemblyInfo.cs ``` [assembly: log4net.Config.XmlConfigurator(ConfigFile = "ApplicationName.log4net.xml", Watch = true)] ``` This doesn't work in NServiceBus (I am using NServiceBus.Host.exe). Then I tried calling ``` SetLoggingLibrary.Log4Net(() => XmlConfigurator.Configure(log4netConfigUri)); ``` before `Configure.With` in `EndpointConfig`. With this log4net reads the config file from the correct location, it doesn't watch for it, so when I change the log level, it isn't reflected in the app. Next I tried with appSettings ``` <appSettings> <add key="log4net.Config" value="ApplicationName.log4net.xml"/> <add key="log4net.Config.Watch" value="True"/> </appSettings> ``` Again, log4net reads this and subsequently reads the ApplicationName.log4net.xml, it still doesn't watch for it. How do I configure the log4net in NServiceBus service that is using NServiceBus.Host.exe to - Read the config file form external config file (i.e. log4net config is not within app.config) - Watch the external config file so that any changes to the external config file are reflected.

Original source