Host Profile Logging

Component: NServiceBus Host
NuGet Package NServiceBus.Host (7.x)
Target NServiceBus Version: 6.x
Standard support for version 6.x of NServiceBus has expired. For more information see our Support Policy.

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

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

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);
    }
}

Related Articles


Last modified