-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSentryLoggerProvider.cs
More file actions
89 lines (77 loc) · 2.71 KB
/
SentryLoggerProvider.cs
File metadata and controls
89 lines (77 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Sentry.Infrastructure;
using Sentry.Protocol;
using Sentry.Reflection;
namespace Sentry.Extensions.Logging
{
/// <summary>
/// Sentry Logger Provider.
/// </summary>
[ProviderAlias("Sentry")]
public class SentryLoggerProvider : ILoggerProvider
{
private readonly ISystemClock _clock;
private readonly SentryLoggingOptions _options;
private readonly IDisposable _scope;
private readonly IDisposable _disposableHub;
internal IHub Hub { get; }
internal static readonly SdkVersion NameAndVersion
= typeof(SentryLogger).Assembly.GetNameAndVersion();
private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name;
/// <summary>
/// Creates a new instance of <see cref="SentryLoggerProvider"/>.
/// </summary>
/// <param name="options">The Options.</param>
/// <param name="hub">The Hub.</param>
public SentryLoggerProvider(IOptions<SentryLoggingOptions> options, IHub hub)
: this(hub,
SystemClock.Clock,
options.Value)
{ }
internal SentryLoggerProvider(
IHub hub,
ISystemClock clock,
SentryLoggingOptions options)
{
Debug.Assert(options != null);
Debug.Assert(clock != null);
Debug.Assert(hub != null);
_disposableHub = hub as IDisposable;
Hub = hub;
_clock = clock;
_options = options;
if (hub.IsEnabled)
{
_scope = hub.PushScope();
hub.ConfigureScope(s =>
{
s.Sdk.Name = Constants.SdkName;
s.Sdk.Version = NameAndVersion.Version;
s.Sdk.AddPackage(ProtocolPackageName, NameAndVersion.Version);
});
// Add scope configuration to hub from options
foreach (var callback in options.ConfigureScopeCallbacks)
{
hub.ConfigureScope(callback);
}
}
}
/// <summary>
/// Creates a logger for the category.
/// </summary>
/// <param name="categoryName">Category name.</param>
/// <returns>A logger.</returns>
public ILogger CreateLogger(string categoryName) => new SentryLogger(categoryName, _options, _clock, Hub);
/// <summary>
/// Dispose.
/// </summary>
public void Dispose()
{
_scope?.Dispose();
_disposableHub?.Dispose();
}
}
}