3
votes

I'm trying to use the serilog elasticsearch sink to write some log to kibana. According to the spec here the content should be set to application/x-ndjson and since there are no specific option for setting the content type i've tried with GlobalHeaders but it has no effect and it is set to json. I've changed the sample application to the following:

Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console(theme: SystemConsoleTheme.Literate)
            .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://elastic:changeme@localhost:9200")) // for the docker-compose implementation
            {
                ModifyConnectionSettings =
                    x => x.GlobalHeaders(new NameValueCollection { { "Content-Type", "application/x-ndjson" } }),
                BufferBaseFilename = "Text.txt",
                Period = TimeSpan.FromSeconds(10),
                AutoRegisterTemplate = true,
                //BufferBaseFilename = "./buffer",
                RegisterTemplateFailure = RegisterTemplateRecovery.IndexAnyway,
                FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                   EmitEventFailureHandling.WriteToFailureSink |
                                   EmitEventFailureHandling.RaiseCallback,
                FailureSink = new FileSink("./failures.txt", new JsonFormatter(), null)
            })
            .CreateLogger();

            // Enable the selflog output
            SelfLog.Enable(Console.Error);

            Log.Information("Hello, world!");

            int a = 10, b = 0;
            try {
                Log.Debug("Dividing {A} by {B}", a, b);
                Console.WriteLine(a / b);
            } catch (Exception ex) {
                Log.Error(ex, "Something went wrong");
            }

            for (int i = 0; i < 10; i++) {
                Log.Error(new Exception($"Test error {i}"), "Test error {Error}", "TEst message");
            }

The api call does not return error but the logs are not present in kibana. Am i doing something wrong, or it just cannot be configured, or I need to change something in the server?

1

1 Answers

0
votes

I hope following code helps identifying whats going wrong

ModifyConnectionSettings = x => x.OnRequestCompleted(ELKReqCompleted);

 void ELKReqCompleted(IApiCallDetails apiCallDetails)
        {
            //apiCallDetails.AuditTrail
            //apiCallDetails.DebugInformation

        }