3

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.
4

2 回答 2

6

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();
于 2013-03-27T06:33:25.973 回答
6

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

于 2015-01-06T12:12:11.713 回答