Skip to content

Commit

Permalink
feat: support breadcrumb
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed Nov 24, 2018
1 parent 2964555 commit befabdb
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 20 deletions.
43 changes: 43 additions & 0 deletions src/Sentry.Serilog/SentrySerilogOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Sentry.Protocol;
using Serilog.Events;

namespace Sentry.Serilog
{
/// <summary>
/// Sentry Options for Serilog logging
/// </summary>
/// <inheritdoc />
public class SentrySerilogOptions : SentryOptions
{
/// <summary>
/// Whether to initialize this SDK through this integration
/// </summary>
public bool InitializeSdk { get; set; } = true;

/// <summary>
/// Minimum log level to send an event.
/// </summary>
/// <remarks>
/// Events with this level or higher will be sent to Sentry.
/// </remarks>
/// <value>
/// The minimum event level.
/// </value>
public LogEventLevel MinimumEventLevel { get; set; }

/// <summary>
/// Minimum log level to record a breadcrumb.
/// </summary>
/// <remarks>Events with this level or higher will be stored as <see cref="Breadcrumb"/></remarks>
/// <value>
/// The minimum breadcrumb level.
/// </value>
public LogEventLevel MinimumBreadcrumbLevel { get; set; }

/// <summary>
/// Optional <see cref="IFormatProvider"/>
/// </summary>
public IFormatProvider FormatProvider { get; set; }
}
}
35 changes: 17 additions & 18 deletions src/Sentry.Serilog/SentrySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

namespace Sentry.Serilog
{
/// <summary>
/// Sentry Sink for Serilog
/// </summary>
/// <inheritdoc cref="IDisposable" />
/// <inheritdoc cref="ILogEventSink" />
public sealed class SentrySink : ILogEventSink, IDisposable
{
private readonly IFormatProvider _formatProvider;
private readonly Func<string, IDisposable> _initAction;
private volatile IDisposable _sdkHandle;
private SentrySerilogOptions _options;

private readonly object _initSync = new object();

Expand All @@ -22,24 +25,23 @@ public sealed class SentrySink : ILogEventSink, IDisposable

private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name;

internal IHub Hub { get; set; }
private IHub _hub;

public string Dsn { get; set; }

public SentrySink(IFormatProvider formatProvider)
: this(formatProvider, SentrySdk.Init, HubAdapter.Instance)
/// <summary>
/// Creates a new instance of <see cref="SentrySink"/>.
/// </summary>
/// <param name="options">The Sentry Serilog options to configure the sink.</param>
public SentrySink(SentrySerilogOptions options)
: this(options, HubAdapter.Instance)
{ }

internal SentrySink(
IFormatProvider formatProvider,
Func<string, IDisposable> initAction,
SentrySerilogOptions options,
IHub hub)
{
Debug.Assert(initAction != null);
Debug.Assert(options != null);
Debug.Assert(hub != null);

_formatProvider = formatProvider;
_initAction = initAction;
Hub = hub;
}

Expand Down Expand Up @@ -78,7 +80,7 @@ public void Emit(LogEvent logEvent)
},
LogEntry = new LogEntry
{
Formatted = logEvent.RenderMessage(_formatProvider),
Formatted = logEvent.RenderMessage(_options.FormatProvider),
Message = logEvent.MessageTemplate.Text
},
Level = logEvent.Level.ToSentryLevel()
Expand Down Expand Up @@ -109,9 +111,6 @@ public void Emit(LogEvent logEvent)
}
}

public void Dispose()
{
_sdkHandle?.Dispose();
}
public void Dispose() => _sdkHandle?.Dispose();
}
}
53 changes: 51 additions & 2 deletions src/Sentry.Serilog/SentrySinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,62 @@ namespace Sentry.Serilog
{
public static class SentrySinkExtensions
{
/// <summary>
/// Add Sentry Serilog Sink.
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="dsn">The Sentry DSN.</param>
/// <param name="minimumBreadcrumbLevel">Minimum log level to record a breadcrumb.</param>
/// <param name="minimumEventLevel">Minimum log level to send an event.</param>
/// <param name="formatProvider">The Serilog format provider.</param>
/// <returns></returns>
public static LoggerConfiguration Sentry(
this LoggerSinkConfiguration loggerConfiguration,
string dsn = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LogEventLevel minimumBreadcrumbLevel = LogEventLevel.Information,
LogEventLevel minimumEventLevel = LogEventLevel.Error,
IFormatProvider formatProvider = null)
=> loggerConfiguration.Sentry(o =>
{
if (dsn != null)
{
o.Dsn = new Dsn(dsn);
}
o.MinimumBreadcrumbLevel = minimumBreadcrumbLevel;
o.MinimumEventLevel = minimumEventLevel;
o.FormatProvider = formatProvider;
});

public static LoggerConfiguration Sentry(
this LoggerSinkConfiguration loggerConfiguration,
Action<SentrySerilogOptions> configureOptions)
{
return loggerConfiguration.Sink(new SentrySink(formatProvider) { Dsn = dsn }, restrictedToMinimumLevel);
var options = new SentrySerilogOptions();
configureOptions?.Invoke(options);


if (options.DiagnosticLogger == null)
{
/var logger = factory.CreateLogger<ISentryClient>();
//options.DiagnosticLogger = new MelDiagnosticLogger(logger, options.DiagnosticsLevel);
}

IHub hub;
if (options.InitializeSdk)
{
hub = new OptionalHub(options);
}
else
{
// Access to whatever the SentrySdk points to (disabled or initialized via SentrySdk.Init)
hub = HubAdapter.Instance;
}

//factory.AddProvider(new SentryLoggerProvider(hub, SystemClock.Clock, options));
//return factory;

var minimalOverall = (LogEventLevel)Math.Min((int)options.MinimumBreadcrumbLevel, (int)options.MinimumEventLevel);
return loggerConfiguration.Sink(new SentrySink(formatProvider) { Dsn = dsn }, minimalOverall);
}
}
}

0 comments on commit befabdb

Please sign in to comment.