Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Serilog to dotnet core Console application #219

Closed
Towmeykaw opened this issue Apr 21, 2017 · 5 comments
Closed

Adding Serilog to dotnet core Console application #219

Towmeykaw opened this issue Apr 21, 2017 · 5 comments

Comments

@Towmeykaw
Copy link

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
Copy link
Owner

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
Copy link
Author

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

pardahlman added a commit that referenced this issue May 7, 2017
There are numerous ways to create a bus client, and at some point
the call to sett CurrentLogger needs to be made. We know that the
ConnectionFactory will allways be a singleton and always be resolved
when the client is resolved. It might be hacky, but it works
@pardahlman
Copy link
Owner

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

@secret-agent-B
Copy link

secret-agent-B commented Feb 22, 2019

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants