-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Introduce a logger provider that counts warnings and errors #54846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Diagnostics.Metrics; | ||
using System.Globalization; | ||
using System.Runtime.CompilerServices; | ||
using System.Xml.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Win32; | ||
|
||
namespace Microsoft.AspNetCore.DataProtection; | ||
|
||
internal sealed class MetricsLoggerProvider : ILoggerProvider | ||
{ | ||
public const string MeterName = "Microsoft.AspNetCore.DataProtection"; | ||
amcasey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private const string CategoryNamePrefix = "Microsoft.AspNetCore.DataProtection"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I include a dot at the end? It's a namespace we "own", so I'm not that worried about picking up, e.g., "DataProtection2". |
||
|
||
private readonly Meter? _meter; | ||
|
||
private readonly ILogger _logger; | ||
|
||
public MetricsLoggerProvider() | ||
{ | ||
_logger = NullLogger.Instance; | ||
} | ||
|
||
public MetricsLoggerProvider(IMeterFactory meterFactory) | ||
{ | ||
_meter = meterFactory.Create(MeterName); | ||
|
||
var counter = _meter.CreateCounter<long>( | ||
"aspnetcore.data_protection.log_messages", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We went with "rate_limiting", so I went with "data_protection", rather than "dataprotection". |
||
unit: "{message}", | ||
description: "Number of log records that have been logged."); | ||
|
||
_logger = new MetricsLogger(counter); | ||
} | ||
|
||
ILogger ILoggerProvider.CreateLogger(string categoryName) => | ||
categoryName.StartsWith(CategoryNamePrefix, StringComparison.Ordinal) | ||
? _logger | ||
: NullLogger.Instance; | ||
|
||
void IDisposable.Dispose() => _meter?.Dispose(); | ||
|
||
private sealed class MetricsLogger : ILogger | ||
{ | ||
private readonly Counter<long> _counter; | ||
|
||
public MetricsLogger(Counter<long> counter) | ||
{ | ||
_counter = counter; | ||
} | ||
|
||
IDisposable? ILogger.BeginScope<TState>(TState state) => null; | ||
|
||
bool ILogger.IsEnabled(LogLevel logLevel) | ||
{ | ||
switch (logLevel) | ||
{ | ||
case LogLevel.Critical: | ||
case LogLevel.Error: | ||
case LogLevel.Warning: | ||
return _counter.Enabled; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState _state, Exception? _exception, Func<TState, Exception?, string> _formatter) | ||
amcasey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var levelName = logLevel switch | ||
{ | ||
LogLevel.Critical => "critical", | ||
LogLevel.Error => "error", | ||
LogLevel.Warning => "warning", | ||
_ => null, | ||
}; | ||
|
||
if (levelName is null || !_counter.Enabled) | ||
{ | ||
return; | ||
} | ||
|
||
var tags = new TagList( | ||
[ | ||
new("aspnetcore.data_protection.log_message_id", eventId.Name ?? eventId.Id.ToString(CultureInfo.InvariantCulture)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arguably, these should be dotnet- or Microsoft.Extensions- scoped, but that seems premature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just personal thoughts, maybe use "aspnetcore.data_protection.logs.event_name" and "aspnetcore.data_protection.logs.log_level" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine by me |
||
new("aspnetcore.data_protection.log_level", levelName), | ||
]); | ||
_counter.Add(1, tags); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.