sentry-dotnet
sentry-dotnet copied to clipboard
Log4Net integration should filter SDK logs
Particular case:
- Have a custom logger and add it to the SDK.
- Have an exception.
- Have the Sentry server respond with error
429 - Too Many Requests
. - With
options.Debug = true;
the SDK will log an error. - Due to the custom logger that error ends up in our Log4Net integration.
- Error gets captured as an event and sent to Sentry.
- 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;
}
}
}