Home > OS >  Serilog does not send logs to Elasticsearch 8
Serilog does not send logs to Elasticsearch 8

Time:07-21

I used ELK stack to log application errors, but Serilog does not log to elasticsearch.

The elasticsearch version that is used is 8.3.2.

Log configuration in program.cs:

builder.UseSerilog((hostBuilder, serviceProvider, loggerConfiguration) =>
{
    var appSettings = hostBuilder.Configuration.GetSection(nameof(AppSettings)).Get<AppSettings>();

    CreateBasicLoggerConfiguration(loggerConfiguration)
        .WriteTo.File(logPath, rollingInterval: RollingInterval.Hour)
        .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(appSettings.ElasticsearchSettings.Uri))
        {
            IndexFormat = $"َapp-logs-{environment.EnvironmentName.Replace(".","-")}-{DateTimeOffset.Now.LocalDateTime:yyyy-MM}",
            AutoRegisterTemplate = true
        });
});

And, elasticsearch setting in appsettings.json:

"ElasticsearchSettings": {
  "Uri" : "http://localhost:9200"
}

The following packages were added and used:

<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Enrichers.ClientInfo" Version="1.1.4" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" />

Everything looks ok but there is no log in elastichsearch.

CodePudding user response:

This issue is because of the current version of Serilog.Sinks.Elasticsearch serilog sink is not supported elasticsearch version 8.*.

By default, so you need some more configuration. I recently faced this issue and wrote an article about it. I recommend reading that article and solving your problem.

Try the following config:

.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(appSettings.ElasticsearchSettings.Uri))
{
      IndexFormat = $"َapp-logs-{environment.EnvironmentName.Replace(".","-")}-{DateTimeOffset.Now.LocalDateTime:yyyy-MM}",
      AutoRegisterTemplate = true,
      OverwriteTemplate = true,
      TemplateName = yourTemplateName,
      AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
      TypeName = null,
      BatchAction = ElasticOpType.Create
});

Article Link : How to integrate Serilog with Elasticsearch 8 Kibana

  • Related