Skip to content

Log4Net integration should filter SDK logs #2953

@bitsandfoxes

Description

@bitsandfoxes

Particular case:

  1. Have a custom logger and add it to the SDK.
  2. Have an exception.
  3. Have the Sentry server respond with error 429 - Too Many Requests.
  4. With options.Debug = true; the SDK will log an error.
  5. Due to the custom logger that error ends up in our Log4Net integration.
  6. Error gets captured as an event and sent to Sentry.
  7. Goto 3.

Repro:

using System.Net;
using log4net;
using log4net.Config;
using Microsoft.AspNetCore;
using Sentry.Extensibility;

[assembly: XmlConfigurator(Watch = true)]

namespace Sentry.Samples.AspNetCore.Basic;

public class Program
{
    private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseSentry(o =>
            {
                o.Dsn = "http://key@localhost:4999/123";
                o.Debug = true;
                o.DiagnosticLogger = new Log4NetDiagnosticLogger(Log);
            })

            // The App:
            .Configure(app =>
            {
                app.UseRouting();

                // An example ASP.NET Core middleware that throws an
                // exception when serving a request to path: /throw
                app.UseEndpoints(endpoints =>
                {
                    // Reported events will be grouped by route pattern
                    endpoints.MapGet("/throw/{message?}", context => 
                        throw new Exception("An exception thrown from the ASP.NET Core pipeline"));
                });
            })
            .Build();
}

public class Log4NetDiagnosticLogger : IDiagnosticLogger
{
    private readonly ILog _log;

    public Log4NetDiagnosticLogger(ILog log)
    {
        _log = log;
    }

    public bool IsEnabled(SentryLevel level)
    {
        return true;
    }

    public void Log(SentryLevel logLevel, string message, Exception exception = null, params object[] args)
    {
        switch (logLevel)
        {
            case SentryLevel.Debug:
                _log.Debug(message);
                break;
            case SentryLevel.Info:
                _log.Info(message);
                break;
            case SentryLevel.Warning:
                _log.Warn(message);
                break;
            case SentryLevel.Error:
                _log.Error(message);
                break;
            case SentryLevel.Fatal:
                _log.Fatal(message);
                break;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions