Skip to content

NServiceBusExtensions/NServiceBus.MicrosoftLogging

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NServiceBus.MicrosoftLogging

Build status NuGet Status

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

See Milestones for release notes.

Already a Patron? skip past this section

Community backed

It is expected that all developers either become a Patron 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.

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

Icon

Abstract designed by Neha Shinde from The Noun Project.

About

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

Resources

License

Security policy

Stars

Watchers

Forks

Languages