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

Move NLog config into appsettings.json, or alternatively, add extension method for ConfigurationBuilder to load NLog config #29

Closed
apawsey opened this issue Apr 13, 2016 · 14 comments

Comments

@apawsey
Copy link

apawsey commented Apr 13, 2016

.config files have been mostly dropped from .net core, in favour of the appsettings.json file. Sections can be added to the file, and read into a strongly typed class used in your configuration (example http://www.codeproject.com/Tips/1005242/Using-nLog-with-ASP-NET-vNext). Alternatively you could keep the .config file, and add an extension method for ConfigurationBuilder, to automatically read and process it. However then you would need to get the NLog config from the IConfigurationRoot I believe (still checking this part out).

@304NotModified
Copy link
Member

304NotModified commented Apr 13, 2016

Alternatively you could keep the .config file, and add an extension method for ConfigurationBuilder, to automatically read and process it

Can you elaborate on this?

We prefer moving to json but everything is on hold as we are waiting for rc2 of Microsoft.

useful links:

@jacksontbryan
Copy link

I think I can elaborate. It should be possible to do this:

https://github.com/nlog/NLog/wiki/Configuration-API

This method only allows loading from XML file:

private static void ConfigureNLog(string fileName)

We should add a method to load from a LoggingConfiguration as well:
https://github.com/NLog/NLog/blob/ab61daac7a8b9a82a9f37e497580234a10b5249c/src/NLog/Config/LoggingConfiguration.cs

And possibly add another extension that can load from appsettings.json

@304NotModified
Copy link
Member

We should add a method to load from a LoggingConfiguration as well:

Well you can use LogManager.Configuration to set configuration programmatically.

@304NotModified
Copy link
Member

I like to work on this, but it's a bit unclear to me what the extension method on ConfigurationBuilder should do

@304NotModified 304NotModified added this to the 1.0 milestone Jun 28, 2016
@alosman24
Copy link

are you still looking for help on this? I could possibly do this for you

@304NotModified
Copy link
Member

Yes, that would be great!

@304NotModified
Copy link
Member

closing this in favor of NLog/NLog#1588

think I can elaborate. It should be possible to do this:

https://github.com/nlog/NLog/wiki/Configuration-API

This method only allows loading from XML file:
NLog.Extensions.Logging/src/NLog.Extensions.Logging/AspNetExtensions.cs
Line 59 in 3e085c7
private static void ConfigureNLog(string fileName)
We should add a method to load from a LoggingConfiguration as well:
https://github.com/NLog/NLog/blob/ab61daac7a8b9a82a9f37e497580234a10b5249c/src/NLog/Config/LoggingConfiguration.cs

This one is available currently

@MacKinley
Copy link

MacKinley commented Dec 26, 2018

I know the discussion in moved to NLog/NLog#1588 but a different solution to consider is giving the NLog.Config.LoggingConfiguration class a constructor which takes a instance of some sort of NLog.Config.IConfiguration interface. This way NLog just expects a instance of a implementing class and allows the user to decided where that config actually comes from (json, xml, db, code etc.).

A relevant example for this solution for this Issue #29 would having something like this in Startup.cs:

// where the NLogConfigSection class implements NLog.Config.IConfiguration
var nLogConfigSection = Configuration.GetSection("Logging:NLog:Config")
                .Get<NLogConfigSection>();
var nLogConfig = new NLog.Config.LoggingConfiguration(nLogConfigSection);
NLog.LogManager.Configuration = nLogConfig;

There could even be a basic class implementing NLog.Config.IConfiguration build into NLog that just has the getters and setters that could be used in the example above so the user wouldn't have to create the NLogConfigSection class (as long as their appsettings.json section is structured to match the NLog implementation).

@snakefoot
Copy link
Contributor

snakefoot commented Dec 26, 2018

@MacKinley I like that LoggingConfiguration is simple and doesn't include a parser of configuration. You are welcome to review NLog/NLog#2891 and come with ideas for how to improve the interface of LoggingConfigurationParser (That can load configuration from ILoggingConfigurationElement)

When NLog 4.6 is out then I will release a PullRequest for NLog.Extensions.Logging that makes it possible to load config from appsettings.json

@snakefoot
Copy link
Contributor

Created #263 for people who would like to test NLog-config from appsettings.json. Depends on NLog 4.6-rc1

@joegithub
Copy link

@snakefoot I checked out #263 and like where you're headed, but I'm wondering if you're doing ".AddJsonFile(...)" and passing "appsettings.json", how would we be able to handle a scenario where we deploy to multiple environments (with different settings needed for log level, file path, etc...) and have a "appsettings.Development.json" and "appsettings.Staging.json"? Is there another way you would recommend handing this? Thanks

@snakefoot
Copy link
Contributor

@joegithub I'm probably one of the worst persons to ask about how to configure NetCore-application using appsettings.json. Never developed any NetCore applications besides console test-applications.

But I have seen code that merges multiple appsettings-files like this:

var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();

This should merge the settings from the different appsettings.json files. Not sure if it makes sense to "merge" nlog-config-sections together.

But you have the option to reference parameters from a different section (Ex. connectionstring-parameters) using the NLog ${configsetting} Ex. ${configsetting:item=connectionstrings.mydatabase}

The environment-specific parameters could then be placed in the environment-specific-file (Ex. appsettings.Production.json), and be merged into the official configuration.

@snakefoot
Copy link
Contributor

snakefoot commented Apr 30, 2019

NLog.Extension.Logging ver. 1.5.0 have now been released with support for NLog-config in appsettings.json:

https://www.nuget.org/packages/NLog.Extensions.Logging

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

@snakefoot
Copy link
Contributor

@joegithub Looks like you are not the only one that likes to merge appsettings-files together depending on environment. If you have any input then you can raise it here: #293

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

7 participants