Skip to content

Adds SerilogServiceCollectionExtensions #19

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

Conversation

Shazwazza
Copy link

Allows for more flexibility when configuring serilog services during startup. In some cases we don't want to use the serilog static Log and we aren't able to pre-create an ILogger before the HostBuilder is configured. We will create our own serilog logger during Configure Services and then we need to configure the rest of serilog services which isn't possible without copying all this code it would be nicer to just expose these methods so people can use them.

Allows for more flexibility when configuring serilog services during startup. In some cases we don't want to use the serilog static Log and we aren't able to pre-create an ILogger before the HostBuilder is configured. We will create our own serilog logger during Configure Services and then we need to configure the rest of serilog services which isn't possible without copying all this code it would be nicer to just expose these methods so people can use them.
using Serilog.Extensions.Logging;

namespace Serilog
{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

// Consumed by user code
collection.AddSingleton<IDiagnosticContext>(diagnosticContext);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


if (providers != null)
{
collection.AddSingleton<ILoggerFactory>(services =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shazwazza @nblumhardt do you mind renaming IServiceCollection collection to IServiceCollection services and services (IServiceProvider arg) to provider ? The current naming is confusing.

@nblumhardt
Copy link
Member

Hi Shannon, thanks for the PR 👍

RE the static Log.Logger, you can achieve what you are after by passing preserveStaticLogger: true to UseSerilog(). It's also not necessary to create an ILogger in advance - the UseSerilog() overload that accepts a delegate allows this to happen during service configuration.

But, there are some limitations at the moment you're probably hitting, namely, UseSerilog() doesn't provide access to the IServiceLocator for DI. I'm in the middle of getting a solution together for this, it would be great if you can take a look at the signature of the call in:

https://github.com/nblumhardt/serilog-reload/blob/dev/example/WebExample/Program.cs#L38

if you have a chance. Would having access to services in the UseSerilog() delegate, combined with preserveStaticLogger, do the job for you?

I'd be hesitant to expose AddSerilogServices() right now, at least until all of the pieces settle, though it's a good proposal to consider.

Cheers,
Nick

@Shazwazza
Copy link
Author

Hi @nblumhardt , for our scenario we have our own logger abstraction and serilog is it's implementation. During our own configuration logic we need to create a serilog logger and configure it and then pass it into our own dependency graph. This all occurs in ConfigureServices in our own ext method. Unfortunately we cannot use any of the current provided serilog ext methods for this scenario. The overload with the preserveStaticLogger doesn't allow us access to the created logger until all services are configured and we don't want to prematurely build the container to resolve that instance since that will end up being a different instance to what is resolved later.
For now we've just copied the underlying code of these ext methods and that is working fine, i just thought it might be nice to be able to call into the code directly instead. Also happy to wait until the pieces settle to see how things turn out.
Let me know if that does/doesn't make sense and I can provide an example of what we are doing.
Thanks
Shannon

@nblumhardt
Copy link
Member

Cool, thanks for the follow-up, Shannon 👍

@Shazwazza
Copy link
Author

Sure thing, let me know if/when you want me to adjust any of this code, or if you just want to close this and re-submit another PR down the road.

@nblumhardt
Copy link
Member

Thanks! Closing for now - still more changes to come in this area, #20 just shook up service registration a bit.

@nblumhardt nblumhardt closed this May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants