-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
CocoaScopeObserver.cs
104 lines (92 loc) · 2.52 KB
/
CocoaScopeObserver.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
using Sentry.Cocoa.Extensions;
using Sentry.Extensibility;
using Sentry.Internal.Extensions;
namespace Sentry.Cocoa;
internal sealed class CocoaScopeObserver : IScopeObserver
{
private readonly SentryOptions _options;
private readonly IScopeObserver? _innerObserver;
public CocoaScopeObserver(SentryOptions options)
{
_options = options;
// Chain any previous observer, but guard against circular reference.
var observer = options.ScopeObserver;
_innerObserver = observer is CocoaScopeObserver ? null : observer;
}
public void AddBreadcrumb(Breadcrumb breadcrumb)
{
try
{
var b = breadcrumb.ToCocoaBreadcrumb();
SentryCocoaSdk.ConfigureScope(scope => scope.AddBreadcrumb(b));
}
finally
{
_innerObserver?.AddBreadcrumb(breadcrumb);
}
}
public void SetExtra(string key, object? value)
{
try
{
if (value is null)
{
_options.LogDebug("Extra with key '{0}' was null.", key);
return;
}
if (value is string s)
{
SentryCocoaSdk.ConfigureScope(scope =>
scope.SetExtraValue(NSObject.FromObject(s), key));
return;
}
try
{
var json = value.ToUtf8Json();
SentryCocoaSdk.ConfigureScope(scope => scope.SetExtraValue(NSObject.FromObject(json), key));
}
catch (Exception ex)
{
_options.LogError(ex, "Extra with key '{0}' could not be serialized.", key);
}
}
finally
{
_innerObserver?.SetExtra(key, value);
}
}
public void SetTag(string key, string value)
{
try
{
SentryCocoaSdk.ConfigureScope(scope => scope.SetTagValue(value, key));
}
finally
{
_innerObserver?.SetTag(key, value);
}
}
public void UnsetTag(string key)
{
try
{
SentryCocoaSdk.ConfigureScope(scope => scope.RemoveTagForKey(key));
}
finally
{
_innerObserver?.UnsetTag(key);
}
}
public void SetUser(SentryUser? user)
{
try
{
var u = user?.ToCocoaUser();
SentryCocoaSdk.ConfigureScope(scope => scope.SetUser(u));
}
finally
{
_innerObserver?.SetUser(user);
}
}
}