Configs2026年5月24日·1 分钟阅读

Serilog — Structured Diagnostic Logging for .NET

A structured logging library for .NET applications that captures log events as rich data rather than plain text, with sinks for every major output target.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Serilog Overview
通用 CLI 安装命令
npx tokrepo install 630c6c48-576b-11f1-9bc6-00163e2b0d79

Introduction

Serilog brings structured logging to the .NET ecosystem. Instead of formatting log messages into flat strings, it preserves named properties as queryable data, making logs far more useful for diagnostics and observability pipelines.

What Serilog Does

  • Captures log events as structured data with named properties
  • Routes output through a rich ecosystem of over 100 community sinks
  • Supports message templates with positional and named placeholders
  • Provides log enrichment to attach contextual data like machine name or thread ID
  • Integrates with ASP.NET Core, Microsoft.Extensions.Logging, and generic hosts

Architecture Overview

Serilog uses a pipeline model: log events flow through enrichers that add contextual properties, then through filters, and finally to one or more sinks. Each sink serializes events to a specific destination — console, files, Seq, Elasticsearch, Application Insights, or databases. Configuration can be done in C# fluent API or through appsettings.json via the Serilog.Settings.Configuration package.

Self-Hosting & Configuration

  • Install the core package with dotnet add package Serilog
  • Add sinks like Serilog.Sinks.File, Serilog.Sinks.Seq, or Serilog.Sinks.Elasticsearch
  • Configure minimum log level, enrichers, and sinks in appsettings.json
  • Use Serilog.AspNetCore for automatic HTTP request logging in ASP.NET Core
  • Enable destructuring policies to control how complex objects are serialized

Key Features

  • Message templates preserve structure without sacrificing readability
  • Over 100 sinks for console, files, databases, and observability platforms
  • Enrichers attach ambient context like correlation IDs and environment info
  • Sub-loggers allow per-component filtering and routing
  • Configuration via code or JSON with hot-reload support

Comparison with Similar Tools

  • NLog — XML-configured .NET logger; Serilog's structured data model is natively richer
  • log4net — classic Java-ported logger; Serilog is more modern with better async support
  • Microsoft.Extensions.Logging — abstractions layer; Serilog plugs in as a provider with more sink options
  • Seq — log server that pairs naturally with Serilog but works with any structured source

FAQ

Q: Can Serilog replace the built-in .NET logger? A: Yes. Use the Serilog.Extensions.Hosting package to replace the default ILogger provider.

Q: Does Serilog support async logging? A: Yes. The Serilog.Sinks.Async wrapper batches and writes events on a background thread.

Q: How does Serilog handle high-throughput scenarios? A: Batching sinks and the async wrapper minimize contention. Periodic batching sinks amortize I/O cost.

Q: Is Serilog compatible with .NET Framework? A: Yes. Serilog supports .NET Framework 4.6.2+ and .NET 6/7/8/9.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产