feat(batch4-task3): implement error and rate-limit logging helpers
This commit is contained in:
@@ -171,6 +171,83 @@ public sealed partial class NatsServer
|
||||
action(logger);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs an error with a scope.
|
||||
/// Mirrors Go <c>Server.Errors()</c>.
|
||||
/// </summary>
|
||||
public void Errors(object scope, Exception e)
|
||||
{
|
||||
ExecuteLogCall(l => l.Errorf("{0} - {1}", scope, ErrorContextHelper.UnpackIfErrorCtx(e)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs an error with context.
|
||||
/// Mirrors Go <c>Server.Errorc()</c>.
|
||||
/// </summary>
|
||||
public void Errorc(string ctx, Exception e)
|
||||
{
|
||||
ExecuteLogCall(l => l.Errorf("{0}: {1}", ctx, ErrorContextHelper.UnpackIfErrorCtx(e)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs an error with scope and context.
|
||||
/// Mirrors Go <c>Server.Errorsc()</c>.
|
||||
/// </summary>
|
||||
public void Errorsc(object scope, string ctx, Exception e)
|
||||
{
|
||||
ExecuteLogCall(l => l.Errorf("{0} - {1}: {2}", scope, ctx, ErrorContextHelper.UnpackIfErrorCtx(e)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rate-limited warning based on the raw format string.
|
||||
/// Mirrors Go <c>Server.rateLimitFormatWarnf()</c>.
|
||||
/// </summary>
|
||||
internal void RateLimitFormatWarnf(string format, params object[] args)
|
||||
{
|
||||
if (!_rateLimitLogging.TryAdd(format, DateTime.UtcNow))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var statement = string.Format(format, args);
|
||||
ExecuteLogCall(l => l.Warnf("{0}", statement));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rate-limited warning based on rendered statement.
|
||||
/// Mirrors Go <c>Server.RateLimitWarnf()</c>.
|
||||
/// </summary>
|
||||
public void RateLimitWarnf(string format, params object[] args)
|
||||
{
|
||||
var statement = string.Format(format, args);
|
||||
if (!_rateLimitLogging.TryAdd(statement, DateTime.UtcNow))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ExecuteLogCall(l => l.Warnf("{0}", statement));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rate-limited debug logging based on rendered statement.
|
||||
/// Mirrors Go <c>Server.RateLimitDebugf()</c>.
|
||||
/// </summary>
|
||||
public void RateLimitDebugf(string format, params object[] args)
|
||||
{
|
||||
var statement = string.Format(format, args);
|
||||
if (!_rateLimitLogging.TryAdd(statement, DateTime.UtcNow))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Interlocked.CompareExchange(ref _debugEnabled, 0, 0) == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ExecuteLogCall(l => l.Debugf("{0}", statement));
|
||||
}
|
||||
|
||||
private static ILogger ToMicrosoftLogger(INatsLogger? logger)
|
||||
{
|
||||
return logger switch
|
||||
|
||||
Reference in New Issue
Block a user