71 lines
3.2 KiB
C#
71 lines
3.2 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
|
|
namespace Core.Blueprint.Logging.Configuration
|
|
{
|
|
/// <summary>
|
|
/// Provides extension methods for configuring logging in the application.
|
|
/// </summary>
|
|
public static class Registerblueprint
|
|
{
|
|
/// <summary>
|
|
/// Registers logging services in the application.
|
|
/// </summary>
|
|
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
|
/// <param name="builder">The <see cref="WebApplicationBuilder"/> for accessing configuration and application setup.</param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
|
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<ILoggerProvider, LoggerProvider>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures middleware for logging and error handling in the application.
|
|
/// </summary>
|
|
/// <param name="app">The <see cref="IApplicationBuilder"/> used to configure the middleware pipeline.</param>
|
|
/// <param name="serviceSettings">The service settings required by the middleware.</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds middleware to handle HTTP exceptions globally.
|
|
/// </summary>
|
|
/// <param name="builder">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
|
|
/// <param name="settings">The settings used by the exception handler middleware.</param>
|
|
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
|
|
public static IApplicationBuilder UseHttpExceptionHandler(this IApplicationBuilder builder, ServiceSettings settings)
|
|
{
|
|
return builder.UseMiddleware<HttpErrorMiddleware>(settings);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds custom HTTP logging middleware to the application pipeline.
|
|
/// </summary>
|
|
/// <param name="builder">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
|
|
/// <param name="settings">The settings used by the logging middleware.</param>
|
|
/// <returns>The updated <see cref="IApplicationBuilder"/>.</returns>
|
|
public static IApplicationBuilder UseCustomHttpLogging(this IApplicationBuilder builder, ServiceSettings settings)
|
|
{
|
|
return builder.UseMiddleware<HttpLoggingMiddleware>(settings);
|
|
}
|
|
}
|
|
}
|