简体   繁体   中英

Serilog does not send logs to Elasticsearch 8

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM