add logging handler

This commit is contained in:
honzapatCZ 2024-12-03 01:02:02 +01:00
parent c996ddc53c
commit c3c53d7744
4 changed files with 126 additions and 15 deletions

View File

@ -11,6 +11,7 @@ using Microsoft.EntityFrameworkCore;
using NejCommon.Utils; using NejCommon.Utils;
using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.Annotations;
using NejCommon.Models; using NejCommon.Models;
using System.ComponentModel.DataAnnotations.Schema;
namespace NejCommon.Controllers namespace NejCommon.Controllers
{ {
@ -46,10 +47,25 @@ namespace NejCommon.Controllers
this.providers = providers; this.providers = providers;
} }
protected abstract TType GetQuery(TOwner comp); [NonAction]
public abstract TType? GetQuery(TOwner comp);
protected abstract void Assign(TOwner comp, TType query); protected abstract void Assign(TOwner comp, TType query);
public virtual bool Trackable => true; public virtual bool Trackable => true;
public virtual TType GetQueryHelper(TOwner owner){
var entity = GetQuery(owner);
if (entity == null)
{
if (Trackable)
entity = db.Create<TType>();
else
entity = new TType();
Assign(owner, entity);
}
return entity;
}
/// <summary> /// <summary>
/// Gets the <typeparamref name="TType"/> /// Gets the <typeparamref name="TType"/>
/// </summary> /// </summary>
@ -76,15 +92,7 @@ namespace NejCommon.Controllers
[Route("")] [Route("")]
public virtual async Task<Results<NotFound, BadRequest<Error>, Ok<TUpdateResponse>>> Update([FromServices] TOwner owner, [FromBody] TUpdateRequest body) public virtual async Task<Results<NotFound, BadRequest<Error>, Ok<TUpdateResponse>>> Update([FromServices] TOwner owner, [FromBody] TUpdateRequest body)
{ {
var entity = GetQuery(owner); var entity = GetQueryHelper(owner);
if (entity == null)
{
if (Trackable)
entity = db.Create<TType>();
else
entity = new TType();
Assign(owner, entity);
}
/* /*
if(entity is InvoiceBase inv){ if(entity is InvoiceBase inv){
//Console.Writeline(inv.InvoiceItems.Count); //Console.Writeline(inv.InvoiceItems.Count);

View File

@ -39,7 +39,7 @@ namespace NejCommon.Controllers
{ {
var okArg = IsSubclassOfRawGeneric(typeof(Ok<>), arg).GetGenericArguments()[0]; var okArg = IsSubclassOfRawGeneric(typeof(Ok<>), arg).GetGenericArguments()[0];
Console.WriteLine("Adding: " + okArg); // Console.WriteLine("Adding: " + okArg);
//get or generate the schema //get or generate the schema
var schema = context.SchemaGenerator.GenerateSchema(okArg, context.SchemaRepository); var schema = context.SchemaGenerator.GenerateSchema(okArg, context.SchemaRepository);
@ -56,7 +56,7 @@ namespace NejCommon.Controllers
operation.Responses.Remove("201"); operation.Responses.Remove("201");
var okArg = IsSubclassOfRawGeneric(typeof(CreatedAtRoute<>), arg).GetGenericArguments()[0]; var okArg = IsSubclassOfRawGeneric(typeof(CreatedAtRoute<>), arg).GetGenericArguments()[0];
Console.WriteLine("Adding: " + okArg); // Console.WriteLine("Adding: " + okArg);
//get or generate the schema //get or generate the schema
var schema = context.SchemaGenerator.GenerateSchema(okArg, context.SchemaRepository); var schema = context.SchemaGenerator.GenerateSchema(okArg, context.SchemaRepository);
@ -72,7 +72,7 @@ namespace NejCommon.Controllers
operation.Responses.Remove("400"); operation.Responses.Remove("400");
var okArg = IsSubclassOfRawGeneric(typeof(BadRequest<>), arg).GetGenericArguments()[0]; var okArg = IsSubclassOfRawGeneric(typeof(BadRequest<>), arg).GetGenericArguments()[0];
Console.WriteLine("Adding: " + okArg); // Console.WriteLine("Adding: " + okArg);
//get or generate the schema //get or generate the schema
var schema = context.SchemaGenerator.GenerateSchema(okArg, context.SchemaRepository); var schema = context.SchemaGenerator.GenerateSchema(okArg, context.SchemaRepository);

View File

@ -107,7 +107,7 @@
} }
/* /*
! tailwindcss v3.4.13 | MIT License | https://tailwindcss.com ! tailwindcss v3.4.15 | MIT License | https://tailwindcss.com
*/ */
/* /*
@ -550,7 +550,7 @@ video {
/* Make elements with the HTML hidden attribute stay hidden by default */ /* Make elements with the HTML hidden attribute stay hidden by default */
[hidden] { [hidden]:where(:not([hidden="until-found"])) {
display: none; display: none;
} }

View File

@ -0,0 +1,103 @@
using System.Net.Http.Headers;
using System.Text;
namespace NejCommon.Utils;
public class HttpDebuggingHandler : DelegatingHandler
{
protected readonly ILogger Logger;
public HttpDebuggingHandler(ILogger logger, HttpMessageHandler innerHandler = null)
: base(innerHandler ?? new HttpClientHandler())
{
Logger = logger;
}
async protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var req = request;
var id = Guid.NewGuid().ToString();
var msg = $"[{id} - Request]";
StringBuilder output = new StringBuilder();
output.AppendLine($"{msg}========Start==========");
output.AppendLine($"{req.Method} {req.RequestUri.PathAndQuery} {req.RequestUri.Scheme}/{req.Version}");
output.AppendLine($"Host: {req.RequestUri.Scheme}://{req.RequestUri.Host}");
foreach (var header in req.Headers)
output.AppendLine($"{header.Key}: {string.Join(", ", header.Value)}");
if (req.Content != null)
{
foreach (var header in req.Content.Headers)
output.AppendLine($"{header.Key}: {string.Join(", ", header.Value)}");
if (req.Content is StringContent || IsTextBasedContentType(req.Headers) ||
this.IsTextBasedContentType(req.Content.Headers))
{
var result = await req.Content.ReadAsStringAsync();
output.AppendLine($"Content:");
output.AppendLine($"{result}");
}
}
var start = DateTime.Now;
var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
var end = DateTime.Now;
output.AppendLine($"Duration: {end - start}");
output.AppendLine($"{msg}==========End==========");
Logger.LogInformation(output.ToString());
output.Clear();
msg = $"[{id} - Response]";
output.AppendLine($"{msg}=========Start=========");
var resp = response;
output.AppendLine(
$"{req.RequestUri.Scheme.ToUpper()}/{resp.Version} {(int) resp.StatusCode} {resp.ReasonPhrase}");
foreach (var header in resp.Headers)
output.AppendLine($"{header.Key}: {string.Join(", ", header.Value)}");
if (resp.Content != null)
{
foreach (var header in resp.Content.Headers)
output.AppendLine($"{header.Key}: {string.Join(", ", header.Value)}");
if (resp.Content is StringContent || this.IsTextBasedContentType(resp.Headers) ||
this.IsTextBasedContentType(resp.Content.Headers))
{
start = DateTime.Now;
var result = await resp.Content.ReadAsStringAsync();
end = DateTime.Now;
output.AppendLine($"Content:");
output.AppendLine($"{result}");
output.AppendLine($"Duration: {end - start}");
}
}
output.AppendLine($"{msg}==========End==========");
Logger.LogInformation(output.ToString());
return response;
}
readonly string[] types = new[] {"html", "text", "xml", "json", "txt", "x-www-form-urlencoded"};
bool IsTextBasedContentType(HttpHeaders headers)
{
IEnumerable<string> values;
if (!headers.TryGetValues("Content-Type", out values))
return false;
var header = string.Join(" ", values).ToLowerInvariant();
return types.Any(t => header.Contains(t));
}
}