项目作者: viktor-nikolaev

项目描述 :
Extensions methods for adding custom properties to structured logging output
高级语言: C#
项目地址: git://github.com/viktor-nikolaev/XeonApps.Extensions.Logging.WithProperty.git
创建时间: 2020-04-30T18:11:09Z
项目社区:https://github.com/viktor-nikolaev/XeonApps.Extensions.Logging.WithProperty

开源协议:Apache License 2.0

下载


XeonApps.Extensions.Logging.WithProperty

Version

Lightweight extension methods that attach additional properties to log messages when using Microsoft.Extensions.Logging. They help enrich log output with contextual data.

Installation

  1. Install-Package XeonApps.Extensions.Logging.WithProperty

Usage

  1. using Microsoft.Extensions.Logging;
  2. ILogger logger = loggerFactory.CreateLogger<Program>();
  3. // Add a property inline
  4. logger
  5. .WithProperty("UserId", "123")
  6. .LogInformation("User {User} logged in", "Jon");
  7. // Create a logger with predefined properties
  8. logger = logger
  9. .WithProperty("AppVersion", "1.0")
  10. .WithProperties(
  11. ("SessionId", Guid.NewGuid()),
  12. ("Country", "RU")
  13. );
  14. // All properties are included in each call
  15. logger.LogInformation("Event {Event} occurred", "UserLoggedOut");

Why use it?

  • Enriches logs with contextual information using concise syntax.
  • Works with NLog and Serilog via the standard logging abstractions.
  • Supports adding single properties or sets of properties at once.