Logging extensions
Abstractions module provides a wide set of extensions that take care of constructing log events.
These extensions can be grouped in two ways:
By level and name: Debug, Info, Warn, Error, Fatal.
By parameters:
string:
Produces an event with given message template and no properties.
log.Info("Hello, world!");
string + T:
Produces an event with given message template and properties of given object.
log.Info("Hello, {Who}!", new { Who = "world" });
string + object[]:
Produces an event with given message template and given property values (names are inferred from message template).
log.Info("Hello, {Who}!", "world");
Exception:
Produces an event with given exception and no message or properties.
log.Error(new Exception());
Exception + string:
Produces an event with given exception and message template.
log.Error(new Exception(), "I have failed.");
Exception + string + T:
Produces an event with given exception, message template and properties of given object.
log.Error(new Exception(), "{Who} have/has failed.", new { Who = "I" });
Exception + string + object[]:
Produces an event with given exception, message template and given property values (names are inferred from message template).
log.Error(new Exception(), "{Who} have/has failed.", "He");
interpolated string:
Produces an event with given message template and given property values (names are extracted from argument names).
Requires C# 10.
var Who = "world"; log.Info($"Hello, {Who}!");
Exception + interpolated string:
Produces an event with given exception, message template and given property values (names are extracted from argument names).
Requires C# 10.
var Who = "world"; log.Info(new Exception(), $"Hello, {Who}!");
See the section about providing property values to extensions to get a grasp of how to provide values for placeholders in message template.
All extensions produce events with timestamp set to current time and level corresponding to method name.
Last updated