Extensions methods for adding custom properties to structured logging output
Lightweight extension methods that attach additional properties to log messages when using Microsoft.Extensions.Logging
. They help enrich log output with contextual data.
Install-Package XeonApps.Extensions.Logging.WithProperty
using Microsoft.Extensions.Logging;
ILogger logger = loggerFactory.CreateLogger<Program>();
// Add a property inline
logger
.WithProperty("UserId", "123")
.LogInformation("User {User} logged in", "Jon");
// Create a logger with predefined properties
logger = logger
.WithProperty("AppVersion", "1.0")
.WithProperties(
("SessionId", Guid.NewGuid()),
("Country", "RU")
);
// All properties are included in each call
logger.LogInformation("Event {Event} occurred", "UserLoggedOut");