Skip to content

Instantly share code, notes, and snippets.

@Calabonga
Created June 1, 2024 01:21
Show Gist options
  • Save Calabonga/7e984ce7ed792c440019926fa7289475 to your computer and use it in GitHub Desktop.
Save Calabonga/7e984ce7ed792c440019926fa7289475 to your computer and use it in GitHub Desktop.
AppDefinitions for ASP.NET Core
/// <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);
}
}
}
/// <summary>
/// Environment variables reader from .env file
/// </summary>
public class DotNetEnvDefinition : AppDefinition
{
public override void ConfigureServices(WebApplicationBuilder builder)
{
builder.Configuration.AddDotNetEnv(options: LoadOptions.TraversePath());
}
}
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