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:

Copied
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.

Copied
// 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:

Copied
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();
        }
    }
}