1

您如何在此处记录 Kentico Kontent .NET 交付 API 的 HTTP 请求:https ://github.com/Kentico/kontent-delivery-sdk-net

具体来说,我正在寻找的是如何将 HTTP Get 请求记录到 delivery.kentico.ai(您从中检索内容 JSON 的端点)。

4

3 回答 3

3

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();

其他资源:

于 2019-11-26T10:43:15.857 回答
1

使用 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;
        }
    }
于 2020-03-02T14:13:50.377 回答
1

另一种方法是使用Serilog.AspNetCoreNuGet 包:https ://github.com/serilog/serilog-aspnetcore

程序.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseSerilog(); // <-- Add this line;
}

启动.cs

public void Configure(IApplicationBuilder app)
{
  app.UseSerilogRequestLogging();
}

示例代码:https ://github.com/Kentico/kontent-sample-app-net/commit/44f1a0e6b245b7ad0be2f0e48a1085adbf80584a

于 2020-05-28T09:51:37.033 回答