Browse Source

Add LoggingHandler to log HTTP requests.

pull/145/head
maliming 1 year ago
parent
commit
bed50d7419
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 15
      src/EventHub.Web/EventHubWebModule.cs
  2. 27
      src/EventHub.Web/LoggingHandler.cs

15
src/EventHub.Web/EventHubWebModule.cs

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Net.Http;
using EventHub.Localization;
using EventHub.Options;
using EventHub.Web.Menus;
@ -89,7 +90,7 @@ namespace EventHub.Web
ConfigureSwaggerServices(context.Services);
ConfigureRazorPageOptions();
}
private void ConfigureBundles()
{
Configure<AbpBundlingOptions>(options =>
@ -118,7 +119,7 @@ namespace EventHub.Web
{
options.Applications["MVC"].RootUrl = EventHubUrlOptions.GetWwwConfigValue(configuration);
});
Configure<AbpRemoteServiceOptions>(options =>
{
options.RemoteServices.Default = new RemoteServiceConfiguration(
@ -127,7 +128,7 @@ namespace EventHub.Web
);
});
}
private void ConfigureRazorPageOptions()
{
Configure<RazorPagesOptions>(options =>
@ -169,9 +170,11 @@ namespace EventHub.Web
options.Scope.Add("email");
options.Scope.Add("phone");
options.Scope.Add("EventHub");
options.BackchannelHttpHandler = new LoggingHandler();
});
}
private void ConfigureAutoMapper()
{
Configure<AbpAutoMapperOptions>(options =>
@ -206,7 +209,7 @@ namespace EventHub.Web
options.Contributors.Add(new EventHubToolbarContributor());
});
}
private void ConfigureCookies(ServiceConfigurationContext context)
{
context.Services.AddSameSiteCookiePolicy();
@ -238,7 +241,7 @@ namespace EventHub.Web
{
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();
app.Use((context, next) =>
{
context.Request.Scheme = "https";

27
src/EventHub.Web/LoggingHandler.cs

@ -0,0 +1,27 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
namespace EventHub.Web;
public class LoggingHandler : DelegatingHandler
{
public LoggingHandler()
: base(new HttpClientHandler())
{
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Log.Error($"[Request] {request.Method} {request.RequestUri}");
if (request.Method == HttpMethod.Post && request.Content != null)
{
var content = await request.Content.ReadAsStringAsync(cancellationToken);
Log.Error($"[Request Content Headers] {request.Content.Headers}");
Log.Error($"[Request Content] {content}");
}
return await base.SendAsync(request, cancellationToken);
}
}
Loading…
Cancel
Save