I have a custom appsettings JSON file that I need to be added to the configuration. I'm doing this in the ConfigureAppConfiguration hook called from CreateHostBuilder called from Main in Program.cs. I'm also using NLog.
public static void Main(string[] args)
{
//
// currently using nlog.config but I want to use appsettings but they haven't been "built" yet
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateHostBuilder(args).Build().Run();
// could move configurenlog call here, but already called "UseNLog()"
}
catch (Exception exception)
{
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) => {
// add custom config file
config
.AddJsonFile("customsettings.json", optional:true, reloadOnChange:true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog(); // NLog: Setup NLog for Dependency injection
Now I want to use the (combined) appsettings to configure nlog as outlined here, but how do I do this AFTER I've loaded my custom config settings?