NServiceBus doesn't play nicely with pre-existing nlog.config files
cross-posted from http://tech.groups.yahoo.com/group/nservicebus/message/17971
Problem:
I have some centralised nlog configuration files that we pull in via nuget - these determine targets and rules, and we then have build/deployment scripts that alter values for the different environments we deploy to. For instance, local dev gets trace logged to console, file and UDP. stage debug logs to file and splunk. prod info logs at different levels to different targets again. This means that I want my endpoint to pick up nlog configuration from our nuget source files. And this is where the problem lies.
I cannot specify Configure.Instance.Nlog() because I have to specify the target(s) - an ArgumentException is thrown otherwise. So I use the NLog API to read in my configuration file and pass in the targets. The problem I have is that the existence of my nlog.config files means i'm getting logs emitted once but picked up by two sets of rules. I see in the source code that NSB is appending a global rule to log everything above the logging threshold. Which means i now have two rules - one created by NSB and the other pulled from my nlog.config file.
Analysis:
NServiceBus.Logging.Loggers.NLogAdapter.NLogConfigurator.Configure method contains: var logLevel = LogLevelType.GetStaticField(levelForNServiceBusToLogWith ?? "Info", true); dynamic loggingRule = Activator.CreateInstance(LoggingRuleType, "*", logLevel, targetsForNServiceBusToLogTo.First()); foreach (dynamic target in targetsForNServiceBusToLogTo.Skip(1)){ loggingRule.Targets.Add(target); } loggingConfiguration.LoggingRules.Add(loggingRule); LogManagerType.SetStaticProperty("Configuration", (object)loggingConfiguration);
Any advice? In my case Ops are concerned with the logging target definitions (which change per environment) - so I need to retain the centralised nlog config definition - but i don't want the duplicate rules created.
Suggested resolution:
- Only add the new logging rule if there are not already explicit rules in NLog configuration.
- Allow NSB to be configured for NLog without specifying targets if an NLog configuration already exists.
Basically, if a user has already provided NLog configuration with targets and rules then assume they really care about the nlog configuration and just use that (and ignore any NServiceBus.Config.Logging logging level specified in app config).
You should be able to use SetLoggingLibrary.NLog(). This will not configure any targets.
yep, that works. thanks