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

Allow me to fully configure logging using Microsoft.Extensions.Configuration #294

Closed
bitbonk opened this issue May 23, 2019 · 8 comments
Closed
Labels

Comments

@bitbonk
Copy link

bitbonk commented May 23, 2019

Type: Feature request

In an application that is using the Microsoft generic host infrastructure (Microsoft.Extensions.Hosting) that comes with its own configuration system (Microsoft.Extensions.Configuration) it is currently not possible to use Microsoft.Extensions.Configuration to fully configure NLog logging.
I have to either use an nlog.config file or use the NLog.Config APIs to do the the configuration in code.

In our applications anything that needs to be configured must always be done through Microsoft.Extensions.Configuration, including but not limited to logging.

I am aware that there is the ConfigSettingsLayoutRender but since this is just a renderer its use is very limited.

Some examples of what we would like to be able to do in external configuration sources:

  1. Add a new logger with special filters.
  2. When the environment is Development, override the filter for one of the preconfigured loggers.
  3. When the environment is "SpecialTracing", override the filter for one of the preconfigured loggers. Set the loglevel for some loggers to tracing.
  4. A reusable analysis component that can be plugged into our applications should be able to read the logging configuration and then modify it in a generic way (i.e. using Microsoft.Extensions.Configuration without needing a dependency to NLog).
@snakefoot
Copy link
Contributor

Not sure I understand configuring logging without a dependency to NLog.

You can read appsettings.json and read the NLog-config like this:

                var config = new ConfigurationBuilder()
                    .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .Build();

                LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));

                var hostBuilder = new HostBuilder()

Extracted from:

https://github.com/NLog/NLog.Extensions.Logging/blob/master/examples/NetCore2/ConsoleExample/Program.cs

See also: https://github.com/NLog/NLog.Extensions.Logging/wiki/Json-NLog-Config

Think you have to show some code examples (or even better make a PR that implements your ideas).

@bitbonk
Copy link
Author

bitbonk commented May 23, 2019

Oops, that seems exactly what I was looking for. I somehow just didn’t manage to find it. 😊

I think at some point I even looked at that example but turned away because that sample still has that nlog.config XML file. Is that XML file needed for the example? Is it actually being loaded?

Also, is there a way to get rid of that global exception handler that calls LogManager.Shutdown() in the finally block?

We would like to wrap everything that is needed to configure and use NLog into a single extension method for IHostBuilder or preferably for IConfigurationBuilder.

The users of our application framework who chose to use Nlog would just call the extension methods once in their Program.cs on their IConfigurationBuilder and everything is preconfigured for them for nlog, including the default file loggers and filters.

@snakefoot
Copy link
Contributor

snakefoot commented May 23, 2019

I guess one could create a new NLogLoggingConfiguration-parameter that causes the NLogLoggerProvider to call LogManager.Shutdown() on Dispose():

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
LogFactory.Flush();
}
}

Instead of calling Flush().

@bitbonk
Copy link
Author

bitbonk commented May 23, 2019

Should this be done in the NLog repo? Or should I roll my own subclass? If I were to submit a PR for this, would it get accepted?

@snakefoot
Copy link
Contributor

If the default value of the new NLogLoggingConfiguration-parameter is NOT to shutdown, then I think it will be accepted if you create a PR for this repository.

@304NotModified
Copy link
Member

If the default value of the new NLogLoggingConfiguration-parameter is NOT to shutdown, then I think it will be accepted if you create a PR for this repository.

Can confirm that :)

@304NotModified
Copy link
Member

Extracted the todo into this: #313

@snakefoot
Copy link
Contributor

snakefoot commented Jul 20, 2019

@bitbonk NLog.Extension.Logging ver. 1.5.2 has been released that allows you to configure the option ShutdownOnDispose:

You can add it to your appsettings.json like this (or explicitly in code by initializing NLogProviderOptions):

{
    "Logging": {
        "NLog": {
            "shutdownOnDispose": true
        }
    }
}

See also #314

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

No branches or pull requests

3 participants