Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 70 additions & 60 deletions CallbackHandler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
.AddJsonFile("hosting.development.json", optional: true)
.AddEnvironmentVariables().Build();

String contentRoot = Directory.GetCurrentDirectory();
String nlogConfigPath = Path.Combine(contentRoot, "nlog.config");

LogManager.Setup(b =>
{
b.SetupLogFactory(setup =>
{
setup.AddCallSiteHiddenAssembly(typeof(NlogLogger).Assembly);
setup.AddCallSiteHiddenAssembly(typeof(Shared.Logger.Logger).Assembly);
setup.AddCallSiteHiddenAssembly(typeof(TenantMiddleware).Assembly);
});
b.LoadConfigurationFromFile(nlogConfigPath);
});
ConfigureLogging();


IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);
Expand All @@ -68,56 +56,78 @@ public static IHostBuilder CreateHostBuilder(string[] args)
logging.AddNLog();

});
hostBuilder.ConfigureWebHostDefaults(webBuilder =>
{
// Configure per-environment configuration and build a snapshot so we can read settings here.
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();

// Build a snapshot of configuration so we can use it immediately (e.g. for Sentry)
var builtConfig = configBuilder.Build();

// Keep existing static usage (if you must), and initialise the ConfigurationReader now.
Startup.Configuration = builtConfig;
ConfigurationReader.Initialise(Startup.Configuration);

// 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 = bool.Parse(builtConfig["SentryConfiguration:CaptureBlockingCalls"]);
o.IncludeActivityData = bool.Parse(builtConfig["SentryConfiguration:IncludeActivityData"]);
o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown";
});
}
}
});

webBuilder.UseStartup<Startup>();
webBuilder.UseConfiguration(config);
webBuilder.UseKestrel();
});
ConfigureWebHost(hostBuilder, fi.Directory.FullName, config);

return hostBuilder;
}

private static void ConfigureWebHost(IHostBuilder hostBuilder,
String basePath,
IConfigurationRoot config) {
hostBuilder.ConfigureWebHostDefaults(webBuilder =>
{
// Configure per-environment configuration and build a snapshot so we can read settings here.
webBuilder.ConfigureAppConfiguration((context, configBuilder) =>
{
IWebHostEnvironment env = context.HostingEnvironment;

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();

Startup.Configuration = configBuilder.Build();
ConfigurationReader.Initialise(Startup.Configuration);

// Configure Sentry on the webBuilder using the config snapshot.
ConfigureSentry(env, webBuilder);
});

webBuilder.UseStartup<Startup>();
webBuilder.UseConfiguration(config);
webBuilder.UseKestrel();
});
}

private static void ConfigureLogging() {
String contentRoot = Directory.GetCurrentDirectory();
String nlogConfigPath = Path.Combine(contentRoot, "nlog.config");

LogManager.Setup(b =>
{
b.SetupLogFactory(setup =>
{
setup.AddCallSiteHiddenAssembly(typeof(NlogLogger).Assembly);
setup.AddCallSiteHiddenAssembly(typeof(Shared.Logger.Logger).Assembly);
setup.AddCallSiteHiddenAssembly(typeof(TenantMiddleware).Assembly);
});
b.LoadConfigurationFromFile(nlogConfigPath);
});
}

private static void ConfigureSentry(IWebHostEnvironment env,
IWebHostBuilder webBuilder) {
var sentrySection = Startup.Configuration.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 = Startup.Configuration["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";
});
}
}
}
}
}
Loading