pardahlman / RawRabbit

A modern .NET framework for communication over RabbitMq
MIT License
747 stars 144 forks source link

Adding Serilog to dotnet core Console application #219

Closed Towmeykaw closed 7 years ago

Towmeykaw commented 7 years ago

When i try to use DI to add the Serilog logger to my application i run into some issues. I have Serilog setup to log to a file but when i do this no RawRabbit logs are written

var serviceProvider = new ServiceCollection()
    .AddRawRabbit(new RawRabbitOptions
    {
        DependencyInjection = ioc => ioc
            .AddSingleton<ILoggerFactory, Logging.Serilog.LoggerFactory>()
    })
    .AddSingleton<IMessageSender, Sender>()
.BuildServiceProvider();

But if i do this then it works

IBusClient _client = RawRabbitFactory.CreateSingleton(new RawRabbitOptions
{           
    DependencyInjection = ioc => ioc.AddSingleton<ILoggerFactory, Logging.Serilog.LoggerFactory>()
});
var serviceProvider = new ServiceCollection()
    .AddSingleton(_client)
    .AddSingleton<IMessageSender, Sender>()
.BuildServiceProvider();

Don`t know if i am doing something wrong when setting it up? And thanks for a great library!

pardahlman commented 7 years ago

Hej @Towmeykaw 👋 - thanks for reporting this!

It looks like you are running the 2.0 branch - exciting! The code you provided looks great, I think the problem is how RawRabbit resolves it's class specific loggers.

Internal loggers are provided by LogManager, typically through a call like this:

_logger = LogManager.GetLogger<ChannelFactory>();

The idea is that during the registration of the bus client, the Log Manager gets assigned it's registered logging factory. Today it done here. It looks like that code is not being executed when registering the client through the IServiceCollection extension method.

This will be addressed in the next pre-release of 2.0. In the mean while, you can either do do what you proposed above, alternatively skip the registration of the logger factory and add register it in the log factory yourself

var serilog = new LoggerConfiguration().CreateLogger();
LogManager.CurrentFactory = new RawRabbit.Logging.Serilog.LoggerFactory(serilog);

Hope this helps!

Towmeykaw commented 7 years ago

Okay great! I will keep playing around and look forward to the next release 👍

pardahlman commented 7 years ago

Heya @Towmeykaw - this issue is fixed in beta6! Happy coding

secret-agent-B commented 5 years ago

@pardahlman I am doing something similar

DependencyInjection = ioc
                        => ioc.AddSingleton<ILoggerFactory, RawRabbit.Logging.Serilog.LoggerFactory>()

But the ILoggerFactory that is being implemented by RawRabbit.Logging.Serilog.LoggerFactory is not the same as the one in Microsoft.Extensions.Logging. Which ILoggerFactory should I be using then for this?

image

image

pajej-dev commented 5 years ago

Hey @raime910 I have the same problem. We would be greatfull if you could repeat @pardahlman