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

Improve the way for combining appsettings.json with nlog.config #265

Closed
snakefoot opened this issue Mar 11, 2019 · 3 comments
Closed

Improve the way for combining appsettings.json with nlog.config #265

snakefoot opened this issue Mar 11, 2019 · 3 comments

Comments

@snakefoot
Copy link
Contributor

snakefoot commented Mar 11, 2019

Right now one one have to reload NLog.config twice. When loading NLog as first thing at startup like this:

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

Because ConfigSetting is not initially available:

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

Configuration = builder.Build();

ConfigSettingLayoutRenderer.DefaultConfiguration = Configuration;

// Loads NLog.config one more time
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

Could be nice if we could have a more fluent interface. Maybe like this:

var config = builder.Build();
var logger = NLog.LogManager.LogFactory
   .WithConfigSettings(config)     // Assigns DefaultConfiguration
   .WithLoggingConfiguration("nlog.config")   // Register NLog.Web.AspNetCore + NLog.Extension.Logging
   .GetCurrentClassLogger();

See also NLog/NLog#3097 and see also https://github.com/NLog/NLog/wiki/Reinitialize-NLog-configuration

@snakefoot
Copy link
Contributor Author

snakefoot commented Nov 3, 2019

NLog.Extension.Logging ver. 1.6.0 inludes #344 that improves on this by adding these extensions methods to ILoggerBuilder:

public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration)
public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, string configFileRelativePath)

They have the special behavior, that they assign/load the NLog-config AFTER having registered ${configsetting}. This should make config-lookup during target-initialization less error-prone.

Ground work is also being prepared for creating a fluent-config-api for the NLog LogFactory. See also NLog/NLog#3686 (Making NLog.Web.NLogBuilder.ConfigureNLog obsolete)

@snakefoot
Copy link
Contributor Author

snakefoot commented Mar 30, 2020

Made a small step with #418 and NLog/NLog.Web#540 (Waiting for NLog 4.7.1)

@snakefoot
Copy link
Contributor Author

NLog.Web.AspNetCore ver 4.9.3 has been released, and now you can do this:

var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();

That replaces the old style:

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

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

2 participants