Developer 02 197db1e08b refactor: Entfernen des App Loggers und Implementierung des ILogger-Interfaces; Konfiguration der API für NLog
- App Logger entfernt und durch die Implementierung des `ILogger`-Interfaces ersetzt, um eine konsistente Logging-Architektur zu gewährleisten.
- API für die Nutzung von NLog konfiguriert, um eine leistungsstarke und flexible Logging-Lösung bereitzustellen.
- Konfigurationsdateien und Setup-Anpassungen für die Integration von NLog in die API vorgenommen.
2024-08-27 19:41:12 +02:00

41 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace HRD.WebApi.Helpers
{
public static class ExceptionExtension
{
public static void AddCustomExceptionHandling(this IApplicationBuilder app)
{
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "application/json";
var errorFeature = context.Features.Get<IExceptionHandlerFeature>();
if (errorFeature != null)
{
HttpErrorDetails customError = new HttpErrorDetails
{
URI = context.Request.Path.ToUriComponent(),
Status = context.Response.StatusCode,
Detail = errorFeature.Error.Message
};
var loggerFactory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
ILogger logger = loggerFactory.CreateLogger("ExceptionExtension");
logger.LogError(errorFeature.Error, "An error occurred while processing the request. URI: {RequestUri}", customError.URI);
await context.Response.WriteAsync(JsonConvert.Serialize(customError)).ConfigureAwait(false);
}
});
});
}
}
}