refactor(wpf): HttpService日志中uri统一使用UriPartial限制

This commit is contained in:
status102
2025-05-30 19:39:36 +08:00
parent 1ab6f93358
commit d42944e3b5
5 changed files with 13 additions and 12 deletions

View File

@@ -21,7 +21,7 @@ namespace MaaWpfGui.Extensions
{
private static readonly ILogger _logger = Serilog.Log.ForContext("SourceContext", "HttpResponseLoggingExtension");
public static void Log(this HttpResponseMessage response, bool logQuery = true)
public static void Log(this HttpResponseMessage response, UriPartial uriPartial = UriPartial.Query)
{
var method = response?.RequestMessage?.Method;
var uri = response?.RequestMessage?.RequestUri;
@@ -29,11 +29,11 @@ namespace MaaWpfGui.Extensions
if (response is { IsSuccessStatusCode: true })
{
_logger.Information("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(logQuery ? UriPartial.Query : UriPartial.Path));
_logger.Information("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(uriPartial));
}
else
{
_logger.Warning("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(logQuery ? UriPartial.Query : UriPartial.Path));
_logger.Warning("HTTP: {StatusCode} {Method} {Url}", statusCode, method, uri?.GetLeftPart(uriPartial));
}
}
}

View File

@@ -126,7 +126,7 @@ namespace MaaWpfGui.Models
HttpResponseMessage? response = null;
try
{
response = await Instances.HttpService.GetAsync(new(url), logQuery: false);
response = await Instances.HttpService.GetAsync(new(url), uriPartial: UriPartial.Path);
}
catch (Exception e)
{

View File

@@ -145,7 +145,7 @@ namespace MaaWpfGui.Services.Web
}
}
public async Task<HttpResponseMessage> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true)
public async Task<HttpResponseMessage> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, UriPartial uriPartial = UriPartial.Query)
{
var request = new HttpRequestMessage { RequestUri = uri, Method = HttpMethod.Get, Version = HttpVersion.Version20, };
if (extraHeader != null)
@@ -157,7 +157,7 @@ namespace MaaWpfGui.Services.Web
}
var response = await _client.SendAsync(request, httpCompletionOption);
response.Log(logQuery);
response.Log(uriPartial);
return response;
}
@@ -189,7 +189,7 @@ namespace MaaWpfGui.Services.Web
}
}
public async Task<HttpResponseMessage> PostAsync(Uri uri, HttpContent content, Dictionary<string, string>? extraHeader = null)
public async Task<HttpResponseMessage> PostAsync(Uri uri, HttpContent content, Dictionary<string, string>? extraHeader = null, UriPartial uriPartial = UriPartial.Query)
{
var message = new HttpRequestMessage(HttpMethod.Post, uri) { Version = HttpVersion.Version20 };
if (extraHeader is not null)
@@ -203,7 +203,7 @@ namespace MaaWpfGui.Services.Web
message.Headers.Accept.ParseAdd("application/json");
message.Content = content;
var response = await _client.SendAsync(message);
response.Log();
response.Log(uriPartial);
return response;
}

View File

@@ -55,9 +55,9 @@ namespace MaaWpfGui.Services.Web
/// <param name="uri">Target Uri</param>
/// <param name="extraHeader">Extra HTTP Request Headers</param>
/// <param name="httpCompletionOption">The HTTP completion option</param>
/// <param name="logQuery">Whether to log uri</param>
/// <param name="uriPartial">Which parts of uri to log</param>
/// <returns><see cref="HttpRequestMessage"/> object</returns>
Task<HttpResponseMessage> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, bool logQuery = true);
Task<HttpResponseMessage> GetAsync(Uri uri, Dictionary<string, string>? extraHeader = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead, UriPartial uriPartial = UriPartial.Query);
/// <summary>
/// Send HTTP POST request and a string response
@@ -84,8 +84,9 @@ namespace MaaWpfGui.Services.Web
/// <param name="uri">Target Uri</param>
/// <param name="content">The POST body content</param>
/// <param name="extraHeader">Extra HTTP Request Headers</param>
/// <param name="uriPartial">Which parts of uri to log</param>
/// <returns>HttpResponseMessage, null when failed</returns>
Task<HttpResponseMessage> PostAsync(Uri uri, HttpContent content, Dictionary<string, string>? extraHeader = null);
Task<HttpResponseMessage> PostAsync(Uri uri, HttpContent content, Dictionary<string, string>? extraHeader = null, UriPartial uriPartial = UriPartial.Query);
/// <summary>
/// Download a file from the Web

View File

@@ -983,7 +983,7 @@ public class VersionUpdateViewModel : Screen
HttpResponseMessage? response = null;
try
{
response = await Instances.HttpService.GetAsync(new(url), logQuery: false);
response = await Instances.HttpService.GetAsync(new(url), uriPartial: UriPartial.Path);
}
catch (Exception e)
{