Add support for NServiceBus to log to Microsoft.Extensions.Logging.
See Milestones for release notes.
Already a Patron? skip past this section
It is expected that all developers either become a Patron to use NServiceBusExtensions. Go to licensing FAQ
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.
Thanks to all the backing developers. Support this project by becoming a patron.
https://nuget.org/packages/NServiceBus.MicrosoftLogging/ https://nuget.org/packages/NServiceBus.MicrosoftLogging.Hosting
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
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.
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();
Note: UseMicrosoftLogFactoryLogger
requires adding NServiceBus.MicrosoftLogging.Hosting
as a package dependency.
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();
}
}
Abstract designed by Neha Shinde from The Noun Project.