# ThrottledLogging **Repository Path**: coldhighsun/ThrottledLogging ## Basic Information - **Project Name**: ThrottledLogging - **Description**: 基于时间间隔的 Microsoft.Extensions.Logging 日志限流器,可按 key 抑制重复日志,并在恢复输出时报告被抑制的条数。 - **Primary Language**: C# - **License**: MIT - **Default Branch**: main - **Homepage**: https://github.com/coldhighsun/ThrottledLogging - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2026-04-11 - **Last Updated**: 2026-11-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: 日志, log, logging, throttle, rate-limit ## README
# ThrottledLogging [![CI](https://github.com/coldhighsun/ThrottledLogging/actions/workflows/ci.yml/badge.svg)](https://github.com/coldhighsun/ThrottledLogging/actions/workflows/ci.yml) [![nuget](https://img.shields.io/nuget/v/ThrottledLogging?label=nuget)](https://www.nuget.org/packages/ThrottledLogging) [![NuGet Downloads](https://img.shields.io/nuget/dt/ThrottledLogging)](https://www.nuget.org/packages/ThrottledLogging) [![Latest Release](https://img.shields.io/github/v/release/coldhighsun/ThrottledLogging)](https://github.com/coldhighsun/ThrottledLogging/releases/latest) [![Latest Pre-release](https://img.shields.io/github/v/release/coldhighsun/ThrottledLogging?include_prereleases&label=pre-release)](https://github.com/coldhighsun/ThrottledLogging/releases) [![Open Issues](https://img.shields.io/github/issues/coldhighsun/ThrottledLogging)](https://github.com/coldhighsun/ThrottledLogging/issues) [![Open PRs](https://img.shields.io/github/issues-pr/coldhighsun/ThrottledLogging)](https://github.com/coldhighsun/ThrottledLogging/pulls) [![Last Commit](https://img.shields.io/github/last-commit/coldhighsun/ThrottledLogging)](https://github.com/coldhighsun/ThrottledLogging/commits/main) [![GitHub Stars](https://img.shields.io/github/stars/coldhighsun/ThrottledLogging?style=flat)](https://github.com/coldhighsun/ThrottledLogging/stargazers) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/coldhighsun/ThrottledLogging/blob/main/LICENSE) [![.NET](https://img.shields.io/badge/.NET-10.0-512BD4?logo=dotnet)](https://dotnet.microsoft.com/) [English](https://github.com/coldhighsun/ThrottledLogging/blob/main/README.md) | 简体中文 | [繁體中文](https://github.com/coldhighsun/ThrottledLogging/blob/main/README.zh-TW.md)
--- 基于时间间隔的 `Microsoft.Extensions.Logging` 日志限流器,可按 key 抑制重复日志,并在恢复输出时报告被抑制的条数。 ## 项目结构 | 项目 | 说明 | |---|---| | `src/ThrottledLogging` | NuGet 库 | | `examples/GettingStarted` | 控制台示例,涵盖所有功能 | ## 安装 ``` dotnet add package ThrottledLogging ``` ## 用法 在任意 `ILogger` 上调用限流扩展方法。每次调用需传入一个 **key**(用于标识被限流的日志消息)和一个 **interval**(两次输出之间的最短间隔)。 ```csharp // "disk-full" 这个 key 每分钟最多输出一次。 // 抑制后下次恢复输出时,会自动追加"(N messages suppressed)"。 logger.LogWarningThrottled("disk-full", TimeSpan.FromMinutes(1), "Disk usage is above {Percent}%", usage); ``` 可用方法与标准 `ILogger` API 一一对应: - `LogTraceThrottled` - `LogDebugThrottled` - `LogInformationThrottled` - `LogWarningThrottled` - `LogErrorThrottled` - `LogCriticalThrottled` 每个方法的签名为 `(string key, TimeSpan interval, string? messageTemplate, params ReadOnlySpan args)`。每个方法还有一个在 messageTemplate 之前接受 `Exception?` 的重载,与标准 `ILogger` 的用法一致: ```csharp logger.LogErrorThrottled("payment-failed", TimeSpan.FromMinutes(1), exception, "Payment failed for order {OrderId}", orderId); ``` ### 抑制计数 当某条被限流的消息在经历一次或多次抑制后重新允许输出时,抑制次数会自动追加到消息末尾。追加的后缀已本地化,在 `zh-CN` 环境下输出为: ``` Disk usage is above 95% (3个消息被隐藏) ``` 如需固定语言而不是跟随系统当前区域自动判断,可以设置 `ThrottledLogger.Language`: ```csharp ThrottledLogger.Language = CultureInfo.GetCultureInfo("zh-Hans"); ``` 将其设回 `null`(默认值)即可恢复跟随 `CultureInfo.CurrentUICulture` 自动判断。 ### 多个独立 key 每个 key 拥有独立的限流计数器,互不干扰: ```csharp logger.LogErrorThrottled("service-a-down", TimeSpan.FromSeconds(5), "Service A is unreachable"); logger.LogErrorThrottled("service-b-down", TimeSpan.FromSeconds(5), "Service B is unreachable"); ``` ### 重置与查询限流状态 使用 `ResetThrottle` 可以手动清除某个 key 的限流状态(下一次调用会被当作首次调用处理);使用 `TryGetThrottledSuppressedCount` 可以在不写日志的情况下查询某个 key 当前被抑制的次数: ```csharp logger.ResetThrottle("disk-full"); if (logger.TryGetThrottledSuppressedCount("disk-full", out var suppressed)) { Console.WriteLine($"当前有 {suppressed} 条消息被抑制"); } ``` ### 全局配置 可在应用启动时调整全局的过期时间和清理周期: ```csharp // 空闲条目 30 分钟后过期;每 15 分钟执行一次清理。 ThrottledLogger.Configure(expiry: TimeSpan.FromMinutes(30), cleanupPeriod: TimeSpan.FromMinutes(15)); ``` | 参数 | 默认值 | 说明 | |---|---|---| | `expiry` | 1 小时 | 空闲条目(无输出也无被抑制的调用)在被移除前的保留时长。仍处于节流间隔内的条目不会被移除,因此应避免将超长间隔与数量无上限的 key 组合使用。 | | `cleanupPeriod` | 1 小时 | 后台清理定时器的运行间隔。必须不小于 1 毫秒且不超过约 49.7 天(4294967294 毫秒);传入 `Timeout.InfiniteTimeSpan` 可禁用清理。 | ## 环境要求 .NET 10 或更高版本。 ## 构建 ```bash dotnet build dotnet test dotnet pack ``` `dotnet test` 通过 [Microsoft.Testing.Platform](https://aka.ms/testingplatform)(MTP)运行,相关配置见 `global.json`。