52 lines
2.0 KiB
C#
52 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
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;
|
|
|
|
public WebApiMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext httpContext)
|
|
{
|
|
try
|
|
{
|
|
var loglevel = WebApiConfig.GetMonitoringWebRequestLevel(httpContext.Request.Method, httpContext.Request.Path.Value);
|
|
if (loglevel == AppLogger.EN_LoggingLevel.Info
|
|
|| loglevel == AppLogger.EN_LoggingLevel.Warn
|
|
|| loglevel == AppLogger.EN_LoggingLevel.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}";
|
|
}
|
|
AppLogger.ILoggerManager logger = new AppLogger.LoggerManager();
|
|
if (loglevel == AppLogger.EN_LoggingLevel.Info) { logger.LogInfo(requestContent); }
|
|
if (loglevel == AppLogger.EN_LoggingLevel.Warn) { logger.LogWarn(requestContent); }
|
|
if (loglevel == AppLogger.EN_LoggingLevel.Error) { logger.LogError(requestContent); }
|
|
}
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
//throw;
|
|
}
|
|
|
|
await _next(httpContext).ConfigureAwait(false);
|
|
}
|
|
}
|
|
} |