dotnet / dotnet/extensions

[API Proposal]: rate limited logging

Open
#4,915 5 comments 1 reaction 0 assignees View on GitHub
api-suggestion area-telemetry
Dominant language
C#
Stars
3.2k
Forks
894
Avg merge
1d 12h
Merged PRs (30d)
23

Description

### Background and motivation

When developing a web service, logs are usually added for unhappy path, for example, a database query failed because the remote server was unreachable. The code would look like this:
```csharp
try
{
QueryDatabase();
}
catch (Exception e)
{
Logger.LogError(e, "Error querying the database");
}
```
One subtle issue with that code that could have very nasty consequences is that if the database is down, all queries will induce an error log. That would create a huge spikes of logs that could overload the logs collector and make it drop logs that were crucial to understand the root cause of the issue.

Because this issue is easy to miss, I think would be great to integrate a token bucket rate limiter in the [LoggerMessageAttribute](https://learn.microsoft.com/en-us/dotnet/core/extensions/logger-message-generator).

https://github.com/dotnet/runtime/issues/82465 seems to be related.

### API Proposal

```cs
namespace Microsoft.Extensions.Logging;

public class LoggerMessageAttribute
{
///
/// Maximum number of logs to restore each replenishment.
///
public int LogsPerPeriod { get; set; }

///
/// Period between replenishments in milliseconds.
///
public int ReplenishmentPeriod { get; set; }
}
```

If `LogsPerPeriod` or `ReplenishmentPeriod` are equal or less than 0 they should be both ignored.

### API Usage

```csharp
try
{
QueryDatabase();
}
catch (DatabaseException e)
{
// A maximum of 5 logs will be sent by second.
LogDatabaseError(e, e.HostName);
}

[LoggerMessage(
EventId = 0,
Level = LogLevel.Error,
Message = "Error querying the database {hostName}"),
LogsPerPeriod = 5,
ReplenishmentPeriod = 1000]
public static partial void LogDatabaseError(ILogger logger, Exception e, string hostName);
```

### Alternative Designs

_No response_

### Risks

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.