Search code examples
elasticsearchserilog

How to provide index for Elastic.Serilog.Sinks


Since Serilog.Sinks.Elasticsearch has become obsolete, I am trying to get Elastic.Serilog.Sings to work.

So I do this:

  var uris = new List<Uri>() { new Uri("http://a-good-url:9200") };
  var loggerConfig = new LoggerConfiguration()
  .ReadFrom.Configuration(configuration, options)
  .WriteTo.Elasticsearch(uris, o =>
  {
      o.ChannelDiagnosticsCallback = l => { SelfLog.WriteLine($"Failure={l.PublishSuccess}, Message={l}, Exception={l.ObservedException}"); };
      o.BootstrapMethod = BootstrapMethod.Failure;
      o.MinimumLevel = LogEventLevel.Information;
      o.ConfigureChannel = channel =>
      {
          channel.ExportResponseCallback = (response, buffer) => SelfLog.WriteLine($"Written {buffer.Count} logs to Elasticsearch: {response.ApiCallDetails.HttpStatusCode}");
      };
  });

Log.Logger = loggerConfig.CreateLogger();

But I get this error:

"Failed publish over channel: EcsDataStreamChannel`1.\nExported Buffers: 0\nExported Items: 0\nExport Responses: 0\nExport Retries: 0\nExport Exhausts: 0\nExport Returned Items to retry: False\nInbound Buffer Read Loop Started: True\nInbound Buffer Publishes: 0\nInbound Buffer Publish Failures: 0\nOutbound Buffer Read Loop Started: True\nOutbound Buffer Read Loop Exited: False\nOutbound Buffer Publishes: 0\nOutbound Buffer Publish Failures: 0\nException: None\n"

Also, I have no idea how to tell it to use a specific index like: "my-log-{0:yyyy.MM}". How do I do that?


Solution

  •   var localHostElasticURL= "http://localhost:9200";
      string indexFormat = "stackOverFlowTestindex";
      Log.Logger = new LoggerConfiguration()
          .Enrich.FromLogContext()
          .Enrich.WithExceptionDetails()
          .Enrich.WithProperty("Environment", environment)
          .WriteTo.Elasticsearch(new[] { new Uri(localHostElasticURL) },
          options =>
          {
              options.DataStream = new DataStreamName(indexFormat);
              options.TextFormatting = new EcsTextFormatterConfiguration();
              options.BootstrapMethod = BootstrapMethod.Failure;
              options.ConfigureChannel = channelOptions =>
              {
                  channelOptions.BufferOptions = new BufferOptions();
              };
          },
          configureTransport =>
          {
              configureTransport.ServerCertificateValidationCallback((_, _, _, _) => true);
          })
          .ReadFrom.Configuration(configuration)
          .CreateLogger();
    
      host.UseSerilog(Log.Logger, true);
    

    You cannot see this index directly in Kibana, so you need to enable the hidden index feature. You will see that a datastream has been created.