Skip to content

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static IDataProtectionBuilder AddDataProtection(this IServiceCollection s
ArgumentNullThrowHelper.ThrowIfNull(services);

services.TryAddSingleton<IActivator, TypeForwardingActivator>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, MetricsLoggerProvider>());
services.AddOptions();
AddDataProtectionServices(services);

Expand Down
91 changes: 91 additions & 0 deletions src/DataProtection/DataProtection/src/MetricsLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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";

private readonly Meter? _meter;

private readonly ILogger _logger;

public MetricsLoggerProvider()
{
_logger = NullLogger.Instance;
}

public MetricsLoggerProvider(IMeterFactory meterFactory)
{
_meter = meterFactory.Create(MeterName);

var errorCounter = _meter.CreateCounter<long>(
"errors",
unit: "{error}",
description: "Number of errors that have occurred.");
var warningCounter = _meter.CreateCounter<long>(
"warnings",
unit: "{warning}",
description: "Number of warnings that have occurred.");

_logger = new MetricsLogger(errorCounter, warningCounter);
}

ILogger ILoggerProvider.CreateLogger(string _categoryName) => _logger;

void IDisposable.Dispose() => _meter?.Dispose();

private sealed class MetricsLogger : ILogger
{
private readonly Counter<long> _errorCounter;
private readonly Counter<long> _warningCounter;

public MetricsLogger(Counter<long> errorCounter, Counter<long> warningCounter)
{
_errorCounter = errorCounter;
_warningCounter = warningCounter;
}

IDisposable? ILogger.BeginScope<TState>(TState state) => null;

bool ILogger.IsEnabled(LogLevel logLevel) => logLevel switch
{
LogLevel.Error => _errorCounter.Enabled,
LogLevel.Warning => _warningCounter.Enabled,
_ => false,
};

void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState _state, Exception? _exception, Func<TState, Exception?, string> _formatter)
{
switch(logLevel)
{
case LogLevel.Error:
if (_errorCounter.Enabled)
{
var tags = new TagList([new("id", eventId.Name ?? eventId.Id.ToString(CultureInfo.InvariantCulture))]);
_errorCounter.Add(1, tags);
}
break;
case LogLevel.Warning:
if (_warningCounter.Enabled)
{
var tags = new TagList([new("id", eventId.Name ?? eventId.Id.ToString(CultureInfo.InvariantCulture))]);
_warningCounter.Add(1, tags);
}
break;
}
}
}
}