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

Log DI container creation exceptions #8

Closed
tsvx opened this issue Dec 14, 2020 · 6 comments
Closed

Log DI container creation exceptions #8

tsvx opened this issue Dec 14, 2020 · 6 comments

Comments

@tsvx
Copy link
Contributor

tsvx commented Dec 14, 2020

For instance, the following approach would work in the Main() method:
Instead of CreateHostBuilder(args).Build().Run(); do

            Microsoft.Extensions.Logging.ILogger logger = null;
            try
            {
                var hostBuilder = CreateHostBuilder(args);
                var host = hostBuilder.Build();
                logger = (Microsoft.Extensions.Logging.ILogger)host.Services.GetService(typeof(Microsoft.Extensions.Logging.ILogger<Program>));
                host.Run();
            }
            catch (Exception ex)
            {
                logger?.LogCritical(ex, "Unhandled Exception, closing application.");
            }
drwatson1 pushed a commit that referenced this issue Feb 23, 2021
@tsvx
Copy link
Contributor Author

tsvx commented Feb 24, 2021

It has turned out that this approach is incomplete. We should dispose the logger, it turns out this is given in the documentation.

@drwatson1 drwatson1 reopened this Feb 27, 2021
@drwatson1
Copy link
Owner

You are right, thank you for pointing it out. I've decided to improve logging in the template based on the latest versions of Serilog extensions and documentation.

@drwatson1
Copy link
Owner

I've updated the template just now - https://marketplace.visualstudio.com/items?itemName=sergey-tregub.asp-net-core-restful-service-template.

Is the solution suitable for you?
Is there something else that I can improve?

@tsvx
Copy link
Contributor Author

tsvx commented Mar 3, 2021

Thank you, Sergey. Looks like what I wanted. I am definitely going to test this solution when I start a new ASP.NET Core project.

One note on the cafec9d commit, it seems that the ConfigureLogging(..) call is redundant after adding UseSerilog(..). Also, that call replaces Log.Logger which is supposed to be modified (or replaced) in the UseSerilog(..) call. Please correct me if I am wrong.

@drwatson1
Copy link
Owner

You are right as always :) I forgot to remove ConfigureLogging after UseSerilog was added.

drwatson1 pushed a commit that referenced this issue Mar 3, 2021
@drwatson1
Copy link
Owner

I close the issue now. Feel free to open a new issue if you find any other problems. Thank you for your contribution.

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

No branches or pull requests

2 participants