Created
June 1, 2024 01:21
-
-
Save Calabonga/7e984ce7ed792c440019926fa7289475 to your computer and use it in GitHub Desktop.
AppDefinitions for ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Show loaded configurations for ASP.NET Core or Web API application | |
/// </summary> | |
public class DebugConfigurationDefinition : AppDefinition | |
{ | |
#if DEBUG | |
public override bool Enabled => true; | |
#else | |
public override bool Enabled => false; | |
#endif | |
public override void ConfigureApplication(WebApplication app) => DisplaySettings(app.Configuration); | |
static void DisplaySettings(IConfiguration config) | |
{ | |
var index = 0; | |
var providers = config.GetChildren(); | |
foreach (var pair in providers) | |
{ | |
index++; | |
Console.WriteLine($"==>{index} {pair.Path} = {pair.Value}"); | |
DisplaySettings(pair); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Environment variables reader from .env file | |
/// </summary> | |
public class DotNetEnvDefinition : AppDefinition | |
{ | |
public override void ConfigureServices(WebApplicationBuilder builder) | |
{ | |
builder.Configuration.AddDotNetEnv(options: LoadOptions.TraversePath()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OpenTelemetryDefinition : AppDefinition | |
{ | |
public override void ConfigureServices(WebApplicationBuilder builder) | |
{ | |
builder.Services.AddOpenTelemetry() | |
.ConfigureResource(resource => resource.AddService("CalabongaBlog")) | |
.WithMetrics(metrics => | |
{ | |
metrics | |
.AddAspNetCoreInstrumentation() | |
.AddHttpClientInstrumentation(); | |
metrics.AddOtlpExporter(); | |
}) | |
.WithTracing(tracing => | |
{ | |
tracing | |
.AddAspNetCoreInstrumentation() | |
.AddHttpClientInstrumentation() | |
.AddEntityFrameworkCoreInstrumentation(); | |
tracing.AddOtlpExporter(); | |
}); | |
builder.Logging.AddOpenTelemetry(logging => logging.AddOtlpExporter()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment