40

I'm using Serilog with an MS SQL Server sink in my application. Let's assume I have defined the following class ...

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public DateTime BirthDate { get; set; }
  // ... more properties
}

... and created an instance:

var person = new Person
{
    FirstName = "John",
    LastName = "Doe",
    BirthDate = DateTime.UtcNow.AddYears(-25)
};

I have placed the following log call in my code:

Log.Information("New user: {FirstName:l} {LastName:l}",
    person.FirstName, person.LastName);

Is it possible to also log the BirthDate property without adding it to the message template so that it's rendered within the Properties XML column? I'd like to output it later in a details view of my application's log viewer.

I'm basically looking for a behavior similar to the object destructuring, but without printing the flat object as part of the log message.

3 Answers 3

48

This is as simple as:

Log.ForContext("BirthDate", person.BirthDate)
   .Information("New user: {FirstName:l} {LastName:l}",
                           person.FirstName, person.LastName);
3
  • I see, that's pretty simple. Does that method also allow for destructuring of objects that have a complex type (such as a Father property of type Person)? I'm more interested in the properties' values than the ToString() representation. Jan 12, 2015 at 9:31
  • 10
    Absolutely - Log.ForContext("Father", father, destructureObjects: true) does that. Jan 12, 2015 at 12:08
  • Perfect! Exactly what I was looking for. I read the manual, but I must have somehow overlooked that. Thank you! Jan 12, 2015 at 13:13
33

You can actually do this in a few different ways. In your case, the first way is probably the best:

Log.ForContext("BirthDate", person.BirthDate)
    .Information("New user: {FirstName:l} {LastName:l}",
        person.FirstName, person.LastName);

But you can also use the LogContext in other scenarios:

Log.Logger = new LoggerConfiguration()
    // Enrich all log entries with properties from LogContext
    .Enrich.FromLogContext();

using (LogContext.PushProperty("BirthDate", person.BirthDate))
{
    Log.Information("New user: {FirstName:l} {LastName:l}",
        person.FirstName, person.LastName);
}

Or, in the case where you want to log a "constant" property, you can add it like this:

Log.Logger = new LoggerConfiguration()
    // Enrich all log entries with property
    .Enrich.WithProperty("Application", "My Application");

See Context and correlation – structured logging concepts in .NET (5) for more information.

1
  • .Enrich.FromLogContext() is the call I was missing to make my property-based filtering work! Jul 14, 2021 at 15:23
10

If you're using the generic Microsoft ILogger interface you can use BeginScope;

using (_logger.BeginScope(new Dictionary<string, object> { { "LogEventType", logEventType }, { "UserName",  userName } }))
{
    _logger.LogInformation(message, args);
}

This is discussed here; https://blog.rsuter.com/logging-with-ilogger-recommendations-and-best-practices/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.