Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 

NServiceBus.MicrosoftLogging

Build status NuGet Status

Add support for NServiceBus to log to Microsoft.Extensions.Logging.

Already a Patron? skip past this section

Community backed

It is expected that all developers either become a Patron or have a Tidelift Subscription to use NServiceBusExtensions. Go to licensing FAQ

Sponsors

Support this project by becoming a Sponsor. The company avatar will show up here with a website link. The avatar will also be added to all GitHub repositories under the NServiceBusExtensions organization.

Patrons

Thanks to all the backing developers. Support this project by becoming a patron.

Support via TideLift

Support is available via a Tidelift Subscription.

Contents

NuGet package

https://nuget.org/packages/NServiceBus.MicrosoftLogging/ https://nuget.org/packages/NServiceBus.MicrosoftLogging.Hosting

Usage

var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(loggingBuilder =>
{
    loggingBuilder.AddFilter(level => level >= MsLogLevel.Information);
    loggingBuilder.AddConsole();
});

using var loggerFactory = new LoggerFactory();
var logFactory = LogManager.Use<MicrosoftLogFactory>();
logFactory.UseMsFactory(loggerFactory);
// endpoint startup and shutdown

snippet source | anchor

Usage when hosting

As LoggerFactory implements IDisposable it must be disposed of after stopping the NServiceBus endpoint. The process for doing this will depend on how the endpoint is being hosted.

In a generic host

Disposing the LoggerFactory is done by the underlying infrastructure.

var builder = Host.CreateDefaultBuilder();
builder.ConfigureLogging(logging => { logging.AddConsole(); });
// should go before any other Use or Configure method that uses NServiceBus
builder.UseMicrosoftLogFactoryLogging();

snippet source | anchor

Note: UseMicrosoftLogFactoryLogger requires adding NServiceBus.MicrosoftLogging.Hosting as a package dependency.

In a windows service

When hosting in a windows service LoggerFactory should be disposed of as part of the ServiceBase.OnStop execution.

using MsLogLevel = Microsoft.Extensions.Logging.LogLevel;
using MsLoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;

[DesignerCategory("Code")]
class ProgramService :
    ServiceBase
{
    IEndpointInstance? endpointInstance;
    Microsoft.Extensions.Logging.ILoggerFactory? loggerFactory;

    static void Main()
    {
        using var service = new ProgramService();
        if (ServiceHelper.IsService())
        {
            Run(service);
            return;
        }
        service.OnStart(null);
        Console.WriteLine("Bus started. Press any key to exit");
        Console.ReadKey();
        service.OnStop();
    }

    protected override void OnStart(string[]? args)
    {
        AsyncOnStart().GetAwaiter().GetResult();
    }

    async Task AsyncOnStart()
    {
        var services = new ServiceCollection();
        services.AddLogging(builder =>
        {
            builder.AddFilter(level => level >= MsLogLevel.Information);
            builder.AddConsole();
        });

        var provider = services.BuildServiceProvider();

        loggerFactory = provider.GetRequiredService<MsLoggerFactory>();
        var logFactory = LogManager.Use<MicrosoftLogFactory>();
        logFactory.UseMsFactory(loggerFactory);
        var endpointConfiguration = new EndpointConfiguration("EndpointName");
        endpointConfiguration.EnableInstallers();
        endpointInstance = await Endpoint.Start(endpointConfiguration);
    }

    protected override void OnStop()
    {
        AsyncOnStop().GetAwaiter().GetResult();
    }

    async Task AsyncOnStop()
    {
        if (endpointInstance != null)
        {
            await endpointInstance.Stop();
        }
        loggerFactory?.Dispose();
    }
}

snippet source | anchor

Security contact information

To report a security vulnerability, use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Icon

Abstract designed by Neha Shinde from The Noun Project.