繁体   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