HTTP requests and responses are logged at DBG level. Host name and API key are obfuscated for user privacy.pull/47/head
parent
5c4fe0886c
commit
1772e7c9fd
@ -1,7 +1,9 @@
|
||||
using Flurl.Http;
|
||||
|
||||
namespace TrashLib.Config
|
||||
{
|
||||
public interface IServerInfo
|
||||
{
|
||||
string BuildUrl();
|
||||
IFlurlRequest BuildRequest();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using Flurl;
|
||||
using Flurl.Http;
|
||||
using Serilog;
|
||||
|
||||
namespace TrashLib.Extensions
|
||||
{
|
||||
public static class FlurlExtensions
|
||||
{
|
||||
public static IFlurlRequest SanitizedLogging(this Uri url, ILogger log)
|
||||
=> new FlurlRequest(url).SanitizedLogging(log);
|
||||
|
||||
public static IFlurlRequest SanitizedLogging(this Url url, ILogger log)
|
||||
=> new FlurlRequest(url).SanitizedLogging(log);
|
||||
|
||||
public static IFlurlRequest SanitizedLogging(this string url, ILogger log)
|
||||
=> new FlurlRequest(url).SanitizedLogging(log);
|
||||
|
||||
public static IFlurlRequest SanitizedLogging(this IFlurlRequest request, ILogger log)
|
||||
{
|
||||
return request.ConfigureRequest(settings => FlurlLogging.SetupLogging(settings, log, SanitizeUrl));
|
||||
}
|
||||
|
||||
private static Url SanitizeUrl(Url url)
|
||||
{
|
||||
// Replace hostname and API key for user privacy
|
||||
url.Host = "hostname";
|
||||
if (url.QueryParams.Contains("apikey"))
|
||||
{
|
||||
url.QueryParams.AddOrReplace("apikey", "SNIP");
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Flurl;
|
||||
using Flurl.Http.Configuration;
|
||||
using Serilog;
|
||||
|
||||
namespace TrashLib.Extensions
|
||||
{
|
||||
public static class FlurlLogging
|
||||
{
|
||||
public static void SetupLogging(FlurlHttpSettings settings, ILogger log, Func<Url, Url>? urlInterceptor = null)
|
||||
{
|
||||
urlInterceptor ??= url => url;
|
||||
|
||||
settings.BeforeCall = call =>
|
||||
{
|
||||
var url = urlInterceptor(call.Request.Url.Clone());
|
||||
log.Debug("HTTP Request to {Url}", url);
|
||||
};
|
||||
|
||||
settings.AfterCall = call =>
|
||||
{
|
||||
var statusCode = call.Response?.StatusCode.ToString() ?? "(No response)";
|
||||
var url = urlInterceptor(call.Request.Url.Clone());
|
||||
log.Debug("HTTP Response {Status} from {Url}", statusCode, url);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue