简体   繁体   中英

Serilog Not Logging to Elasticsearch

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.

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.

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