diff --git a/samples/Serilog.HttpClient.Samples.AspNetCore/Controllers/HomeController.cs b/samples/Serilog.HttpClient.Samples.AspNetCore/Controllers/HomeController.cs index 917ee92..c83ab29 100644 --- a/samples/Serilog.HttpClient.Samples.AspNetCore/Controllers/HomeController.cs +++ b/samples/Serilog.HttpClient.Samples.AspNetCore/Controllers/HomeController.cs @@ -8,9 +8,13 @@ public class HomeController : Controller { private readonly IMyService _myService; - public HomeController(IMyService myService) + // for demonstrating multiple configurations of HttpClient + private readonly IMyOtherService _myOtherService; + + public HomeController(IMyService myService, IMyOtherService myOtherService) { - _myService = myService; + _myService = myService; + _myOtherService = myOtherService; } public async Task Index() @@ -18,5 +22,11 @@ public async Task Index() var result = await _myService.SendRequest(); return Ok(result); } + + public async Task Other() + { + var otherResult = await _myOtherService.SendRequest(); + return Ok(otherResult); + } } } \ No newline at end of file diff --git a/samples/Serilog.HttpClient.Samples.AspNetCore/Services/IMyOtherService.cs b/samples/Serilog.HttpClient.Samples.AspNetCore/Services/IMyOtherService.cs new file mode 100644 index 0000000..56de4da --- /dev/null +++ b/samples/Serilog.HttpClient.Samples.AspNetCore/Services/IMyOtherService.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Serilog.HttpClient.Samples.AspNetCore.Services +{ + public interface IMyOtherService + { + Task SendRequest(); + } +} diff --git a/samples/Serilog.HttpClient.Samples.AspNetCore/Services/MyOtherService.cs b/samples/Serilog.HttpClient.Samples.AspNetCore/Services/MyOtherService.cs new file mode 100644 index 0000000..c84bd15 --- /dev/null +++ b/samples/Serilog.HttpClient.Samples.AspNetCore/Services/MyOtherService.cs @@ -0,0 +1,24 @@ +// +// Copyright (c) Sleep Outfitters USA. All rights reserved. +// + +using System.Net.Http.Json; +using System.Threading.Tasks; + +namespace Serilog.HttpClient.Samples.AspNetCore.Services +{ + public class MyOtherService : IMyOtherService + { + private readonly System.Net.Http.HttpClient _httpClient; + + public MyOtherService(System.Net.Http.HttpClient httpClient) + { + _httpClient = httpClient; + } + + public Task SendRequest() + { + return _httpClient.GetFromJsonAsync("https://reqres.in/api/users?page=1"); + } + } +} diff --git a/samples/Serilog.HttpClient.Samples.AspNetCore/Startup.cs b/samples/Serilog.HttpClient.Samples.AspNetCore/Startup.cs index aadd8c0..469aea0 100644 --- a/samples/Serilog.HttpClient.Samples.AspNetCore/Startup.cs +++ b/samples/Serilog.HttpClient.Samples.AspNetCore/Startup.cs @@ -45,7 +45,18 @@ public void ConfigureServices(IServiceCollection services) //Proxy = new WebProxy("127.0.0.1", 8888) }); //or - + services + .AddHttpClient() + .CorrelateRequests("X-Correlation-ID") + .LogRequestResponse(p => + { + p.LogMode = LogMode.LogNone; + }) + // /*OR*/ .LogRequestResponse() + .ConfigurePrimaryHttpMessageHandler(p => new HttpClientHandler() + { + //Proxy = new WebProxy("127.0.0.1", 8888) + }); services.AddControllers(); } diff --git a/src/Serilog.HttpClient/Extensions/HttpClientBuilderExtensions.cs b/src/Serilog.HttpClient/Extensions/HttpClientBuilderExtensions.cs index 9c2c1e1..50dcd77 100644 --- a/src/Serilog.HttpClient/Extensions/HttpClientBuilderExtensions.cs +++ b/src/Serilog.HttpClient/Extensions/HttpClientBuilderExtensions.cs @@ -1,6 +1,8 @@ using System; using Microsoft.Extensions.DependencyInjection; +#if NET8_0_OR_GREATER using Microsoft.Extensions.DependencyInjection.Extensions; +#endif using Microsoft.Extensions.Options; namespace Serilog.HttpClient.Extensions @@ -22,14 +24,22 @@ public static IHttpClientBuilder LogRequestResponse(this IHttpClientBuilder buil throw new ArgumentNullException(nameof(builder)); builder.Services.Configure(builder.Name, configureOptions); - builder.Services.TryAddTransient(s => +#if NET8_0_OR_GREATER + builder.Services.TryAddKeyedTransient(builder.Name, (s, k) => { - var o = s.GetRequiredService>(); - return new LoggingDelegatingHandler(o.Get(builder.Name), default, true); + var opt = s.GetRequiredService>(); + return new LoggingDelegatingHandler(opt.Get((string)k), default, true); }); - builder.AddHttpMessageHandler(s => s.GetRequiredService()); - + builder.AddHttpMessageHandler(s => s.GetRequiredKeyedService(builder.Name)); +#else + builder.AddHttpMessageHandler(s => + { + var o = s.GetRequiredService>().Get(builder.Name); + return new LoggingDelegatingHandler(o, forHttpClientFactory: true); + }); +#endif return builder; } } + } \ No newline at end of file