31

When using the HttpClientFactory of .NET Core, is it possible to somehow remove the default LoggingHttpMessageHandler?

I expect something like the below but it doesn't seem to exists

services.AddHttpClient("minos")
   .RemoveHttpMessageHandler<LoggingHttpMessageHandler>();

2 Answers 2

32

Just for anyone needing this, I had opened an issue on the GitHub repo, and one of the contributors had replied with the following

services.RemoveAll<IHttpMessageHandlerBuilderFilter>();
  1. Just Add this line AFTER the adding of the HttpClient
  2. Make sure to use Microsoft.Extensions.DependencyInjection.Extensions namespace

https://github.com/aspnet/HttpClientFactory/issues/196#issuecomment-432755765

Sign up to request clarification or add additional context in comments.

2 Comments

a lot of people copy the code without reading a lot, so posting a code that does not work at all and adding an update it is not ideal at all, this is what happened to me and this is why I updated this answer. Anyway, that worked, Thank you
THANK YOU @HakanFıstık and Stephen!!! I've had this stupid HttpClient bug for weeks. Your line disabled the logging and resolved the bug. System.ObjectDisposedException: Cannot access a disposed object. Object name: 'DefaultMeterFactory'. at Microsoft.Extensions.Diagnostics.Metrics.DefaultMeterFactory.Create... at System.Net.Http.Metrics.MetricsHandler..ctor... at System.Net.Http.SocketsHttpHandler.SetupHandlerChain() at System.Net.Http.SocketsHttpHandler.SendAsync(... at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.<SendCoreAsync>...
28

You can configure it in appsettings.json by setting HttpClient's log level to none:

However, this will also enable informational messages from all other components in the System root namespace. Therefore it may be better to configure the logging by limiting the configuration to “System.Net.Http.HttpClient” so that you only add in the messages from the HTTP requests through clients created via the HttpClientFactory:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning",
          "System.Net.Http.HttpClient": "Information"
        }
      }
    }

https://www.stevejgordon.co.uk/httpclientfactory-asp-net-core-logging

4 Comments

Thanks for that, but I didn't want to remove only the logs, I wanted to remove the whole middleware (or messagehandler) from the pipeline in order to be even more faster. I left a comment on the github and they gave me a solutio which i havent tried yet
I like this solution better. that way we can change the logging level to error. instead of removing all loging from httpclient completely
But the question is how to remove it (which could be for any number of valid reasons) not if you like it being there or not :)
This was the correct solution for limiting the logging threshold. Thanks much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.