Host Profile Logging

Component: NServiceBus Host | Nuget: NServiceBus.Host (Version: 7.x)
Target NServiceBus Version: 6.x

Code walk-through

Illustrates how to configure logging using the profiles functionality of the NServiceBus host.

Change the default profile

The startup action for this sample is configured to use the Lite profile in the .csproj.

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

Edit
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 IConfigureLoggingForProfiles handlers for each of the profiles. Note that they all implement IConfigureLoggingForProfile<T>.

Integration

Edit
class IntegrationLoggingHandler :
    NServiceBus.Hosting.Profiles.IConfigureLoggingForProfile<Integration>
{
    public void Configure(IConfigureThisEndpoint specifier)
    {
        LoggingHelper.ConfigureLogging(Level.Warn);
    }
}

Lite

Edit
class LiteLoggingHandler :
    NServiceBus.Hosting.Profiles.IConfigureLoggingForProfile<Lite>
{
    public void Configure(IConfigureThisEndpoint specifier)
    {
        LoggingHelper.ConfigureLogging(Level.Info);
    }
}

Production

Edit
class ProductionLoggingHandler :
    NServiceBus.Hosting.Profiles.IConfigureLoggingForProfile<Production>
{
    public void Configure(IConfigureThisEndpoint specifier)
    {
        LoggingHelper.ConfigureLogging(Level.Error);
    }
}

Related Articles

  • Logging
    Manage and integrate with NServiceBus logging.

Last modified