Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ 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<IActionResult> Index()
{
var result = await _myService.SendRequest();
return Ok(result);
}

public async Task<IActionResult> Other()
{
var otherResult = await _myOtherService.SendRequest();
return Ok(otherResult);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Serilog.HttpClient.Samples.AspNetCore.Services
{
public interface IMyOtherService
{
Task<object> SendRequest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <copyright file="MyOtherService.cs" company="Sleep Outfitters USA">
// Copyright (c) Sleep Outfitters USA. All rights reserved.
// </copyright>

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<object> SendRequest()
{
return _httpClient.GetFromJsonAsync<object>("https://reqres.in/api/users?page=1");
}
}
}
13 changes: 12 additions & 1 deletion samples/Serilog.HttpClient.Samples.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ public void ConfigureServices(IServiceCollection services)
//Proxy = new WebProxy("127.0.0.1", 8888)
});
//or

services
.AddHttpClient<IMyOtherService, MyOtherService>()
.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();
}

Expand Down
20 changes: 15 additions & 5 deletions src/Serilog.HttpClient/Extensions/HttpClientBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<LoggingDelegatingHandler>(builder.Name, (s, k) =>
{
var o = s.GetRequiredService<IOptionsSnapshot<RequestLoggingOptions>>();
return new LoggingDelegatingHandler(o.Get(builder.Name), default, true);
var opt = s.GetRequiredService<IOptionsSnapshot<RequestLoggingOptions>>();
return new LoggingDelegatingHandler(opt.Get((string)k), default, true);
});
builder.AddHttpMessageHandler(s => s.GetRequiredService<LoggingDelegatingHandler>());

builder.AddHttpMessageHandler(s => s.GetRequiredKeyedService<LoggingDelegatingHandler>(builder.Name));
#else
builder.AddHttpMessageHandler(s =>
{
var o = s.GetRequiredService<IOptionsSnapshot<RequestLoggingOptions>>().Get(builder.Name);
return new LoggingDelegatingHandler(o, forHttpClientFactory: true);
});
#endif
return builder;
}
}

}