簡體   English   中英

如何將 Kafka SSL 相關故障捕獲到 Serilog 接收器 C# 控制台應用程序中

[英]How to capture Kafka SSL related failures into Serilog sinks C# console app

我正在嘗試通過 Kafka 在 C# .NET Core 中生成一個主題並接收 SSL 相關故障,因為我使用的證書不正確。

[15:19:49 INF] Log in Program.cs
%3|1620890390.755|FAIL|samplepublisher#producer-1| [thrd:ssl://bootstrap.data.test.co:9094/bootstrap]: ssl://bootstrap.data.test.co:9094/bootstrap: SSL handshake failed: error:140900:SSL routines:ssl3_get_server_certificate:certificate verify failed: broker certificate could not be verified, verify that ssl.ca.location is correctly configured or root CA certificates are installed (add broker's CA certificate to the Windows Root certificate store) (after 18ms in state SSL_HANDSHAKE)

此問題已修復,並且與證書有關。 但真正的問題是,來自 Kafka 的這條 FAIL 消息沒有登錄到我們的 .NET Core 3.1 應用程序的任何地方。 我們正在使用 SeriLog,下面是我們使用的代碼。

private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddLogging(configure => configure.AddSerilog());

            services.Configure<LoggerFilterOptions>(options => options.MinLevel = LogLevel.Trace);
        }

這是主程序代碼。

Log.Logger = new LoggerConfiguration()
    .WriteTo.File("logs\\log.txt", rollingInterval: RollingInterval.Day, shared: true)
    .WriteTo.Console()
    .WriteTo.Debug()
    .CreateLogger();

var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection, configuration);

var serviceProvider = serviceCollection.BuildServiceProvider();

var logger = serviceProvider.GetService<ILogger<Program>>();

logger.LogInformation("Log in Program.cs");

try
    {
       var config = new ProducerConfig
       {
         BootstrapServers = Server,
         SslCaLocation = "data.cer",
         SecurityProtocol = SecurityProtocol.Ssl,
       };

 using var producer = new ProducerBuilder<Null, string>(config).Build();
       await producer.ProduceAsync(Topic, new Message<Null, string> { Value = "test message from"});
            
        }
        catch (Exception e)
        {
            Console.WriteLine($"publish failed in {sw.ElapsedMilliseconds} seconds", e);
            throw;
        }

我認為問題在於這些錯誤來自 confluence 庫的深處,而不是來自我們的代碼。 如果我可以將這些日志也放入我們的 SeriLog 接收器,我正在尋找一種方法。

我相信您正在尋找的是ErrorHandler 將 Serilog 記錄器的實例傳遞給它,例如:

using var producer = new ProducerBuilder<Null, string>(config)
    .SetErrorHandler((_, error) => logger.LogWarning("Error in Kafka Handler: {}", error))
    .Build();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM