Introduction
Illustrates how to configure logging using the profiles functionality of the NServiceBus host.
Code walk-through
Change the default profile
The startup action for this sample is configured to use the Lite profile in the .
.
<PropertyGroup>
<StartAction>Program</StartAction>
<StartProgram>$(ProjectDir)$(OutputPath)NServiceBus.Host.exe</StartProgram>
<StartArguments>NServiceBus.Lite</StartArguments>
</PropertyGroup>
Logging helper
This is a simple helper that takes in a threshold as a parameter and configures logging based on that parameter.
static class LoggingHelper
{
public static void ConfigureLogging(Level threshold)
{
var layout = new PatternLayout
{
ConversionPattern = "%d %-5p %c - %m%n"
};
layout.ActivateOptions();
var appender = new ConsoleAppender
{
Layout = layout,
Threshold = threshold
};
appender.ActivateOptions();
BasicConfigurator.Configure(appender);
LogManager.Use<Log4NetFactory>();
}
}
Profile Handlers
Inside the LoggingHandlers
directory there are IConfigureLoggingForProfile
s handlers for each of the profiles. Note that they all implement IConfigureLoggingForProfile
.
Integration
class IntegrationLoggingHandler :
NServiceBus.Hosting.Profiles.IConfigureLoggingForProfile<Integration>
{
public void Configure(IConfigureThisEndpoint specifier)
{
LoggingHelper.ConfigureLogging(Level.Warn);
}
}
Lite
class LiteLoggingHandler :
NServiceBus.Hosting.Profiles.IConfigureLoggingForProfile<Lite>
{
public void Configure(IConfigureThisEndpoint specifier)
{
LoggingHelper.ConfigureLogging(Level.Info);
}
}
Production
class ProductionLoggingHandler :
NServiceBus.Hosting.Profiles.IConfigureLoggingForProfile<Production>
{
public void Configure(IConfigureThisEndpoint specifier)
{
LoggingHelper.ConfigureLogging(Level.Error);
}
}