added exception middleware to server for logging
This commit is contained in:
parent
e23ac8496e
commit
83bfaabd95
|
@ -44,5 +44,9 @@ namespace Oqtane.Extensions
|
||||||
|
|
||||||
public static IApplicationBuilder UseJwtAuthorization(this IApplicationBuilder builder)
|
public static IApplicationBuilder UseJwtAuthorization(this IApplicationBuilder builder)
|
||||||
=> builder.UseMiddleware<JwtMiddleware>();
|
=> builder.UseMiddleware<JwtMiddleware>();
|
||||||
|
|
||||||
|
public static IApplicationBuilder UseExceptionMiddleWare(this IApplicationBuilder builder)
|
||||||
|
=> builder.UseMiddleware<ExceptionMiddleware>();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Oqtane.Enums;
|
||||||
|
using Oqtane.Models;
|
||||||
|
using Oqtane.Shared;
|
||||||
|
|
||||||
|
namespace Oqtane.Infrastructure
|
||||||
|
{
|
||||||
|
internal class ExceptionMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
|
||||||
|
public ExceptionMiddleware(RequestDelegate next)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Invoke(HttpContext context, IServiceProvider provider)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
var _logger = provider.GetRequiredService<ILogManager>();
|
||||||
|
var endPoint = context.GetEndpoint()?.DisplayName;
|
||||||
|
var contextAlias = context.Items.FirstOrDefault(i => i.Key.ToString() == "Alias");
|
||||||
|
Alias alias;
|
||||||
|
int siteId = -1;
|
||||||
|
var defaultVal = default(KeyValuePair<int, string>);
|
||||||
|
if (!contextAlias.Equals(defaultVal))
|
||||||
|
{
|
||||||
|
alias = contextAlias.Value as Alias;
|
||||||
|
siteId = alias.SiteId;
|
||||||
|
}
|
||||||
|
_logger.Log(siteId, LogLevel.Error, endPoint, LogFunction.Other, exception, exception.Message, context.User?.Identity.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -199,6 +199,7 @@ namespace Oqtane
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
|
app.UseExceptionMiddleWare();
|
||||||
app.UseTenantResolution();
|
app.UseTenantResolution();
|
||||||
app.UseJwtAuthorization();
|
app.UseJwtAuthorization();
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user