From 5e5cdb3b4719b9d44cc81b62ecb14f6286a4e03a Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Sun, 11 Jan 2026 00:53:44 +0100 Subject: [PATCH] Improve logging --- .../Http/HttpClient.cs | 64 +++++++++---------- .../Logging/ScopedLogger.cs | 2 +- .../SystemExtensions.NetStandard.csproj | 2 +- .../WebSockets/ClientWebSocket.cs | 16 ++--- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/SystemExtensions.NetStandard/Http/HttpClient.cs b/SystemExtensions.NetStandard/Http/HttpClient.cs index 142d585..fdc5f6a 100644 --- a/SystemExtensions.NetStandard/Http/HttpClient.cs +++ b/SystemExtensions.NetStandard/Http/HttpClient.cs @@ -76,157 +76,157 @@ public sealed class HttpClient : IHttpClient, IDisposable public void CancelPendingRequests() { - this.LogInformation(string.Empty, "Canceling request"); + this.LogDebug(string.Empty, "Canceling request"); this.httpClient.CancelPendingRequests(); } public Task DeleteAsync(string requestUri) { - this.LogInformation(requestUri, nameof(DeleteAsync)); + this.LogDebug(requestUri, nameof(DeleteAsync)); return this.httpClient.DeleteAsync(requestUri); } public Task DeleteAsync(string requestUri, CancellationToken cancellationToken) { - this.LogInformation(requestUri, nameof(DeleteAsync)); + this.LogDebug(requestUri, nameof(DeleteAsync)); return this.httpClient.DeleteAsync(requestUri, cancellationToken); } public Task DeleteAsync(Uri requestUri) { - this.LogInformation(requestUri.ToString(), nameof(DeleteAsync)); + this.LogDebug(requestUri.ToString(), nameof(DeleteAsync)); return this.httpClient.DeleteAsync(requestUri); } public Task DeleteAsync(Uri requestUri, CancellationToken cancellationToken) { - this.LogInformation(requestUri.ToString(), nameof(DeleteAsync)); + this.LogDebug(requestUri.ToString(), nameof(DeleteAsync)); return this.httpClient.DeleteAsync(requestUri, cancellationToken); } public Task GetAsync(string requestUri) { - this.LogInformation(requestUri, nameof(GetAsync)); + this.LogDebug(requestUri, nameof(GetAsync)); return this.httpClient.GetAsync(requestUri); } public Task GetAsync(string requestUri, HttpCompletionOption completionOption) { - this.LogInformation(requestUri, nameof(GetAsync)); + this.LogDebug(requestUri, nameof(GetAsync)); return this.httpClient.GetAsync(requestUri, completionOption); } public Task GetAsync(string requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken) { - this.LogInformation(requestUri, nameof(GetAsync)); + this.LogDebug(requestUri, nameof(GetAsync)); return this.httpClient.GetAsync(requestUri, completionOption, cancellationToken); } public Task GetAsync(string requestUri, CancellationToken cancellationToken) { - this.LogInformation(requestUri, nameof(GetAsync)); + this.LogDebug(requestUri, nameof(GetAsync)); return this.httpClient.GetAsync(requestUri, cancellationToken); } public Task GetAsync(Uri requestUri) { - this.LogInformation(requestUri.ToString(), nameof(GetAsync)); + this.LogDebug(requestUri.ToString(), nameof(GetAsync)); return this.httpClient.GetAsync(requestUri); } public Task GetAsync(Uri requestUri, HttpCompletionOption completionOption) { - this.LogInformation(requestUri.ToString(), nameof(GetAsync)); + this.LogDebug(requestUri.ToString(), nameof(GetAsync)); return this.httpClient.GetAsync(requestUri, completionOption); } public Task GetAsync(Uri requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken) { - this.LogInformation(requestUri.ToString(), nameof(GetAsync)); + this.LogDebug(requestUri.ToString(), nameof(GetAsync)); return this.httpClient.GetAsync(requestUri, completionOption, cancellationToken); } public Task GetAsync(Uri requestUri, CancellationToken cancellationToken) { - this.LogInformation(requestUri.ToString(), nameof(GetAsync)); + this.LogDebug(requestUri.ToString(), nameof(GetAsync)); return this.httpClient.GetAsync(requestUri, cancellationToken); } public Task GetByteArrayAsync(string requestUri) { - this.LogInformation(requestUri, nameof(GetByteArrayAsync)); + this.LogDebug(requestUri, nameof(GetByteArrayAsync)); return this.httpClient.GetByteArrayAsync(requestUri); } public Task GetByteArrayAsync(Uri requestUri) { - this.LogInformation(requestUri.ToString(), nameof(GetByteArrayAsync)); + this.LogDebug(requestUri.ToString(), nameof(GetByteArrayAsync)); return this.httpClient.GetByteArrayAsync(requestUri); } public Task GetStreamAsync(string requestUri) { - this.LogInformation(requestUri, nameof(GetStreamAsync)); + this.LogDebug(requestUri, nameof(GetStreamAsync)); return this.httpClient.GetStreamAsync(requestUri); } public Task GetStreamAsync(Uri requestUri) { - this.LogInformation(requestUri.ToString(), nameof(GetStreamAsync)); + this.LogDebug(requestUri.ToString(), nameof(GetStreamAsync)); return this.httpClient.GetStreamAsync(requestUri); } public Task GetStringAsync(string requestUri) { - this.LogInformation(requestUri, nameof(GetStringAsync)); + this.LogDebug(requestUri, nameof(GetStringAsync)); return this.httpClient.GetStringAsync(requestUri); } public Task GetStringAsync(Uri requestUri) { - this.LogInformation(requestUri.ToString(), nameof(GetStringAsync)); + this.LogDebug(requestUri.ToString(), nameof(GetStringAsync)); return this.httpClient.GetStringAsync(requestUri); } public Task PostAsync(string requestUri, HttpContent content) { - this.LogInformation(requestUri, nameof(PostAsync)); + this.LogDebug(requestUri, nameof(PostAsync)); return this.httpClient.PostAsync(requestUri, content); } public Task PostAsync(string requestUri, HttpContent content, CancellationToken cancellationToken) { - this.LogInformation(requestUri, nameof(PostAsync)); + this.LogDebug(requestUri, nameof(PostAsync)); return this.httpClient.PostAsync(requestUri, content, cancellationToken); } public Task PostAsync(Uri requestUri, HttpContent content) { - this.LogInformation(requestUri.ToString(), nameof(PostAsync)); + this.LogDebug(requestUri.ToString(), nameof(PostAsync)); return this.httpClient.PostAsync(requestUri, content); } public Task PostAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken) { - this.LogInformation(requestUri.ToString(), nameof(PostAsync)); + this.LogDebug(requestUri.ToString(), nameof(PostAsync)); return this.httpClient.PostAsync(requestUri, content, cancellationToken); } public Task PutAsync(string requestUri, HttpContent content) { - this.LogInformation(requestUri, nameof(PutAsync)); + this.LogDebug(requestUri, nameof(PutAsync)); return this.httpClient.PutAsync(requestUri, content); } public Task PutAsync(string requestUri, HttpContent content, CancellationToken cancellationToken) { - this.LogInformation(requestUri, nameof(PutAsync)); + this.LogDebug(requestUri, nameof(PutAsync)); return this.httpClient.PutAsync(requestUri, content, cancellationToken); } public Task PutAsync(Uri requestUri, HttpContent content) { - this.LogInformation(requestUri.ToString(), nameof(PutAsync)); + this.LogDebug(requestUri.ToString(), nameof(PutAsync)); return this.httpClient.PutAsync(requestUri, content); } public Task PutAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken) { - this.LogInformation(requestUri.ToString(), nameof(PutAsync)); + this.LogDebug(requestUri.ToString(), nameof(PutAsync)); return this.httpClient.PutAsync(requestUri, content, cancellationToken); } public Task SendAsync(HttpRequestMessage request) { - this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync)); + this.LogDebug(request.RequestUri.ToString(), nameof(SendAsync)); return this.httpClient.SendAsync(request); } public Task SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption) { - this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync)); + this.LogDebug(request.RequestUri.ToString(), nameof(SendAsync)); return this.httpClient.SendAsync(request, completionOption); } public Task SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken) { - this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync)); + this.LogDebug(request.RequestUri.ToString(), nameof(SendAsync)); return this.httpClient.SendAsync(request, completionOption, cancellationToken); } public Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - this.LogInformation(request.RequestUri.ToString(), nameof(SendAsync)); + this.LogDebug(request.RequestUri.ToString(), nameof(SendAsync)); return this.httpClient.SendAsync(request, cancellationToken); } public void Dispose() @@ -237,7 +237,7 @@ public sealed class HttpClient : IHttpClient, IDisposable } } - private void LogInformation(string url, string method) + private void LogDebug(string url, string method) { this.eventEmitted?.Invoke(this, new HttpClientEventMessage(this.scope, method, url)); } diff --git a/SystemExtensions.NetStandard/Logging/ScopedLogger.cs b/SystemExtensions.NetStandard/Logging/ScopedLogger.cs index 1df1a54..d59bd4d 100644 --- a/SystemExtensions.NetStandard/Logging/ScopedLogger.cs +++ b/SystemExtensions.NetStandard/Logging/ScopedLogger.cs @@ -3,7 +3,7 @@ using System.Extensions; namespace System.Logging; -public struct ScopedLogger +public readonly struct ScopedLogger { private readonly ILogger logger; private readonly string scope; diff --git a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj index 7315f27..bc0951e 100644 --- a/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj +++ b/SystemExtensions.NetStandard/SystemExtensions.NetStandard.csproj @@ -7,7 +7,7 @@ LICENSE true System - 1.7 + 1.8 Alexandru Macocian https://github.com/AlexMacocian/SystemExtensions Extensions for the System namespace diff --git a/SystemExtensions.NetStandard/WebSockets/ClientWebSocket.cs b/SystemExtensions.NetStandard/WebSockets/ClientWebSocket.cs index f8941af..9def379 100644 --- a/SystemExtensions.NetStandard/WebSockets/ClientWebSocket.cs +++ b/SystemExtensions.NetStandard/WebSockets/ClientWebSocket.cs @@ -39,44 +39,44 @@ public sealed class ClientWebSocket : IClientWebSocket, IDisposa public void Dispose() { var scopedLogger = this.logger?.CreateScopedLogger(nameof(Dispose), string.Empty); - scopedLogger?.LogInformation($"Disposing websocket"); + scopedLogger?.LogDebug($"Disposing websocket"); this.internalWebSocket?.Dispose(); } public void Abort() { var scopedLogger = this.logger?.CreateScopedLogger(nameof(Abort), string.Empty); - scopedLogger?.LogInformation($"Aborting websocket"); + scopedLogger?.LogDebug($"Aborting websocket"); this.internalWebSocket.Abort(); } public Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken) { var scopedLogger = this.logger?.CreateScopedLogger(nameof(CloseAsync), string.Empty); - scopedLogger?.LogInformation($"Closing websocket. Status [{closeStatus}]. Status Description [{statusDescription}]"); + scopedLogger?.LogDebug($"Closing websocket. Status [{closeStatus}]. Status Description [{statusDescription}]"); return this.internalWebSocket.CloseAsync(closeStatus, statusDescription, cancellationToken); } public Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken) { var scopedLogger = this.logger?.CreateScopedLogger(nameof(CloseOutputAsync), string.Empty); - scopedLogger?.LogInformation($"Closing output. Status [{closeStatus}]. Status Description [{statusDescription}]"); + scopedLogger?.LogDebug($"Closing output. Status [{closeStatus}]. Status Description [{statusDescription}]"); return this.internalWebSocket.CloseOutputAsync(closeStatus, statusDescription, cancellationToken); } public Task ConnectAsync(Uri uri, CancellationToken cancellationToken) { var scopedLogger = this.logger?.CreateScopedLogger(nameof(ConnectAsync), string.Empty); - scopedLogger?.LogInformation($"Connecting to {uri}"); + scopedLogger?.LogDebug($"Connecting to {uri}"); return this.internalWebSocket.ConnectAsync(uri, cancellationToken); } public async Task ReceiveAsync(ArraySegment buffer, CancellationToken cancellationToken) { var scopedLogger = this.logger?.CreateScopedLogger(nameof(ConnectAsync), string.Empty); - scopedLogger?.LogInformation($"Attempting to receive bytes"); + scopedLogger?.LogDebug($"Attempting to receive bytes"); var result = await this.internalWebSocket.ReceiveAsync(buffer, cancellationToken); - scopedLogger?.LogInformation($"Received message [{result.MessageType}]"); + scopedLogger?.LogDebug($"Received message [{result.MessageType}]"); scopedLogger?.LogDebug($"Type: {result.MessageType}\nCount: {result.Count}\nEndOfMessage: {result.EndOfMessage}\nCloseStatus: {result.CloseStatus}\nCloseStatusDescription: {result.CloseStatusDescription}"); return result; @@ -85,7 +85,7 @@ public sealed class ClientWebSocket : IClientWebSocket, IDisposa public Task SendAsync(ArraySegment buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) { var scopedLogger = this.logger?.CreateScopedLogger(nameof(SendAsync), string.Empty); - scopedLogger?.LogInformation($"Sending bytes"); + scopedLogger?.LogDebug($"Sending bytes"); scopedLogger?.LogDebug($"Type: {messageType}\nCount: {buffer.Count}\nEndOfMessage: {endOfMessage}"); return this.internalWebSocket.SendAsync(buffer, messageType, endOfMessage, cancellationToken); }