Been getting a app that serves up data to a VBA application. Now at point trying to get configuration files (appsettings.json & appsettings.Development.Json). Right now it's a .net6.0 console app, later I'm making it an IconTrayApp.
Ran into a problem, the main Program.cs method gets the appsettings.json and the worker gets the appssettings.Developement.json. I understand why Main() get the none environment json, because I explicitly load it. But I want the app to use the same file both in an out of the IDE.
here is program.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PostDataService.InterfaceModels;
using Serilog;
using Serilog.Events;
using System;
namespace PostDataService
{
public class Program
{
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfiguration config = builder.Build();
string _logFQN = config["PostConfig:Basefolder"] + config["PostConfig:LogsFolder"] + config["PostConfig:LogFname"];
string _LogTemplate = config["PostConfig:LogTemplate"];
var loggerConfig = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Verbose)
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.WithEnvironmentUserName()
.WriteTo.File(_logFQN,
fileSizeLimitBytes: 524288000,
rollOnFileSizeLimit: true,
rollingInterval: RollingInterval.Day,
outputTemplate: _LogTemplate);
Log.Logger = loggerConfig.CreateLogger();
try
{
Log.Write(LogEventLevel.Information, "Start USPS Data Server");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.Information("USPS Server stopped");
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
IConfiguration configuration = hostContext.Configuration;
WorkerOptions options = configuration.GetSection("PostConfig").Get<WorkerOptions>();
services.AddSingleton(options);
services.AddHostedService<Worker>();
});
}
}
and here is appsettings.json (appsettings.Development.json)
{
"PostConfig": {
"BaseFolder": "./App_Data/",
"LogsFolder": "logs/",
"SystemFQN": "C:/Test/PostalDB/PostalData.db",
"CustFQN": "C:/Test/PostalDB/PostalCustomer.db",
"LogFname": "USPS_.log",
"LogTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}\t{EnvironmentUserName}\t[{Level:u4}]\t<{ThreadId}>\t{Message:lj}{NewLine}{Exception}"
}
}
I did try and use the .AddEnvironmentVariables() in the configuration builder and that did not seem to change things. I suspect because of the explicit addjsonfile call.



