LANCommander/LANCommander.ServiceDefaults/Extensions.cs
2025-12-11 16:32:35 +11:00

150 lines
No EOL
5.7 KiB
C#

using LANCommander.ServiceDefaults.Logging;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ServiceDiscovery;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
namespace Microsoft.Extensions.Hosting;
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
private const string HealthEndpointPath = "/health";
private const string AlivenessEndpointPath = "/alive";
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();
builder.AddDefaultHealthChecks();
builder.Services.AddServiceDiscovery();
builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();
// Turn on service discovery by default
http.AddServiceDiscovery();
});
// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });
return builder;
}
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddStandardLogging();
builder.Services.AddOpenTelemetryDefaults(builder.Environment.ApplicationName, !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]));
return builder;
}
public static void AddStandardLogging(this ILoggingBuilder builder)
{
// Add console logging
builder.AddConsole();
builder.AddDebug();
// Add file logging to default Logs directory
var logsDirectory = Path.Combine(AppContext.BaseDirectory, "Logs");
builder.AddFileLogging(logsDirectory);
// Set default minimum level
builder.SetMinimumLevel(LogLevel.Information);
// Add common filters to reduce noise
builder.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
builder.AddFilter("Microsoft.AspNetCore.Components", LogLevel.Warning);
builder.AddFilter("AntDesign", LogLevel.Warning);
// Add OpenTelemetry logging
builder.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});
}
public static ILoggingBuilder AddFileLogging(this ILoggingBuilder builder, string logDirectory, LogLevel minimumLevel = LogLevel.Information)
{
if (!Directory.Exists(logDirectory))
Directory.CreateDirectory(logDirectory);
return builder.AddProvider(new FileLoggerProvider(logDirectory, minimumLevel));
}
public static void AddOpenTelemetryDefaults(this IServiceCollection services, string applicationName, bool useOtlpExporter)
{
services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(applicationName)
.AddAspNetCoreInstrumentation(options =>
// Exclude health check requests from tracing
options.Filter = context =>
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
)
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});
if (useOtlpExporter)
{
services.AddOpenTelemetry().UseOtlpExporter();
}
}
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
return builder;
}
public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks(HealthEndpointPath);
// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}
return app;
}
}