Added Serilog to manage logs

Handle required fields while starting
This commit is contained in:
Tech Garage 2024-07-21 00:31:22 +03:30
parent 6fbfaa1007
commit b128154c60
17 changed files with 749 additions and 154 deletions

View 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; }
}
}