Skip to content
This repository was archived by the owner on Jun 1, 2024. It is now read-only.

Unable to connect to ElasticSearch server causing main thread to block #467

Open
@li-zheng-hao

Description

@li-zheng-hao

Does this issue relate to a new feature or an existing bug?

  • Bug

What version of Serilog.Sinks.Elasticsearch is affected? Please list the related NuGet package.
image

What is the target framework and operating system? See target frameworks & net standard matrix.

  • NET 6

Please describe the current behavior?

When Serilog connects to a closed ElasticSearch server, the main thread will always be blocked (more than 2 minutes, maybe longer)

Please describe the expected behavior?

I want to throw an exception if I can't connect in about 10 seconds

If the current behavior is a bug, please provide the steps to reproduce the issue and if possible a minimal demo of the problem

here is my init function:

public static Logger InitSeialog(IConfiguration configuration)
    {
        const string OUTPUT_TEMPLATE =
            "[{Level}] {ENV} {Timestamp:yyyy-MM-dd HH:mm:ss.fff} {SourceContext} <{ThreadId}>  {Message:lj}{NewLine}{Exception}";
        var config = new LoggerConfiguration()
#if DEBUG
            .MinimumLevel.Debug()
#else
            .MinimumLevel.Information()
#endif
            .Enrich.WithProperty("ENV", configuration["Env"])
            .Enrich.WithMachineName()
            .Enrich.WithThreadId()
            .Enrich.FromLogContext()
            .Enrich.WithExceptionDetails()
            .WriteTo.Console(outputTemplate: OUTPUT_TEMPLATE)
            .WriteTo.File("logs/applog_.log"
                , rollingInterval: RollingInterval.Day
                , outputTemplate: OUTPUT_TEMPLATE);
        // init elasticsearch
        if (String.IsNullOrWhiteSpace(configuration["ElasticSearchUrl"])==false)
            // block here
            config.WriteTo.Elasticsearch(
                new ElasticsearchSinkOptions(new Uri(configuration["ElasticSearchUrl"])) // for the docker-compose implementation
                    {
                        ConnectionTimeout = TimeSpan.FromSeconds(3),
                        AutoRegisterTemplate = true,
                        OverwriteTemplate = true,
                        DetectElasticsearchVersion = true,
                        AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                        NumberOfReplicas = 1,
                        NumberOfShards = 2,
                        // BufferBaseFilename = "logs/buffer",
                        // RegisterTemplateFailure = RegisterTemplateRecovery.FailSink,
                        FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                        EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                           EmitEventFailureHandling.WriteToFailureSink |
                                           EmitEventFailureHandling.RaiseCallback,
                        FailureSink = new FileSink("logs/fail-{Date}.txt", new JsonFormatter(), null, null)
                    });

        var logger = config.CreateLogger();
        Log.Logger = logger;
        return logger;
    }

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions