From befabdb8252c4d07312309faab41712f9bca1d3a Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Sat, 24 Nov 2018 20:01:42 +0100 Subject: [PATCH] feat: support breadcrumb --- src/Sentry.Serilog/SentrySerilogOptions.cs | 43 ++++++++++++++++++ src/Sentry.Serilog/SentrySink.cs | 35 +++++++------- src/Sentry.Serilog/SentrySinkExtensions.cs | 53 +++++++++++++++++++++- 3 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 src/Sentry.Serilog/SentrySerilogOptions.cs diff --git a/src/Sentry.Serilog/SentrySerilogOptions.cs b/src/Sentry.Serilog/SentrySerilogOptions.cs new file mode 100644 index 0000000000..a9abc2cbec --- /dev/null +++ b/src/Sentry.Serilog/SentrySerilogOptions.cs @@ -0,0 +1,43 @@ +using System; +using Sentry.Protocol; +using Serilog.Events; + +namespace Sentry.Serilog +{ + /// + /// Sentry Options for Serilog logging + /// + /// + public class SentrySerilogOptions : SentryOptions + { + /// + /// Whether to initialize this SDK through this integration + /// + public bool InitializeSdk { get; set; } = true; + + /// + /// Minimum log level to send an event. + /// + /// + /// Events with this level or higher will be sent to Sentry. + /// + /// + /// The minimum event level. + /// + public LogEventLevel MinimumEventLevel { get; set; } + + /// + /// Minimum log level to record a breadcrumb. + /// + /// Events with this level or higher will be stored as + /// + /// The minimum breadcrumb level. + /// + public LogEventLevel MinimumBreadcrumbLevel { get; set; } + + /// + /// Optional + /// + public IFormatProvider FormatProvider { get; set; } + } +} diff --git a/src/Sentry.Serilog/SentrySink.cs b/src/Sentry.Serilog/SentrySink.cs index 751f2dc0f3..9e4e72a169 100644 --- a/src/Sentry.Serilog/SentrySink.cs +++ b/src/Sentry.Serilog/SentrySink.cs @@ -9,11 +9,14 @@ namespace Sentry.Serilog { + /// + /// Sentry Sink for Serilog + /// + /// + /// public sealed class SentrySink : ILogEventSink, IDisposable { - private readonly IFormatProvider _formatProvider; - private readonly Func _initAction; - private volatile IDisposable _sdkHandle; + private SentrySerilogOptions _options; private readonly object _initSync = new object(); @@ -22,24 +25,23 @@ internal static readonly (string Name, string Version) NameAndVersion 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) + /// + /// Creates a new instance of . + /// + /// The Sentry Serilog options to configure the sink. + public SentrySink(SentrySerilogOptions options) + : this(options, HubAdapter.Instance) { } internal SentrySink( - IFormatProvider formatProvider, - Func initAction, + SentrySerilogOptions options, IHub hub) { - Debug.Assert(initAction != null); + Debug.Assert(options != null); Debug.Assert(hub != null); - _formatProvider = formatProvider; - _initAction = initAction; Hub = hub; } @@ -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() @@ -109,9 +111,6 @@ private IEnumerable> GetLoggingEventProperties(LogE } } - public void Dispose() - { - _sdkHandle?.Dispose(); - } + public void Dispose() => _sdkHandle?.Dispose(); } } diff --git a/src/Sentry.Serilog/SentrySinkExtensions.cs b/src/Sentry.Serilog/SentrySinkExtensions.cs index 669da7040a..415fa30e2f 100644 --- a/src/Sentry.Serilog/SentrySinkExtensions.cs +++ b/src/Sentry.Serilog/SentrySinkExtensions.cs @@ -7,13 +7,62 @@ namespace Sentry.Serilog { public static class SentrySinkExtensions { + /// + /// Add Sentry Serilog Sink. + /// + /// The logger configuration. + /// The Sentry DSN. + /// Minimum log level to record a breadcrumb. + /// Minimum log level to send an event. + /// The Serilog format provider. + /// 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 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(); + //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); } } }