- 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.
55 lines
2.0 KiB
C#
55 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HRD.WebApi.DAL.Middleware
|
|
{
|
|
/// <summary>
|
|
/// Gets the current API resource name from HTTP context
|
|
/// </summary>
|
|
/// <param name="httpContext">The HTTP context</param>
|
|
/// <returns>The current resource name if available, otherwise an empty string</returns>
|
|
|
|
public class WebApiMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
private readonly ILogger<WebApiMiddleware> _logger;
|
|
|
|
public WebApiMiddleware(RequestDelegate next, ILogger<WebApiMiddleware> logger)
|
|
{
|
|
_next = next;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext httpContext)
|
|
{
|
|
try
|
|
{
|
|
var loglevel = WebApiConfig.GetMonitoringWebRequestLevel(httpContext.Request.Method, httpContext.Request.Path.Value);
|
|
if (loglevel == LogLevel.Information
|
|
|| loglevel == LogLevel.Warning
|
|
|| loglevel == LogLevel.Error
|
|
)
|
|
{
|
|
var requestReader = new StreamReader(httpContext.Request.Body);
|
|
var requestContent = await requestReader.ReadToEndAsync();
|
|
if (string.IsNullOrEmpty(requestContent))
|
|
{
|
|
requestContent = $"{httpContext.Request.Method} {httpContext.Request.Path.Value}";
|
|
}
|
|
|
|
if (loglevel == LogLevel.Information) { _logger.LogInformation(requestContent); }
|
|
if (loglevel == LogLevel.Warning) { _logger.LogWarning(requestContent); }
|
|
if (loglevel == LogLevel.Error) { _logger.LogError(requestContent); }
|
|
}
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
//throw;
|
|
}
|
|
|
|
await _next(httpContext).ConfigureAwait(false);
|
|
}
|
|
}
|
|
} |