Repository navigation
Expand file tree
/
Copy pathSentryAppender.Structured.cs
More file actions
59 lines (49 loc) · 2.3 KB
/
Copy pathSentryAppender.Structured.cs
File metadata and controls
59 lines (49 loc) · 2.3 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
namespace Sentry.Log4Net;
public partial class SentryAppender
{
private static void CaptureStructuredLog(IHub hub, SentryOptions options, LoggingEvent loggingEvent, string? environment, bool sendIdentity)
{
if (loggingEvent.ToSentryLogLevel() is not { } level)
{
return;
}
DateTimeOffset timestamp = new(loggingEvent.TimeStampUtc);
const string? template = null; // cannot get format-string from `log4net.Util.SystemStringFormat` via `LoggingEvent.MessageObject`
var parameters = ImmutableArray<KeyValuePair<string, object>>.Empty; // cannot get arguments from `log4net.Util.SystemStringFormat` via `LoggingEvent.MessageObject`
var message = !string.IsNullOrWhiteSpace(loggingEvent.RenderedMessage) ? loggingEvent.RenderedMessage : string.Empty;
var log = SentryLog.Create(hub, timestamp, level, message, template, parameters);
var scope = hub.GetScope();
log.SetDefaultAttributes(options, scope, Sdk);
log.SetOrigin("auto.log.log4net");
// Honor the appender-level settings, overriding the scope/options defaults, to match the SentryEvent path.
if (!string.IsNullOrWhiteSpace(environment))
{
log.SetAttribute("sentry.environment", environment!);
}
if (sendIdentity && !string.IsNullOrEmpty(loggingEvent.Identity))
{
log.SetAttribute("user.id", loggingEvent.Identity);
}
if (loggingEvent.LoggerName is { } loggerName)
{
log.SetAttribute("category.name", loggerName);
}
// GetProperties() is non-null in current log4net, but the sibling GetLoggingEventProperties guards
// against null, so we do too rather than rely on log4net's internals.
if (loggingEvent.GetProperties() is { } properties)
{
foreach (var key in properties.GetKeys())
{
if (string.IsNullOrEmpty(key) || key.StartsWith("log4net:", StringComparison.OrdinalIgnoreCase) || Guid.TryParse(key, out _))
{
continue;
}
if (properties[key] is { } value)
{
log.SetAttribute($"property.{key}", value);
}
}
}
hub.Logger.CaptureLog(log);
}
}