SlackLogger
SlackLogger copied to clipboard
Consider IHttpClientFactory instead of static HttpClient
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests
https://www.nuget.org/packages/Microsoft.Extensions.Http
How to support applications that don't use IHttpClientFactory
? Would we have to attempt to resolve it, but default to a static one when we are unable to?
How to support applications that don't use
IHttpClientFactory
? Would we have to attempt to resolve it, but default to a static one when we are unable to?
I think AddHttpClient()
is designed to be able to support something like this:
public static class SlackConfiguration
{
public static ILoggingBuilder AddSlack(this ILoggingBuilder builder)
{
builder.Services.AddHttpClient(nameof(SlackLogger));
builder.Services.AddTransient<ILoggerProvider, LoggerProvider>();
return builder;
}
}
internal class SlackService
{
private async Task PostAsync(/* ... */)
{
// ...
var httpClient = _httpClientFactory.CreateClient(nameof(SlackLogger));
await httpClient.PostAsync(url, content).ConfigureAwait(false);
}
}
Or SlackService
could be refactored as a transient typed client that would be directly injected into SlackLogger.Logger
instead of new
ed up.
Glad to take a shot at an implementation. The project I'm currently using this on doesn't use IHttpClientFactory
.
Allright, that sounds good ☺
I do still plan to get back to this. Someday.™