-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
SentryAppender.cs
213 lines (177 loc) · 6.47 KB
/
SentryAppender.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using Sentry.Internal.Extensions;
namespace Sentry.Log4Net;
/// <summary>
/// Sentry appender for log4net.
/// </summary>
public class SentryAppender : AppenderSkeleton
{
private readonly Func<string, IDisposable> _initAction;
private volatile IDisposable? _sdkHandle;
private readonly object _initSync = new();
internal static readonly SdkVersion NameAndVersion
= typeof(SentryAppender).Assembly.GetNameAndVersion();
private static readonly string ProtocolPackageName = "nuget:" + NameAndVersion.Name;
private readonly IHub _hub;
/// <summary>
/// Sentry DSN.
/// </summary>
public string? Dsn { get; set; }
/// <summary>
/// Whether to send the Identity or not.
/// </summary>
public bool SendIdentity { get; set; }
/// <summary>
/// Environment to send in the event.
/// </summary>
public string? Environment { get; set; }
/// <summary>
/// Lowest level required for a log message to become an event.
/// Every level above threshold and below this level will become a breadcrumb.
/// </summary>
public Level? MinimumEventLevel { get; set; }
/// <summary>
/// log4net SDK name.
/// </summary>
/// <see href="https://github.com/getsentry/sentry-release-registry" />
internal const string SdkName = "sentry.dotnet.log4net";
/// <summary>
/// Creates a new instance of the <see cref="SentryAppender"/>.
/// </summary>
public SentryAppender() : this(SentrySdk.Init, HubAdapter.Instance)
{ }
internal SentryAppender(
Func<string, IDisposable> initAction,
IHub hubGetter)
{
_initAction = initAction;
_hub = hubGetter;
}
/// <summary>
/// Append log.
/// </summary>
/// <param name="loggingEvent">The event.</param>
protected override void Append(LoggingEvent loggingEvent)
{
// Not to throw on code that ignores nullability warnings.
if (loggingEvent.IsNull())
{
return;
}
if (!_hub.IsEnabled && _sdkHandle == null)
{
if (Dsn == null)
{
return;
}
lock (_initSync)
{
// ReSharper disable once NonAtomicCompoundOperator Double init guarded by the lock
_sdkHandle ??= _initAction(Dsn);
}
}
var exception = loggingEvent.ExceptionObject ?? loggingEvent.MessageObject as Exception;
if (MinimumEventLevel is not null && loggingEvent.Level < MinimumEventLevel)
{
var message = !string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage) ? loggingEvent.RenderedMessage : string.Empty;
var category = loggingEvent.LoggerName;
var level = loggingEvent.ToBreadcrumbLevel();
IDictionary<string, string> data = GetLoggingEventProperties(loggingEvent)
.Where(kvp => kvp.Value != null)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value!.ToString() ?? "");
_hub.AddBreadcrumb(message, category, type: null, data, level ?? default);
return;
}
var evt = new SentryEvent(exception)
{
Logger = loggingEvent.LoggerName,
Level = loggingEvent.ToSentryLevel()
};
if (evt.Sdk is { } sdk)
{
sdk.Name = SdkName;
sdk.Version = NameAndVersion.Version;
if (NameAndVersion.Version is { } version)
{
sdk.AddPackage(ProtocolPackageName, version);
}
}
if (!string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage))
{
evt.Message = loggingEvent.RenderedMessage;
}
evt.SetExtras(GetLoggingEventProperties(loggingEvent));
if (SendIdentity && !string.IsNullOrEmpty(loggingEvent.Identity))
{
evt.User = new SentryUser
{
Id = loggingEvent.Identity
};
}
if (!string.IsNullOrWhiteSpace(Environment))
{
evt.Environment = Environment;
}
_hub.CaptureEvent(evt);
}
private static IEnumerable<KeyValuePair<string, object?>> GetLoggingEventProperties(LoggingEvent loggingEvent)
{
var properties = loggingEvent.GetProperties();
if (properties == null)
{
yield break;
}
foreach (var key in properties.GetKeys())
{
if (!string.IsNullOrWhiteSpace(key)
&& !key.StartsWith("log4net:", StringComparison.OrdinalIgnoreCase))
{
var value = properties[key];
if (value != null
&& (value is not string stringValue || !string.IsNullOrWhiteSpace(stringValue)))
{
yield return new KeyValuePair<string, object?>(key, value);
}
}
}
var locInfo = loggingEvent.LocationInformation;
if (locInfo != null)
{
if (!string.IsNullOrEmpty(locInfo.ClassName))
{
yield return new KeyValuePair<string, object?>(nameof(locInfo.ClassName), locInfo.ClassName);
}
if (!string.IsNullOrEmpty(locInfo.FileName))
{
yield return new KeyValuePair<string, object?>(nameof(locInfo.FileName), locInfo.FileName);
}
if (int.TryParse(locInfo.LineNumber, out var lineNumber) && lineNumber != 0)
{
yield return new KeyValuePair<string, object?>(nameof(locInfo.LineNumber), lineNumber);
}
if (!string.IsNullOrEmpty(locInfo.MethodName))
{
yield return new KeyValuePair<string, object?>(nameof(locInfo.MethodName), locInfo.MethodName);
}
}
if (!string.IsNullOrEmpty(loggingEvent.ThreadName))
{
yield return new KeyValuePair<string, object?>(nameof(loggingEvent.ThreadName), loggingEvent.ThreadName);
}
if (!string.IsNullOrEmpty(loggingEvent.Domain))
{
yield return new KeyValuePair<string, object?>(nameof(loggingEvent.Domain), loggingEvent.Domain);
}
if (loggingEvent.Level != null)
{
yield return new KeyValuePair<string, object?>("log4net-level", loggingEvent.Level.Name);
}
}
/// <summary>
/// Disposes the SDK if initialized.
/// </summary>
protected override void OnClose()
{
base.OnClose();
_sdkHandle?.Dispose();
}
}