mirror of
https://github.com/techgarage-ir/MTWireGuard.git
synced 2025-08-29 14:28:09 +02:00
Added Serilog to manage logs
Handle required fields while starting
This commit is contained in:
parent
6fbfaa1007
commit
b128154c60
17 changed files with 749 additions and 154 deletions
59
Application/ExceptionHandler.cs
Normal file
59
Application/ExceptionHandler.cs
Normal file
|
@ -0,0 +1,59 @@
|
|||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MTWireGuard.Application
|
||||
{
|
||||
public class ExceptionHandler(Serilog.ILogger logger, IHttpContextAccessor contextAccessor) : IExceptionHandler
|
||||
{
|
||||
private readonly Serilog.ILogger logger = logger;
|
||||
private readonly IHttpContextAccessor contextAccessor = contextAccessor;
|
||||
|
||||
public ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
string exceptionType = exception.GetType().Name,
|
||||
message = exception.Message,
|
||||
stackTrace = exception.StackTrace,
|
||||
//details = JsonConvert.SerializeObject(exception)!;
|
||||
details = exception.Source;
|
||||
|
||||
ExceptionHandlerContext.Message = message;
|
||||
ExceptionHandlerContext.StackTrace = stackTrace;
|
||||
ExceptionHandlerContext.Details = details;
|
||||
|
||||
if (SetupValidator.IsValid)
|
||||
{
|
||||
logger.Error(exception, "Unhandled error");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.Error("Error in configuration: {Title}, {Description}", SetupValidator.Title, SetupValidator.Description);
|
||||
}
|
||||
|
||||
contextAccessor.HttpContext.Response.Redirect("/Error", true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Fatal(ex, "Error In Exception Handler");
|
||||
}
|
||||
return ValueTask.FromResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExceptionHandlerContext
|
||||
{
|
||||
public static string Message { get; internal set; }
|
||||
public static string StackTrace { get; internal set; }
|
||||
public static string Details { get; internal set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue