3
votes

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

  1. Read the config file form external config file (i.e. log4net config is not within app.config)
  2. Watch the external config file so that any changes to the external config file are reflected.
2
Make sure that you add IWantCustomLogging to your endpoint config?Andreas Öhlund
I believe I did that in the second option but I will check one more time and let you know.amit_g
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "ApplicationName.log4net.xml", Watch = true)] has no impact with or without IWantCustomLogging. The log is not even directed to the desired file, so this setting has no impact.amit_g
Second way redirects the log which means it reads the external config file but there is no way (not that I know of) to specify the "Watch" attribute so log4net doesn't watch for the file. In this case the IWantCustomLogging is used on the endpoint config.amit_g
Third way, log4net seems to read the values from app.config file so it doesn't matter what I do in endpoint config but I have tried without IWantCustomLogging, with IWantCustomLogging, with a call to SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure); and without a call to SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);. The log is redirected as per the app.config setting but it is not watched for.amit_g

2 Answers

6
votes

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();
6
votes

NServiceBus >= 5.0 has a separate nugetPackage for log4Net: NServiceBus.Log4Net

Instead of calling: SetLoggingLibrary.Log4Net(x => ...);

use the following statements:

log4net.Config.XmlConfigurator.Configure();
LogManager.Use<Log4NetFactory>();

Or refer to this sample on how to programmatically configure the appenders: https://github.com/Particular/NServiceBus.Log4Net/blob/develop/src/Sample/LoggingConfig.cs