using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
namespace Core.Blueprint.Logging.Configuration
{
///
/// Provides extension methods for configuring logging in the application.
///
public static class Registerblueprint
{
///
/// Registers logging services in the application.
///
/// The to add the services to.
/// The for accessing configuration and application setup.
/// The updated .
public static IServiceCollection AddLogs(this IServiceCollection services, WebApplicationBuilder builder)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.CreateLogger();
builder.Host.UseSerilog(Log.Logger);
services.AddScoped();
return services;
}
///
/// Configures middleware for logging and error handling in the application.
///
/// The used to configure the middleware pipeline.
/// The service settings required by the middleware.
public static void UseLogging(this IApplicationBuilder app, IConfiguration configuration)
{
var serviceSettings = new ServiceSettings();
configuration.GetSection(nameof(ServiceSettings)).Bind(serviceSettings);
app.UseCustomHttpLogging(serviceSettings);
app.UseHttpExceptionHandler(serviceSettings);
}
///
/// Adds middleware to handle HTTP exceptions globally.
///
/// The to add the middleware to.
/// The settings used by the exception handler middleware.
/// The updated .
public static IApplicationBuilder UseHttpExceptionHandler(this IApplicationBuilder builder, ServiceSettings settings)
{
return builder.UseMiddleware(settings);
}
///
/// Adds custom HTTP logging middleware to the application pipeline.
///
/// The to add the middleware to.
/// The settings used by the logging middleware.
/// The updated .
public static IApplicationBuilder UseCustomHttpLogging(this IApplicationBuilder builder, ServiceSettings settings)
{
return builder.UseMiddleware(settings);
}
}
}