6.71HTTP Request Logging (Disabling)
By default, the system prints relevant log information when sending HTTP remote requests, as shown below:
info: System.Net.Http.HttpClient.Default.LogicalHandler[100] Start processing HTTP request GET https://furion.net/info: System.Net.Http.HttpClient.Default.ClientHandler[100] Sending HTTP request GET https://furion.net/info: System.Net.Http.HttpClient.Default.ClientHandler[101] Received HTTP response headers after 93.0553ms - 200info: System.Net.Http.HttpClient.Default.LogicalHandler[101] End processing HTTP request after 122.2355ms - 200If you want to disable these log messages, you can make the following configuration in the project's appsettings.json and appsettings.Development.json configuration files:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning", "Microsoft.EntityFrameworkCore": "Information", "System.Net.Http.HttpClient": "Warning" // Set the log level to Warning to disable Info-level logging } }}By setting the log level of System.Net.Http.HttpClient to Warning, you can effectively disable Info-level HTTP request log messages. Please note that settings in appsettings.Development.json will override the corresponding settings in appsettings.json (if both exist), which is typically used to provide a different logging strategy in the development environment.
In addition to the above configuration approach, you can also disable HTTP remote request logging within the program. The specific steps are as follows:
// Disable logging for the default clientservices.AddHttpClient(string.Empty) .RemoveAllLoggers();// Disable logging for a specific client//services.AddHttpClient("weixin")// .RemoveAllLoggers();// You can also disable logging for all clients at onceservices.ConfigureHttpClientDefaults(clientBuilder =>{ clientBuilder.RemoveAllLoggers();});// Or use the IHttpRemoteBuilder extension method for one-click configurationservices.AddHttpRemote() .ConfigureHttpClientDefaults(clientBuilder => { clientBuilder.RemoveAllLoggers(); });Custom Logging Service (Inheriting HttpRemoteLoggerBase)
HttpRemoteBuilder provides the UseLogger method, allowing you to replace the built-in logging implementation with a custom logging service. A custom logging service must inherit from the HttpRemoteLoggerBase abstract class and implement (override) the Log method, as shown below:
// Custom logging service: inherit from HttpRemoteLoggerBaseinternal sealed class CustomHttpRemoteLogger( ILogger<Logging> logger, IOptionsMonitor<HttpRemoteOptions> httpRemoteOptions, bool isLoggingRegistered) : HttpRemoteLoggerBase{ /// <inheritdoc /> public override void Log(LogLevel logLevel, Exception? exception, string? message, params object?[] args) { // Check whether a logging provider is registered if (isLoggingRegistered) { logger.Log(logLevel, exception, message, args); } else { // Invoke the fallback log output delegate httpRemoteOptions.CurrentValue.FallbackLogger?.Invoke(LogMessageFormatter.Value(message, args)); } }}Once defined, register it via the UseLogger method:
services.AddHttpRemote(builder =>{ // Register the custom logging service builder.UseLogger<CustomHttpRemoteLogger>(); // Or register by Type // builder.UseLogger(typeof(CustomHttpRemoteLogger));});Manual registration without UseLogger:
If you want full control — for example, passing more parameters to a custom constructor (not just isLoggingRegistered) — you can skip the UseLogger method and register the IHttpRemoteLogger service yourself, as shown below:
// Check whether a logging provider is registeredvar isLoggingRegistered = services.Any(u => u.ServiceType == typeof(ILoggerProvider));// Remove all existing IHttpRemoteLogger registrationsservices.RemoveAll<IHttpRemoteLogger>();// Manually register the custom logging service, passing more parametersservices.AddSingleton<IHttpRemoteLogger>(provider => (IHttpRemoteLogger)ActivatorUtilities.CreateInstance(provider, typeof(CustomHttpRemoteLogger), isLoggingRegistered, extraParam1, extraParam2 /* ... more parameters can be passed here */));This gives you complete control over constructor parameter passing, allowing you to inject any required services or parameters as needed.