2

I am trying to set up an ASP.NET Core 3.1 Web API to test the elk stack using Serilog v2.9 and Serilog.Sinks.Elasticsearch v8.0.1. This is all new to me and I'm just trying to figure things out. I seem to have everything working and can log simple things all day long and see them in both ES and Kibana. Trouble is I can't seem to destructure anything except anonymous types. To illustrate:

var data = new
{
    SampleData = "Hello World!"
};

_logger.LogInformation("Destructured anonymous object: {@data}", data);

Produces the expected result. A nice shiny log entry with the object "data" serialized perfectly. Whereas:

var test = new TestClass
{
    Guid = Guid.NewGuid(),
    Timestamp = DateTime.UtcNow,
    Title = "Testing this serialization!"
};

_logger.LogInformation("Destructred discrete type.  {@test}", test);

Produces nothing at all. No exception, no entry in ElasticSearch. Nothing. TestClass is a simple class with only those 3 properties, all of which should serialize just fine. I can't figure this out. Here is my LoggerConfiguration:

Log.Logger = new LoggerConfiguration()
   .Enrich.FromLogContext()
   .Enrich.WithExceptionDetails()
   .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticUri))
   {
       AutoRegisterTemplate = true,
       AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
       CustomFormatter = new ExceptionAsObjectJsonFormatter(renderMessage: true)
   })
   .CreateLogger();

What am I missing? Do I have to like, produce a property map or something to destructre non-anonymous objects? Is .Net Core 3.1 too new? I'm at a loss. Every example I've seen online says this should be working.

Lordshmee
  • 43
  • 3

1 Answers1

0

I finally figured this out after banging my head against the problem for a few more hours. My problem is that I was using the plain old Serilog NuGet Package instead of Serilog.AspNetCore. Once I installed the latter it began working as expected.

Welp. I hope this might help anyone else in my position who is following along with the otherwise excellent guide here: https://www.humankode.com/asp-net-core/logging-with-elasticsearch-kibana-asp-net-core-and-docker

Lordshmee
  • 43
  • 3