This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace StartupHelpers;
|
||||
@@ -7,12 +8,33 @@ namespace StartupHelpers;
|
||||
public static class EnvironmentDiagnostics
|
||||
{
|
||||
public static void LogEnvironmentSettings(ILogger logger, IConfiguration configuration, IWebHostEnvironment environment)
|
||||
{
|
||||
LogEnvironmentSettingsCore(logger, configuration, environment.ApplicationName, environment.EnvironmentName,
|
||||
environment.ContentRootPath, environment.WebRootPath);
|
||||
}
|
||||
|
||||
public static void LogEnvironmentSettings(ILogger logger, IConfiguration configuration, IHostEnvironment environment)
|
||||
{
|
||||
LogEnvironmentSettingsCore(logger, configuration, environment.ApplicationName, environment.EnvironmentName,
|
||||
environment.ContentRootPath, webRootPath: null);
|
||||
}
|
||||
|
||||
private static void LogEnvironmentSettingsCore(
|
||||
ILogger logger,
|
||||
IConfiguration configuration,
|
||||
string applicationName,
|
||||
string environmentName,
|
||||
string contentRootPath,
|
||||
string? webRootPath)
|
||||
{
|
||||
logger.LogInformation("==================== ENVIRONMENT SETTINGS ====================");
|
||||
logger.LogInformation("Application Name: {ApplicationName}", environment.ApplicationName);
|
||||
logger.LogInformation("Environment Name: {EnvironmentName}", environment.EnvironmentName);
|
||||
logger.LogInformation("Content Root Path: {ContentRootPath}", environment.ContentRootPath);
|
||||
logger.LogInformation("Web Root Path: {WebRootPath}", environment.WebRootPath);
|
||||
logger.LogInformation("Application Name: {ApplicationName}", applicationName);
|
||||
logger.LogInformation("Environment Name: {EnvironmentName}", environmentName);
|
||||
logger.LogInformation("Content Root Path: {ContentRootPath}", contentRootPath);
|
||||
if (!string.IsNullOrEmpty(webRootPath))
|
||||
{
|
||||
logger.LogInformation("Web Root Path: {WebRootPath}", webRootPath);
|
||||
}
|
||||
|
||||
logger.LogInformation("-------------- Environment Variables --------------");
|
||||
var envVars = Environment.GetEnvironmentVariables();
|
||||
|
||||
@@ -44,6 +44,22 @@ public static class StartupExtensions
|
||||
});
|
||||
}
|
||||
|
||||
public static void ConfigureJsonSerilog(this HostApplicationBuilder builder, string serviceName, string appVersion)
|
||||
{
|
||||
builder.Services.AddSerilog((services, configuration) =>
|
||||
{
|
||||
configuration
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
.ReadFrom.Services(services)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithMachineName()
|
||||
.Enrich.WithEnvironmentName()
|
||||
.Enrich.WithProperty("Service", serviceName)
|
||||
.Enrich.WithProperty("AppVersion", appVersion)
|
||||
.WriteTo.Console(new Serilog.Formatting.Json.JsonFormatter());
|
||||
});
|
||||
}
|
||||
|
||||
public static void AddAzureKeyVaultIfConfigured(this WebApplicationBuilder builder)
|
||||
{
|
||||
var keyVaultUri = builder.Configuration["KeyVault:VaultUri"];
|
||||
@@ -131,6 +147,21 @@ public static class StartupExtensions
|
||||
}
|
||||
}
|
||||
|
||||
public static void LogHostStartupDiagnostics(this IHost host, string serviceName)
|
||||
{
|
||||
var logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger(serviceName);
|
||||
logger.LogInformation("{Service} starting up...", serviceName);
|
||||
var environment = host.Services.GetRequiredService<IHostEnvironment>();
|
||||
logger.LogInformation("Environment: {Environment}", environment.EnvironmentName);
|
||||
|
||||
var configuration = host.Services.GetRequiredService<IConfiguration>();
|
||||
var logEnvironmentOnStartup = configuration.GetValue("LogEnvironmentOnStartup", defaultValue: true);
|
||||
if (logEnvironmentOnStartup)
|
||||
{
|
||||
EnvironmentDiagnostics.LogEnvironmentSettings(logger, configuration, environment);
|
||||
}
|
||||
}
|
||||
|
||||
public static void UseDefaultSerilogRequestLogging(this WebApplication app, bool includeProxyHeaders = false)
|
||||
{
|
||||
app.UseSerilogRequestLogging(options =>
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
<PackageReference Include="DotNetEnv" Version="3.2.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Email" Version="4.2.1" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.1.7" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user