Diagnostic Logger
Sentry's SDK includes its own internal logger to report diagnostics that may be useful when troubleshooting your Sentry configuration. To enable logging, set the Debug
option to true
:
options =>
{
options.Dsn = "https://examplePublicKey@o0.ingest.sentry.io/0";
// Enable debug mode to write diagnostic messages
options.Debug = true;
// By default it's already the most verbose level: Debug
// You can use this make this less noisy by changing it to
// a less verbose level such as `Information` or `Warning`.
options.DiagnosticLevel = SentryLevel.Debug;
});
Customize Implementation
By default, Sentry will write diagnostic messages to console. This may not be optimal in some circumstances; for example, when running applications that don't have a visible console window attached.
To use a custom implementation of IDiagnosticLogger
, you can pass it to the DiagnosticLogger
option. Sentry comes with two implementations out of the box: ConsoleDiagnosticLogger
and TraceDiagnosticLogger
.
// Provide a custom logger
options.DiagnosticLogger = new TraceDiagnosticLogger(SentryLevel.Debug);
The logger TraceDiagnosticLogger
in the example uses the .NET's Trace class. As a result, you can view the SDK logs inside Visual Studio's Debug log window, which is useful for technologies that don't have a Console to see the log messages, such as WinForms
, WPF
and ASP.NET
.
You can also create your own implementation of IDiagnosticLogger
to fully customize logging behavior. For example, to naively append diagnostic messages to a file:
using System;
using System.Globalization;
using System.IO;
using Sentry.Extensibility;
using Sentry.Protocol;
public class FileAppenderDiagnosticLogger : IDiagnosticLogger, IDisposable
{
private readonly StreamWriter _writer;
private readonly SentryLevel _minimalLevel;
public FileAppenderDiagnosticLogger(string filePath, SentryLevel minimalLevel)
{
_writer = new StreamWriter(filePath);
_minimalLevel = minimalLevel;
}
public bool IsEnabled(SentryLevel level) => level >= _minimalLevel;
public void Log(SentryLevel logLevel, string message, Exception exception = null, params object[] args)
{
lock (_writer)
{
_writer.Write("{0} - {1}: ", DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ss.ffffZ", DateTimeFormatInfo.InvariantInfo), logLevel);
_writer.Write("");
_writer.Write(message, args);
if (exception is Exception)
{
_writer.Write("Exception: ");
_writer.Write(exception);
}
_writer.WriteLine();
_writer.Flush();
}
}
public void Dispose()
{
lock (_writer)
{
_writer.Dispose();
}
}
}
For a production environment you'd want to extend this so the file doesn't grow indefinitely and is eventually rotated to avoid filling up the disk space.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- nuget:Sentry.AspNetCore
- Version:
- 3.29.1
- Repository:
- https://github.com/getsentry/sentry-dotnet