diff --git a/MessagingService/Program.cs b/MessagingService/Program.cs index b0d4136..bde93eb 100644 --- a/MessagingService/Program.cs +++ b/MessagingService/Program.cs @@ -29,12 +29,25 @@ public static IHostBuilder CreateHostBuilder(string[] args) { //At this stage, we only need our hosting file for ip and ports FileInfo fi = new(System.Reflection.Assembly.GetExecutingAssembly().Location); + + ConfigureLogging(); + + IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args); + hostBuilder.UseWindowsService(); + hostBuilder.UseLamar(); + hostBuilder.ConfigureLogging(logging => + { + logging.AddConsole(); + logging.AddNLog(); - IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(fi.Directory.FullName) - .AddJsonFile("hosting.json", optional: false) - .AddJsonFile("hosting.development.json", optional: true) - .AddEnvironmentVariables().Build(); + }); + hostBuilder.ConfigureWebHostDefaults(webBuilder => { ConfigureWebHost(webBuilder, fi.Directory.FullName); }); + + return hostBuilder; + } + + private static void ConfigureLogging() { String contentRoot = Directory.GetCurrentDirectory(); String nlogConfigPath = Path.Combine(contentRoot, "nlog.config"); @@ -48,64 +61,52 @@ public static IHostBuilder CreateHostBuilder(string[] args) }); b.LoadConfigurationFromFile(nlogConfigPath); }); + } - IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args); - hostBuilder.UseWindowsService(); - hostBuilder.UseLamar(); - hostBuilder.ConfigureLogging(logging => - { - logging.AddConsole(); - logging.AddNLog(); - - }).ConfigureWebHostDefaults(webBuilder => - { - webBuilder.ConfigureAppConfiguration((context, configBuilder) => - { - var env = context.HostingEnvironment; - - configBuilder.SetBasePath(fi.Directory.FullName) - .AddJsonFile("hosting.json", optional: true) - .AddJsonFile($"hosting.{env.EnvironmentName}.json", optional: true) - .AddJsonFile("/home/txnproc/config/appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"/home/txnproc/config/appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(); + private static void ConfigureWebHost(IWebHostBuilder webBuilder, + String basePath) { + webBuilder.ConfigureAppConfiguration((context, configBuilder) => + { + var env = context.HostingEnvironment; - // Build a snapshot of configuration so we can use it immediately (e.g. for Sentry) - var builtConfig = configBuilder.Build(); + configBuilder.SetBasePath(basePath) + .AddJsonFile("hosting.json", optional: true) + .AddJsonFile($"hosting.{env.EnvironmentName}.json", optional: true) + .AddJsonFile("/home/txnproc/config/appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"/home/txnproc/config/appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); - // Keep existing static usage (if you must), and initialise the ConfigurationReader now. - Startup.Configuration = builtConfig; - ConfigurationReader.Initialise(Startup.Configuration); + // Build a snapshot of configuration so we can use it immediately (e.g. for Sentry) + var builtConfig = configBuilder.Build(); - // Configure Sentry on the webBuilder using the config snapshot. - var sentrySection = builtConfig.GetSection("SentryConfiguration"); - if (sentrySection.Exists()) - { - // Replace the condition below if you intended to only enable Sentry in certain environments. - if (env.IsDevelopment() == false) - { - webBuilder.UseSentry(o => - { - o.Dsn = builtConfig["SentryConfiguration:Dsn"]; - o.SendDefaultPii = true; - o.MaxRequestBodySize = RequestSize.Always; - o.CaptureBlockingCalls = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "CaptureBlockingCalls", false); - o.IncludeActivityData = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "IncludeActivityData", false); - o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; - }); - } - } - }); + // Keep existing static usage (if you must), and initialise the ConfigurationReader now. + Startup.Configuration = builtConfig; + ConfigurationReader.Initialise(Startup.Configuration); - webBuilder.UseStartup(); - webBuilder.UseConfiguration(config); - webBuilder.UseKestrel(); - }); + // Configure Sentry on the webBuilder using the config snapshot. + var sentrySection = builtConfig.GetSection("SentryConfiguration"); + if (sentrySection.Exists()) + { + // Replace the condition below if you intended to only enable Sentry in certain environments. + if (env.IsDevelopment() == false) + { + webBuilder.UseSentry(o => + { + o.Dsn = builtConfig["SentryConfiguration:Dsn"]; + o.SendDefaultPii = true; + o.MaxRequestBodySize = RequestSize.Always; + o.CaptureBlockingCalls = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "CaptureBlockingCalls", false); + o.IncludeActivityData = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "IncludeActivityData", false); + o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; + }); + } + } + }); - - return hostBuilder; + webBuilder.UseStartup(); + webBuilder.UseKestrel(); } } }