Skip to content

Commit

Permalink
Add verbose HTTP logging (2.4.0 backport)
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Jul 2, 2023
1 parent cec5c58 commit 69dc4f1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Bloxstrap/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public partial class App : Application
public static string Version = Assembly.GetExecutingAssembly().GetName().Version!.ToString()[..^2];

// singletons
public static readonly Logger Logger = new();
public static readonly JsonManager<Settings> Settings = new();
public static readonly JsonManager<State> State = new();
public static readonly FastFlagManager FastFlags = new();
public static readonly HttpClient HttpClient = new(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All });

public static readonly Logger Logger = new();
public static readonly HttpClient HttpClient = new(new HttpClientLoggingHandler(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All }));

public static System.Windows.Forms.NotifyIcon Notification { get; private set; } = null!;

Expand Down
31 changes: 31 additions & 0 deletions Bloxstrap/HttpClientLoggingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Bloxstrap
{
internal class HttpClientLoggingHandler : MessageProcessingHandler
{
public HttpClientLoggingHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
{
}

protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, CancellationToken cancellationToken)
{
App.Logger.WriteLine($"[HttpClientLoggingHandler::HttpRequestMessage] {request.Method} {request.RequestUri}");
return request;
}

protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken)
{
App.Logger.WriteLine($"[HttpClientLoggingHandler::HttpResponseMessage] {(int)response.StatusCode} {response.ReasonPhrase} {response.RequestMessage!.RequestUri}");
return response;
}
}
}
2 changes: 0 additions & 2 deletions Bloxstrap/RobloxDeployment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public static async Task<ClientVersion> GetInfo(string channel, bool timestamp =
throw new Exception($"Could not get latest deploy for channel {channel}! (HTTP {deployInfoResponse.StatusCode})");
}

App.Logger.WriteLine($"[RobloxDeployment::GetInfo] Got JSON: {rawResponse}");

ClientVersion clientVersion = JsonSerializer.Deserialize<ClientVersion>(rawResponse)!;

// for preferences
Expand Down
4 changes: 1 addition & 3 deletions Bloxstrap/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ public static int GetProcessCount(string processName, bool log = true)
{
try
{
App.Logger.WriteLine($"[Utilities::GetJson<{typeof(T).Name}>] Getting JSON from {url}!");
string json = await App.HttpClient.GetStringAsync(url);
App.Logger.WriteLine($"[Utilities::GetJson<{typeof(T).Name}>] Got JSON: {json}");
return JsonSerializer.Deserialize<T>(json);
}
catch (Exception ex)
{
App.Logger.WriteLine($"[Utilities::GetJson<{typeof(T).Name}>] Failed to deserialize JSON!");
App.Logger.WriteLine($"[Utilities::GetJson<{typeof(T).Name}>] Failed to deserialize JSON for {url}!");
App.Logger.WriteLine($"[Utilities::GetJson<{typeof(T).Name}>] {ex}");
return default;
}
Expand Down

0 comments on commit 69dc4f1

Please sign in to comment.