Skip to content

Commit

Permalink
Updates SentryAbstractLogger to take in SentryClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernarden committed Jan 8, 2019
1 parent 57d18d7 commit c11ef7e
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions Source/Vima.LoggingAbstractor.Sentry/SentryAbstractLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ namespace Vima.LoggingAbstractor.Sentry
/// <seealso cref="ISentryAbstractLogger" />
public class SentryAbstractLogger : AbstractLoggerBase, ISentryAbstractLogger
{
private readonly IHub _hub;
private readonly ISentryClient _sentryClient;

/// <summary>
/// Initializes a new instance of the <see cref="SentryAbstractLogger"/> class.
/// </summary>
/// <param name="hub">The Sentry hub.</param>
/// <param name="sentryClient">The Sentry client.</param>
/// <param name="minimalLoggingLevel">The minimal logging level.</param>
public SentryAbstractLogger(IHub hub, LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
public SentryAbstractLogger(ISentryClient sentryClient, LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(new AbstractLoggerSettings { MinimalLoggingLevel = minimalLoggingLevel })
{
_hub = hub ?? throw new ArgumentNullException(nameof(hub));
_sentryClient = sentryClient ?? throw new ArgumentNullException(nameof(sentryClient));
}

/// <summary>
/// Initializes a new instance of the <see cref="SentryAbstractLogger"/> class.
/// </summary>
/// <param name="hub">The Sentry hub.</param>
/// <param name="sentryClient">The Sentry client.</param>
/// <param name="settings">The logger's settings.</param>
public SentryAbstractLogger(IHub hub, AbstractLoggerSettings settings)
public SentryAbstractLogger(ISentryClient sentryClient, AbstractLoggerSettings settings)
: base(settings ?? throw new ArgumentNullException(nameof(settings)))
{
_hub = hub ?? throw new ArgumentNullException(nameof(hub));
_sentryClient = sentryClient ?? throw new ArgumentNullException(nameof(sentryClient));
}

/// <summary>
Expand All @@ -54,12 +54,8 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn
return;
}

_hub.WithScope(scope =>
{
SetupSentryScope(scope, loggingLevel, parameters);

_hub.CaptureMessage(message);
});
var scope = CreateSentryScope(loggingLevel, parameters);
_sentryClient.CaptureEvent(new SentryEvent { Message = message }, scope);
}

/// <summary>
Expand All @@ -75,23 +71,21 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev
return;
}

_hub.WithScope(scope =>
{
SetupSentryScope(scope, loggingLevel, parameters);

_hub.CaptureException(exception);
});
var scope = CreateSentryScope(loggingLevel, parameters);
_sentryClient.CaptureEvent(new SentryEvent(exception), scope);
}

private static Dictionary<string, string> GenerateTags(IEnumerable<ILoggingParameter> parameters)
{
return parameters.ExtractTags().ToDictionary(tag => tag);
}

private void SetupSentryScope(BaseScope scope, LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
private Scope CreateSentryScope(LoggingLevel loggingLevel, IEnumerable<ILoggingParameter> parameters)
{
var loggingParameters = GetGlobalAndLocalLoggingParameters(parameters);
Dictionary<string, string> tags = GenerateTags(loggingParameters);

var scope = new Scope(new SentryOptions());
scope.SetTags(tags);
scope.Level = LoggingLevelMapper.ConvertLoggingLevelToSentryLevel(loggingLevel);

Expand All @@ -107,6 +101,8 @@ private void SetupSentryScope(BaseScope scope, LoggingLevel loggingLevel, IEnume
{
scope.SetExtra($"Extra #{extraCount++}", data);
}

return scope;
}
}
}
Expand Down

0 comments on commit c11ef7e

Please sign in to comment.