ln-771-logging-configurator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ln-771-logging-configurator

ln-771-logging-configurator

Type: L3 Worker Category: 7XX Project Bootstrap Parent: ln-770-crosscutting-setup
Configures structured JSON logging for .NET and Python projects.

类型: L3 Worker 分类: 7XX 项目初始化 父项: ln-770-crosscutting-setup
为.NET和Python项目配置结构化JSON日志。

Overview

概述

AspectDetails
InputContext Store from ln-770
OutputLogging configuration files
Stacks.NET (Serilog), Python (structlog)

方面详情
输入来自ln-770的上下文存储
输出日志配置文件
技术栈.NET(Serilog)、Python(structlog)

Phase 1: Receive Context

阶段1:接收上下文

Accept Context Store from coordinator.
Required Context:
  • STACK
    : .NET or Python
  • FRAMEWORK
    : ASP.NET Core or FastAPI
  • FRAMEWORK_VERSION
    : Version number
  • PROJECT_ROOT
    : Project directory path
  • ENVIRONMENT
    : Development or Production
Validation:
  • If
    STACK
    not provided, detect from project files
  • If version not provided, use latest stable

接收来自协调器的上下文存储。
必需上下文:
  • STACK
    :.NET 或 Python
  • FRAMEWORK
    :ASP.NET Core 或 FastAPI
  • FRAMEWORK_VERSION
    :版本号
  • PROJECT_ROOT
    :项目目录路径
  • ENVIRONMENT
    :开发或生产
验证:
  • 如果未提供
    STACK
    ,从项目文件中检测
  • 如果未提供版本,使用最新稳定版

Phase 2: Research Current Best Practices

阶段2:调研当前最佳实践

Use MCP tools to get up-to-date documentation.
For .NET (Serilog):
MCP ref: "Serilog ASP.NET Core structured logging configuration"
Context7: /serilog/serilog-aspnetcore
For Python (structlog):
MCP ref: "structlog Python structured logging configuration"
Context7: /hynek/structlog
Key Patterns to Research:
  1. Request logging middleware
  2. Log enrichment (correlation ID, user context)
  3. Log level configuration by environment
  4. Sink configuration (Console, File, Seq, Elastic)

使用MCP工具获取最新文档。
针对.NET(Serilog):
MCP ref: "Serilog ASP.NET Core structured logging configuration"
Context7: /serilog/serilog-aspnetcore
针对Python(structlog):
MCP ref: "structlog Python structured logging configuration"
Context7: /hynek/structlog
需调研的核心模式:
  1. 请求日志中间件
  2. 日志增强(关联ID、用户上下文)
  3. 按环境配置日志级别
  4. Sink配置(控制台、文件、Seq、Elastic)

Phase 3: Decision Points

阶段3:决策点

Ask user for configuration preferences.
询问用户配置偏好。

Q1: Log Format

Q1:日志格式

OptionWhen to Use
JSON (Recommended for Production)Machine-readable, log aggregation systems
Pretty/Colored (Recommended for Development)Human-readable, local debugging
选项使用场景
JSON(生产环境推荐)机器可读,适用于日志聚合系统
美化/彩色(开发环境推荐)人类可读,适用于本地调试

Q2: Enrichment Fields

Q2:增强字段

FieldDescriptionDefault
correlationId
Request tracking across services✓ Yes
userId
Authenticated user identifier✓ Yes
requestPath
HTTP request path✓ Yes
responseTime
Request duration in ms✓ Yes
machineName
Server hostnameOptional
threadId
Thread identifierOptional
字段描述默认值
correlationId
跨服务的请求追踪✓ 是
userId
已认证用户标识符✓ 是
requestPath
HTTP请求路径✓ 是
responseTime
请求耗时(毫秒)✓ 是
machineName
服务器主机名可选
threadId
线程标识符可选

Q3: Log Sinks

Q3:日志Sink

SinkUse Case
ConsoleAlways enabled
FileLocal persistence, log rotation
SeqStructured log server
ElasticsearchLog aggregation at scale
Sink使用场景
控制台始终启用
文件本地持久化,日志轮转
Seq结构化日志服务器
Elasticsearch大规模日志聚合

Q4: Log Levels by Environment

Q4:按环境配置日志级别

LevelDevelopmentProduction
DefaultDebugInformation
Microsoft.*InformationWarning
System.*InformationWarning
ApplicationDebugInformation

级别开发环境生产环境
默认DebugInformation
Microsoft.*InformationWarning
System.*InformationWarning
应用程序DebugInformation

Phase 4: Generate Configuration

阶段4:生成配置

Generate files based on stack and decisions.
根据技术栈和决策生成文件。

.NET Output Files

.NET输出文件

FilePurpose
Extensions/LoggingExtensions.cs
Service registration
appsettings.json
(update)
Serilog configuration
appsettings.Development.json
(update)
Dev overrides
Generation Process:
  1. Use MCP ref to get current Serilog API
  2. Generate LoggingExtensions.cs with:
    • UseSerilog configuration
    • Request logging middleware
    • Enrichment configuration
  3. Update appsettings.json with Serilog section
Packages to Add:
  • Serilog.AspNetCore
  • Serilog.Sinks.Console
  • Serilog.Sinks.File
    (if File sink selected)
  • Serilog.Enrichers.Environment
    (if machineName selected)
文件用途
Extensions/LoggingExtensions.cs
服务注册
appsettings.json
(更新)
Serilog配置
appsettings.Development.json
(更新)
开发环境覆盖配置
生成流程:
  1. 使用MCP参考获取当前Serilog API
  2. 生成LoggingExtensions.cs,包含:
    • UseSerilog配置
    • 请求日志中间件
    • 增强配置
  3. 更新appsettings.json中的Serilog配置段
需添加的包:
  • Serilog.AspNetCore
  • Serilog.Sinks.Console
  • Serilog.Sinks.File
    (如果选择文件Sink)
  • Serilog.Enrichers.Environment
    (如果选择machineName)

Python Output Files

Python输出文件

FilePurpose
core/logging_config.py
structlog configuration
middleware/logging_middleware.py
Request logging
Generation Process:
  1. Use MCP ref to get current structlog API
  2. Generate logging_config.py with:
    • Processor chain configuration
    • Renderer selection (JSON/Console)
    • Log level configuration
  3. Generate logging_middleware.py for FastAPI
Packages to Add:
  • structlog
  • python-json-logger
    (if JSON format)

文件用途
core/logging_config.py
structlog配置
middleware/logging_middleware.py
请求日志
生成流程:
  1. 使用MCP参考获取当前structlog API
  2. 生成logging_config.py,包含:
    • 处理器链配置
    • 渲染器选择(JSON/控制台)
    • 日志级别配置
  3. 为FastAPI生成logging_middleware.py
需添加的包:
  • structlog
  • python-json-logger
    (如果选择JSON格式)

Phase 5: Validate

阶段5:验证

Verify the configuration works.
Validation Steps:
  1. Check imports: Ensure all packages are available
    • .NET:
      dotnet list package | grep Serilog
    • Python:
      pip list | grep structlog
  2. Syntax check:
    • .NET:
      dotnet build --no-restore
    • Python:
      python -m py_compile core/logging_config.py
  3. Test log output:
    • Start application
    • Make test request
    • Verify log format matches configuration
Expected Log Format:
json
{
  "timestamp": "2026-01-10T12:00:00.000Z",
  "level": "info",
  "message": "Request completed",
  "correlationId": "abc-123",
  "requestPath": "/api/health",
  "responseTime": 45,
  "statusCode": 200
}

验证配置是否可用。
验证步骤:
  1. 检查依赖包: 确保所有包已安装
    • .NET:
      dotnet list package | grep Serilog
    • Python:
      pip list | grep structlog
  2. 语法检查:
    • .NET:
      dotnet build --no-restore
    • Python:
      python -m py_compile core/logging_config.py
  3. 测试日志输出:
    • 启动应用程序
    • 发起测试请求
    • 验证日志格式与配置一致
预期日志格式:
json
{
  "timestamp": "2026-01-10T12:00:00.000Z",
  "level": "info",
  "message": "Request completed",
  "correlationId": "abc-123",
  "requestPath": "/api/health",
  "responseTime": 45,
  "statusCode": 200
}

Return to Coordinator

返回至协调器

Return result to ln-770:
json
{
  "status": "success",
  "files_created": [
    "Extensions/LoggingExtensions.cs",
    "appsettings.json"
  ],
  "packages_added": [
    "Serilog.AspNetCore",
    "Serilog.Sinks.Console"
  ],
  "registration_code": "services.AddLoggingServices(configuration);",
  "message": "Configured structured logging with Serilog"
}

向ln-770返回结果:
json
{
  "status": "success",
  "files_created": [
    "Extensions/LoggingExtensions.cs",
    "appsettings.json"
  ],
  "packages_added": [
    "Serilog.AspNetCore",
    "Serilog.Sinks.Console"
  ],
  "registration_code": "services.AddLoggingServices(configuration);",
  "message": "Configured structured logging with Serilog"
}

Idempotency

幂等性

This skill is idempotent:
  • Phase 1: Check if logging already configured (Grep for Serilog/structlog)
  • If configured: Return
    { "status": "skipped", "message": "Logging already configured" }
  • If not: Proceed with configuration

此工具具备幂等性:
  • 阶段1:检查日志是否已配置(搜索Serilog/structlog)
  • 如果已配置:返回
    { "status": "skipped", "message": "Logging already configured" }
  • 如果未配置:继续执行配置流程

Reference Links

参考链接


Version: 2.0.0 Last Updated: 2026-01-10

版本: 2.0.0 最后更新: 2026-01-10