Serilog Log Exception and Other Properties

6.3k Views Asked by At

I am implementing Serilog in a WCF Service application. In my Global.asax, I am trying to set up the global logger to print the time of the log, the thread ID, any exceptions of that may be logged, and the additional properties in json format included with the events.

My log setup:

Log.Logger = new LoggerConfiguration()
    .Enrich.WithThreadId()
    .Enrich.WithThreadName()
    .Enrich.WithExceptionDetails()
    .Enrich.FromLogContext()
        .WriteTo.File(
            path: "\\LogFiles\\Gateway\\gateway-properties-.log", 
             rollingInterval: RollingInterval.Day, 
            outputTemplate: "{Timestamp:HH:mm} [{Level}] ({ThreadId}) {Message}{NewLine}{Exception} {Properties:j}")
    .CreateLogger();
Log.Information("Initilizing Application");

When I look at the log created for the Information event in the snippet, it looks like this

16:28 [Information] (1) Initilizing Application
{}

When I try to log an exception:

throw new Exception("This is a forced exception");
...
catch (Exception ex)
{
    Log.Fatal("Application could not be started", ex);
}

I see this in the log:

16:28 [Fatal] (1) Application could not be started
{}

I was expecting to see the exception message and stack trace included with the log item.

In another location I am trying to log the success of a request process:

Log.Information("Transaction {0} successfully processed", request.TransactionId, request); 

And I see:

16:40 [Information] (8) Transaction "the correct transaction ID" successfully processed
{}

Obviously I am setting things up incorrectly or nor logging things properly.

Specific questions

  • Question 1: Does anything in my configuration conflict with my desired output?
  • Question 2: Does Serilog automatically include the braces for properties even if they are not included with the log event? (the empty {} after the log)
  • Question 3: When I do provide properties, why are they being included the interpolated field, but not in the properties section of the log?
  • Question 4: Do I need further configuration to include the details of an exception?
0

There are 0 best solutions below