【发布时间】: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