J.Doe
J.Doe

Reputation: 155

Logging (via Serilog) to ElasticSearch does not work

I am trying to send logs into ElasticSearch and review them in Kibana. For some strange reason it does not work: the projects is doing something but there is nothing in Kibana. I downloaded and tried over 10 projects related to this topic and althugh they were introduced as the working samples, non of them did the job: there is nothing in Kibana!

Here I am publishing the source code of one of them:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        // Create Serilog Elasticsearch logger
        Log.Logger = new LoggerConfiguration()
           .Enrich.FromLogContext()
           .MinimumLevel.Information() //debug
           .WriteTo.Elasticsearch().WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
           {
               MinimumLogEventLevel = LogEventLevel.Verbose,
               AutoRegisterTemplate = true
           })
           .CreateLogger();

        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();


        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });


    }
}

And this is the Controller:

public IActionResult Index()
{
    _logger.LogInformation("Hello world!");
    return View();
}

public IActionResult About()
{
    var elasticUri = new Uri("http://localhost:9200/cmstest/obj");

    var loggerConfig = new LoggerConfiguration()
                    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(elasticUri)
                    {
                        AutoRegisterTemplate = true,
                        MinimumLogEventLevel = Serilog.Events.LogEventLevel.Verbose
                    });


    var logger = loggerConfig.CreateLogger();

    logger.Error("Hello world");
    logger.Information("Hello world");
    logger.Warning("Hello world");

    return View();
}




public IActionResult Contact()
{
    _logger.LogInformation("Hello world");

    var logger = new LoggerConfiguration()
                    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(
                    new Uri("http://localhost:9200"))
                    {
                        AutoRegisterTemplate = true
                    }).CreateLogger();

    logger.Information("Hello world");

    return View();
}

PS I also found the examples with log4net - did not work neither

Upvotes: 4

Views: 9490

Answers (2)

promlert
promlert

Reputation: 31

ELK v.8 latest Add Option BatchAction = ElasticOpType.Create, TypeName = null,

return new ElasticsearchSinkOptions(new Uri(configuration["ElasticsearchSettings:Uri"])) {

    ModifyConnectionSettings = x => x.BasicAuthentication(configuration["ElasticsearchSettings:username"], configuration["ElasticsearchSettings:password"]),
    AutoRegisterTemplate = true,
    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,// < --this was the magic bullet
    IndexFormat = $"{Assembly.GetExecutingAssembly().GetName().Name.ToLower()}-{env.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}",
    BatchAction = ElasticOpType.Create,
    TypeName = null,
    FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
    EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                     EmitEventFailureHandling.WriteToFailureSink |
                                     EmitEventFailureHandling.RaiseCallback,
    FailureSink = new FileSink("./failures.txt", new JsonFormatter(), null)
};

Upvotes: 3

Niraj Trivedi
Niraj Trivedi

Reputation: 2870

You can console log the response by handling failure call back event

.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
                {
                    FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                    EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog |
                                       EmitEventFailureHandling.WriteToFailureSink |
                                       EmitEventFailureHandling.RaiseCallback,
                    FailureSink = new FileSink("./failures.txt", new JsonFormatter(), null)
                })

For more information visit https://github.com/serilog/serilog-sinks-elasticsearch#handling-errors

Also, for EmitEventFailureHandling.WriteToSelfLog you need to enable it in startup.cs file

Serilog.Debugging.SelfLog.Enable(Console.WriteLine);

above code enable logs the error in console

Upvotes: 5

Related Questions