-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSentryStructuredLogger.cs
More file actions
96 lines (84 loc) · 2.73 KB
/
SentryStructuredLogger.cs
File metadata and controls
96 lines (84 loc) · 2.73 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
90
91
92
93
94
95
96
using Microsoft.Extensions.Logging;
namespace Sentry.Extensions.Logging;
[Experimental(Infrastructure.DiagnosticId.ExperimentalFeature)]
internal sealed class SentryStructuredLogger : ILogger
{
private readonly string _categoryName;
private readonly SentryLoggingOptions _options;
private readonly IHub _hub;
internal SentryStructuredLogger(string categoryName, SentryLoggingOptions options, IHub hub)
{
_categoryName = categoryName;
_options = options;
_hub = hub;
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return NullDisposable.Instance;
}
public bool IsEnabled(LogLevel logLevel)
{
return _hub.IsEnabled
&& _options.Experimental.EnableLogs
&& logLevel != LogLevel.None
&& logLevel >= _options.ExperimentalLogging.MinimumLogLevel;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
// not quite ideal as this is a boxing allocation from Microsoft.Extensions.Logging.FormattedLogValues
/*
string? template = null;
object[]? parameters = null;
if (state is IReadOnlyList<KeyValuePair<string, object?>> formattedLogValues)
{
foreach (var formattedLogValue in formattedLogValues)
{
if (formattedLogValue.Key == "{OriginalFormat}" && formattedLogValue.Value is string formattedString)
{
template = formattedString;
break;
}
}
}
*/
string message = formatter.Invoke(state, exception);
switch (logLevel)
{
case LogLevel.Trace:
_hub.Logger.LogTrace(message);
break;
case LogLevel.Debug:
_hub.Logger.LogDebug(message);
break;
case LogLevel.Information:
_hub.Logger.LogInfo(message);
break;
case LogLevel.Warning:
_hub.Logger.LogWarning(message);
break;
case LogLevel.Error:
_hub.Logger.LogError(message);
break;
case LogLevel.Critical:
_hub.Logger.LogFatal(message);
break;
case LogLevel.None:
default:
break;
}
}
}
file sealed class NullDisposable : IDisposable
{
public static NullDisposable Instance { get; } = new NullDisposable();
private NullDisposable()
{
}
public void Dispose()
{
}
}