【问题标题】:ELK serilog sink FailureCallbackELK serilog sink FailureCallback
【发布时间】:2022-03-07 17:49:40
【问题描述】:

我能够成功地将弹性搜索接收器集成到我的 .net 应用程序中,现在我正在尝试设置 FailureCallback 选项,但每次出现错误时,LogEvent 中的异常为 null,我可以在控制台,但我希望能够在我的失败回调函数中捕获此异常,这是我当前的配置:

myLogger = new LoggerConfiguration()
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticSearchUrl))
                {
                    AutoRegisterTemplate = true,
                    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
                    IndexDecider = (@event, dateTimeOffset) =>
                    {
                        //some logic here
                        return $"custom-index";
                    },
                    EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog | EmitEventFailureHandling.RaiseCallback,
                    FailureCallback = HandleElasticError
                })
                .Enrich.WithProperty("Environment", Config.Environment)
                .Destructure.ByTransforming<ExpandoObject>(JsonConvert.SerializeObject)
                .CreateLogger();

这是我的 HandleElasticError 函数:

private void HandleElasticError(LogEvent e)
        {
            FileLogger.Error(e.Exception, e.MessageTemplate.Text, e.Properties);
        }

当我附加调试器并检查 LogEvent 时,异常为 null,但是在 Visual Studio 的输出窗口中我可以看到实际错误:

2020-11-30T20:55:07.7820674Z Caught exception while preforming bulk operation to Elasticsearch: Elasticsearch.Net.ElasticsearchClientException: The underlying connection was closed: An unexpected error occurred on a send.. Call: Status code unknown from: POST /_bulk ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: The handshake failed due to an unexpected packet format.
   at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
   at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
   at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---

我不关心错误,我知道问题是什么,我只是希望能够在我的失败回调函数中捕获这个异常。 有什么想法吗?

【问题讨论】:

    标签: elasticsearch serilog


    【解决方案1】:

    LogEvent.Exception 是您对(例如)logger.Error(myException) 的调用附带的 Exception - 不是写入 Elastic Search 失败引发的异常。

    您尝试捕获的异常看起来不像 just passes the LogEvent to FailureCallback 那样被接收器公开,而不是导致失败的异常。

    Here's what it could look like were the exception exposed.

    【讨论】:

    • 谢谢,后来我确实意识到,当我查看protected virtual void HandleException(Exception ex, IEnumerable&lt;LogEvent&gt; events) 时,Exception 属性是来自实际调用者的属性,如果您可以将该提交合并到 master 分支中,那就太好了知道实际问题是什么而不是仅仅知道发生了不好的事情非常有帮助。
    【解决方案2】:

    你可以使用:

        StreamWriter writer = File.CreateText(...);
        Serilog.Debugging.SelfLog.Enable(writer);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多