如何在 Kentico Kontent .NET Delivery Client 中记录 HttpClient 请求?
How to log HttpClient requests in Kentico Kontent .NET Delivery Client?
如何在此处记录 Kentico Kontent .NET 交付 API 的 HTTP 请求:https://github.com/Kentico/kontent-delivery-sdk-net
具体来说,我正在寻找的是如何将 HTTP Get 请求记录到 delivery.kentico.ai(您从 JSON 检索内容的终点)。
您可以丰富 HttpClient
并将其注入 DeliveryClient
。
充实:
public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler, Microsoft.Extensions.Logging.ILogger logger)
: base(innerHandler)
{
Logger = logger;
}
public Microsoft.Extensions.Logging.ILogger Logger { get; }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Logger.LogInformation(request.Method + " " + request.RequestUri);
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
Logger.LogInformation(response.StatusCode + " " + response.Content.Headers);
return response;
}
}
使用例如Serilog
services.AddLogging(builder =>
{
// Add Serilog
builder.AddSerilog(new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File("logs\log.log", rollingInterval: RollingInterval.Day)
.CreateLogger());
});
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Startup>>();
HttpClient httpClient = new HttpClient(new LoggingHandler(new HttpClientHandler(), logger));
var deliveryOptions = new DeliveryOptions();
Configuration.GetSection(nameof(DeliveryOptions)).Bind(deliveryOptions);
注入:
var deliveryClient = DeliveryClientBuilder
.WithOptions(_ => deliveryOptions)
.WithHttpClient(httpClient)
.Build();
其他资源:
使用 HttpClientFactory 的替代方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder =>
{
// Add Serilog
builder.AddSerilog(new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File("logs\log.log", rollingInterval: RollingInterval.Day)
.CreateLogger());
});
services.AddTransient<LoggingHandler>();
services.AddHttpClient("FactoryClient", c => { /* Do whatever else you wish here... */ })
.AddHttpMessageHandler<LoggingHandler>()
.AddTypedClient(c => DeliveryClientBuilder.WithOptions(...).WithHttpClient(c).Build());
services.AddControllersWithViews();
}
LoggingHandler.cs
public class LoggingHandler : DelegatingHandler
{
public Microsoft.Extensions.Logging.ILogger Logger { get; }
public LoggingHandler(ILoggerFactory loggerFactory) : base()
{
Logger = loggerFactory.CreateLogger<LoggingHandler>();
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Logger.LogInformation(request.Method + " " + request.RequestUri);
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
Logger.LogInformation(response.StatusCode + " " + response.Content.Headers);
return response;
}
}
另一种方法是使用 Serilog.AspNetCore
NuGet 包:https://github.com/serilog/serilog-aspnetcore
Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog(); // <-- Add this line;
}
Startup.cs
public void Configure(IApplicationBuilder app)
{
app.UseSerilogRequestLogging();
}
示例代码:https://github.com/Kentico/kontent-sample-app-net/commit/44f1a0e6b245b7ad0be2f0e48a1085adbf80584a
如何在此处记录 Kentico Kontent .NET 交付 API 的 HTTP 请求:https://github.com/Kentico/kontent-delivery-sdk-net
具体来说,我正在寻找的是如何将 HTTP Get 请求记录到 delivery.kentico.ai(您从 JSON 检索内容的终点)。
您可以丰富 HttpClient
并将其注入 DeliveryClient
。
充实:
public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler, Microsoft.Extensions.Logging.ILogger logger)
: base(innerHandler)
{
Logger = logger;
}
public Microsoft.Extensions.Logging.ILogger Logger { get; }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Logger.LogInformation(request.Method + " " + request.RequestUri);
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
Logger.LogInformation(response.StatusCode + " " + response.Content.Headers);
return response;
}
}
使用例如Serilog
services.AddLogging(builder =>
{
// Add Serilog
builder.AddSerilog(new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File("logs\log.log", rollingInterval: RollingInterval.Day)
.CreateLogger());
});
var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Startup>>();
HttpClient httpClient = new HttpClient(new LoggingHandler(new HttpClientHandler(), logger));
var deliveryOptions = new DeliveryOptions();
Configuration.GetSection(nameof(DeliveryOptions)).Bind(deliveryOptions);
注入:
var deliveryClient = DeliveryClientBuilder
.WithOptions(_ => deliveryOptions)
.WithHttpClient(httpClient)
.Build();
其他资源:
使用 HttpClientFactory 的替代方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(builder =>
{
// Add Serilog
builder.AddSerilog(new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File("logs\log.log", rollingInterval: RollingInterval.Day)
.CreateLogger());
});
services.AddTransient<LoggingHandler>();
services.AddHttpClient("FactoryClient", c => { /* Do whatever else you wish here... */ })
.AddHttpMessageHandler<LoggingHandler>()
.AddTypedClient(c => DeliveryClientBuilder.WithOptions(...).WithHttpClient(c).Build());
services.AddControllersWithViews();
}
LoggingHandler.cs
public class LoggingHandler : DelegatingHandler
{
public Microsoft.Extensions.Logging.ILogger Logger { get; }
public LoggingHandler(ILoggerFactory loggerFactory) : base()
{
Logger = loggerFactory.CreateLogger<LoggingHandler>();
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Logger.LogInformation(request.Method + " " + request.RequestUri);
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
Logger.LogInformation(response.StatusCode + " " + response.Content.Headers);
return response;
}
}
另一种方法是使用 Serilog.AspNetCore
NuGet 包:https://github.com/serilog/serilog-aspnetcore
Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog(); // <-- Add this line;
}
Startup.cs
public void Configure(IApplicationBuilder app)
{
app.UseSerilogRequestLogging();
}
示例代码:https://github.com/Kentico/kontent-sample-app-net/commit/44f1a0e6b245b7ad0be2f0e48a1085adbf80584a