0

Below is the code for serilog elastic search.It's working perfectly when elastic service is running but fails when the server is not available

 .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/")) // humio elastic bulk endpoint
                        {
                            AutoRegisterTemplate = true,
                            OverwriteTemplate = true,
                            DetectElasticsearchVersion = true,
                            AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                            NumberOfReplicas = 1,
                            NumberOfShards = 2,
                            RegisterTemplateFailure = RegisterTemplateRecovery.FailSink,
                            FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                            EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                               EmitEventFailureHandling.WriteToFailureSink |
                                               EmitEventFailureHandling.RaiseCallback,
                            //FailureSink = new FileSink("./fail-{Date}.txt", new JsonFormatter(), null, null)
                            //ModifyConnectionSettings = x => x.BasicAuthentication(username: "", password: "88WS28JEuB0G2WC3cYufdgTINAxOGQvliBrqH5Vqutjb")  // password is ingest token from humio
                        })

exception is occuring when elastic server is missing

Elasticsearch.Net.ElasticsearchClientException: 'No connection could be made because the target machine actively refused it.. Call: Status code unknown from: GET /_cat/nodes?h=v'

HttpRequestException: No connection could be made because the target machine actively refused it.
SocketException: No connection could be made because the target machine actively refused it.

This exception was originally thrown at this call stack:
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
System.Net.Http.ConnectHelper.ConnectAsync(string, int, System.Threading.CancellationToken)
11738472
  • 184
  • 3
  • 22
  • sorry. I dont understand your question. It will throw an error if connection is not there. What are you expecting it to do? – Ashish Modi Feb 12 '20 at 10:22
  • 1
    @AshishModi i would like to handle the exception when server is not found. so how can i handle exception when creating new uri – 11738472 Feb 17 '20 at 10:11

0 Answers0