liveBook · Manning

Message template

As explained in Building Web APIs with ASP.NET Core

Message template syntax is a logging notation that combines a message format with named placeholders whose values are captured as structured properties.

A message template is a log message with named placeholders. Values supplied to the logging call fill those placeholders and are captured as structured properties. Names such as `{StartTime}` make a log call easier to read than numbered placeholders such as `{0}`. section 7.2.5 section 7.4.5

Log a controller start time

_logger.LogInformation(CustomLogEvents.BoardGamesController_Get, "Get method started at {StartTime:HH:mm}.", DateTime.Now); `StartTime` names the value; `HH:mm` formats its displayed time. The event ID remains a separate argument. section 7.4.5

With multiple placeholders, pass values in the same order as the placeholders appear. The names describe the properties; they do not select variables by name. For example, `{catName}` receives `categoryName` because it occupies the corresponding argument position. section 7.2.5

_logger.LogInformation(CustomLogEvents.BoardGamesController_Get,
    "Get method started [{MachineName}] [{ThreadId}].",           #1
    Environment.MachineName,                                      #2
    Environment.CurrentManagedThreadId);                          #3

This controller call pairs `{MachineName}` and `{ThreadId}` with two arguments in order. section 7.4.5

The same pattern works for errors: add an `{errorMessage}` placeholder to an exception-handler log call and supply the exception’s `Message` value. section 7.5.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains placeholder names, positional value matching, and time formatting through logging calls; does not detail property storage.
Examples
Shows controller start times, machine and thread values, and an exception-message property.
What it assumes
Builds on C# logging calls and introduces named placeholders by comparison with string.Format.
What you can do after
Apply it via ILogger and Serilog to name logged values and add an exception-message property.
How it connects
Connects ILogger and Serilog to structured logging, event IDs, and exception logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.