perf: 引入 Microsoft.VisualStudio.Threading.Analyzers 分析器 (#62)

This commit is contained in:
2023-11-20 18:11:44 +08:00
committed by GitHub
parent b6bbd8dc88
commit 36c20b813d
55 changed files with 545 additions and 364 deletions

View File

@@ -10,7 +10,7 @@ public static class HttpRequestMessageExtensions
/// </summary>
public static async Task<string> BuildJsonAsync(this HttpRequestMessage me)
{
var body = me?.Content == null ? null : await me.Content!.ReadAsStringAsync();
var body = me?.Content == null ? null : await me.Content!.ReadAsStringAsync().ConfigureAwait(false);
return new { Uri = me?.RequestUri, Header = me?.ToString(), Body = body }.ToJson();
}
@@ -19,7 +19,7 @@ public static class HttpRequestMessageExtensions
/// </summary>
public static async Task<HttpRequestMessage> LogAsync<T>(this HttpRequestMessage me, ILogger<T> logger)
{
logger.Info($"{Ln.请求}: {await me.BuildJsonAsync()}");
logger.Info($"{Ln.请求}: {await me.BuildJsonAsync().ConfigureAwait(false)}");
return me;
}
}

View File

@@ -13,23 +13,25 @@ public static class HttpRequestPartExtensions
public static HttpRequestPart SetLog<T>(this HttpRequestPart me, ILogger<T> logger
, Func<string, string> bodyHandle = null)
{
async void RequestHandle(HttpClient _, HttpRequestMessage req)
#pragma warning disable S1172
Task RequestHandle(HttpClient _, HttpRequestMessage req)
{
#pragma warning disable IDE0058
await req.LogAsync(logger);
#pragma warning restore IDE0058
return req.LogAsync(logger);
}
Task ExceptionHandle(HttpClient _, HttpResponseMessage rsp, string errors)
{
return rsp.LogExceptionAsync(errors, logger, bodyHandle);
}
Task ResponseHandle(HttpClient _, HttpResponseMessage rsp)
{
return rsp.LogAsync(logger, bodyHandle);
}
#pragma warning restore S1172
return me.OnRequesting(RequestHandle).OnResponsing(ResponseHandle).OnException(ExceptionHandle);
async void ExceptionHandle(HttpClient _, HttpResponseMessage rsp, string errors)
{
await rsp.LogExceptionAsync(errors, logger, bodyHandle);
}
async void ResponseHandle(HttpClient _, HttpResponseMessage rsp)
{
await rsp.LogAsync(logger, bodyHandle);
}
}
}

View File

@@ -11,7 +11,7 @@ public static class HttpResponseMessageExtensions
public static async Task LogAsync<T>(this HttpResponseMessage me, ILogger<T> logger
, Func<string, string> bodyPreHandle = null)
{
logger.Info(await me.BuildJsonAsync(bodyPreHandle));
logger.Info(await me.BuildJsonAsync(bodyPreHandle).ConfigureAwait(false));
}
/// <summary>
@@ -20,7 +20,7 @@ public static class HttpResponseMessageExtensions
public static async Task LogExceptionAsync<T>(this HttpResponseMessage me, string errors, ILogger<T> logger
, Func<string, string> bodyHandle = null)
{
logger.Warn($"{errors}: {await me.BuildJsonAsync(bodyHandle)}");
logger.Warn($"{errors}: {await me.BuildJsonAsync(bodyHandle).ConfigureAwait(false)}");
}
/// <summary>
@@ -29,7 +29,7 @@ public static class HttpResponseMessageExtensions
private static async Task<string> BuildJsonAsync( //
this HttpResponseMessage me, Func<string, string> bodyHandle = null)
{
var body = me?.Content is null ? null : await me.Content!.ReadAsStringAsync();
var body = me?.Content is null ? null : await me.Content!.ReadAsStringAsync().ConfigureAwait(false);
return new { Header = me?.ToString(), Body = bodyHandle is null ? body : bodyHandle(body) }.ToJson();
}
}