4

followed the Serilog Elasticsearch tutorial here:

https://github.com/serilog/serilog-sinks-elasticsearch#handling-errors

And nothing is working. I've seen other related SO posts that have suggestions + accepted answers but nothing is working. No errors, the console and file sinks work, however ES never gets written to. Here is the code that I am using:

using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Json;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.File;

namespace aspnet.serilog.sample
{
    public class Program
    {
        public static readonly ILogger logger;

        static Program()
        {
            logger = new LoggerConfiguration() // = Log.Logger = new LoggerConfiguration()
               .Enrich.FromLogContext()
               .MinimumLevel.Information()
               .WriteTo.ColoredConsole(
                   LogEventLevel.Information,
                   "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}")
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/")){
                    AutoRegisterTemplate = true,
                    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                    IndexFormat = "test-index-{0:yyyy.MM.DD}",
                    FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                    EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                   EmitEventFailureHandling.WriteToFailureSink |
                                   EmitEventFailureHandling.RaiseCallback
                    , MinimumLogEventLevel = LogEventLevel.Information
                })
                .WriteTo.File($"./failures.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
        }

        public static void Main(string[] args)
        {
            try
            {
                logger.Information($"starting test application....");
                CreateWebHostBuilder(args).Build().Run();
            }
            finally
            {
                logger.Information($"stopping test application.....");
                //Log.CloseAndFlush();
            }
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseSerilog(logger);
    }
}

I've also added configurations in the Startup.cs file to add the logging but to no avail.

1
  • 2
    To get any output from the SelfLog you need to configure the SelfLog static method. Something like Serilog.Debugging.SelfLog.Enable(Console.Error); Commented Aug 9, 2020 at 13:11

1 Answer 1

1

For those w/ the same struggle here is my solution:

  1. Removed everything logging from the Startup.cs file/class
  2. Reconfigured the Program.cs like so:
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Json;
using Serilog.Sinks.Elasticsearch;
using Serilog.Sinks.File;

namespace aspnet.serilog.sample
{
    public class Program
    {
        public static readonly ILogger logger;

        static Program()
        {
            logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .Enrich.FromLogContext()
                .WriteTo.ColoredConsole(LogEventLevel.Information,
                   "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}")
                .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(
                    new Uri("http://localhost:9200/"))
                    {
                        AutoRegisterTemplate = true,
                        //AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                        IndexFormat = "test-index-{0:yyyy.MM.dd}",
                        // FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                        // EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                        //                EmitEventFailureHandling.WriteToFailureSink |
                        //                EmitEventFailureHandling.RaiseCallback,
                        //                MinimumLogEventLevel = LogEventLevel.Information,
                        // RegisterTemplateFailure = RegisterTemplateRecovery.IndexAnyway,
                        DeadLetterIndexName = "test-deadletter-{0:yyyy.MM.dd}"
                    })
                .WriteTo.File($"./failures.log", rollingInterval: RollingInterval.Day)
                .CreateLogger();
        }

        public static void Main(string[] args)
        {
            try
            {
                logger.Information($"starting test application....");
                CreateWebHostBuilder(args).Build().Run();
            }
            finally
            {
                logger.Information($"stopping test application.....");
                //Log.CloseAndFlush();
                //((IDisposable)logger)?.Dispose();
            }
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseSerilog(logger);
    }
}

I left the comments in to see what doesn't work.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you provider more details about what was wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.