-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathSentrySdk.cs
291 lines (264 loc) · 11.5 KB
/
SentrySdk.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Sentry.Extensibility;
using Sentry.Infrastructure;
using Sentry.Internal;
using Sentry.Protocol;
namespace Sentry
{
/// <summary>
/// Sentry SDK entrypoint
/// </summary>
/// <remarks>
/// This is a facade to the SDK instance.
/// It allows safe static access to a client and scope management.
/// When the SDK is uninitialized, calls to this class result in no-op so no callbacks are invoked.
/// </remarks>
public static class SentrySdk
{
private static IHub _hub = DisabledHub.Instance;
/// <summary>
/// Last event id recorded in the current scope
/// </summary>
public static SentryId LastEventId { [DebuggerStepThrough] get => _hub.LastEventId; }
/// <summary>
/// Initializes the SDK while attempting to locate the DSN
/// </summary>
/// <remarks>
/// If the DSN is not found, the SDK will not change state.
/// </remarks>
public static IDisposable Init() => Init(DsnLocator.FindDsnStringOrDisable());
/// <summary>
/// Initializes the SDK with the specified DSN
/// </summary>
/// <remarks>
/// An empty string is interpreted as a disabled SDK
/// </remarks>
/// <seealso href="https://docs.sentry.io/clientdev/overview/#usage-for-end-users"/>
/// <param name="dsn">The dsn</param>
public static IDisposable Init(string dsn)
=> string.IsNullOrWhiteSpace(dsn)
? DisabledHub.Instance
: Init(c => c.Dsn = new Dsn(dsn));
/// <summary>
/// Initializes the SDK with the specified DSN
/// </summary>
/// <param name="dsn">The dsn</param>
public static IDisposable Init(Dsn dsn) => Init(c => c.Dsn = dsn);
/// <summary>
/// Initializes the SDK with an optional configuration options callback.
/// </summary>
/// <param name="configureOptions">The configure options.</param>
public static IDisposable Init(Action<SentryOptions> configureOptions)
{
var options = new SentryOptions();
configureOptions?.Invoke(options);
return Init(options);
}
/// <summary>
/// Initializes the SDK with the specified options instance
/// </summary>
/// <param name="options">The options instance</param>
/// <remarks>
/// Used by integrations which have their own delegates
/// </remarks>
/// <returns>A disposable to close the SDK.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static IDisposable Init(SentryOptions options)
{
if (options.Dsn == null)
{
if (!Dsn.TryParse(DsnLocator.FindDsnStringOrDisable(), out var dsn))
{
options.DiagnosticLogger?.LogWarning("Init was called but no DSN was provided nor located. Sentry SDK will be disabled.");
return DisabledHub.Instance;
}
options.Dsn = dsn;
}
return UseHub(new Hub(options));
}
internal static IDisposable UseHub(IHub hub)
{
var oldHub = Interlocked.Exchange(ref _hub, hub);
(oldHub as IDisposable)?.Dispose();
return new DisposeHandle(hub);
}
/// <summary>
/// Flushes events queued up.
/// </summary>
[DebuggerStepThrough]
public static Task FlushAsync(TimeSpan timeout) => _hub.FlushAsync(timeout);
/// <summary>
/// Close the SDK
/// </summary>
/// <remarks>
/// Flushes the events and disables the SDK.
/// This method is mostly used for testing the library since
/// Init returns a IDisposable that can be used to shutdown the SDK.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Close()
{
var oldHub = Interlocked.Exchange(ref _hub, DisabledHub.Instance);
(oldHub as IDisposable)?.Dispose();
}
private class DisposeHandle : IDisposable
{
private IHub _localHub;
public DisposeHandle(IHub hub) => _localHub = hub;
public void Dispose()
{
_ = Interlocked.CompareExchange(ref _hub, DisabledHub.Instance, _localHub);
(_localHub as IDisposable)?.Dispose();
_localHub = null;
}
}
/// <summary>
/// Whether the SDK is enabled or not
/// </summary>
public static bool IsEnabled { [DebuggerStepThrough] get => _hub.IsEnabled; }
/// <summary>
/// Creates a new scope that will terminate when disposed
/// </summary>
/// <remarks>
/// Pushes a new scope while inheriting the current scope's data.
/// </remarks>
/// <typeparam name="TState"></typeparam>
/// <param name="state">A state object to be added to the scope</param>
/// <returns>A disposable that when disposed, ends the created scope.</returns>
[DebuggerStepThrough]
public static IDisposable PushScope<TState>(TState state) => _hub.PushScope(state);
/// <summary>
/// Creates a new scope that will terminate when disposed
/// </summary>
/// <returns>A disposable that when disposed, ends the created scope.</returns>
[DebuggerStepThrough]
public static IDisposable PushScope() => _hub.PushScope();
/// <summary>
/// Binds the client to the current scope.
/// </summary>
/// <param name="client">The client.</param>
[DebuggerStepThrough]
public static void BindClient(ISentryClient client) => _hub.BindClient(client);
/// <summary>
/// Adds a breadcrumb to the current Scope
/// </summary>
/// <param name="message">
/// If a message is provided it’s rendered as text and the whitespace is preserved.
/// Very long text might be abbreviated in the UI.</param>
/// <param name="type">
/// The type of breadcrumb.
/// The default type is default which indicates no specific handling.
/// Other types are currently http for HTTP requests and navigation for navigation events.
/// <seealso href="https://docs.sentry.io/clientdev/interfaces/breadcrumbs/#breadcrumb-types"/>
/// </param>
/// <param name="category">
/// Categories are dotted strings that indicate what the crumb is or where it comes from.
/// Typically it’s a module name or a descriptive string.
/// For instance ui.click could be used to indicate that a click happened in the UI or flask could be used to indicate that the event originated in the Flask framework.
/// </param>
/// <param name="data">
/// Data associated with this breadcrumb.
/// Contains a sub-object whose contents depend on the breadcrumb type.
/// Additional parameters that are unsupported by the type are rendered as a key/value table.
/// </param>
/// <param name="level">Breadcrumb level.</param>
/// <seealso href="https://docs.sentry.io/clientdev/interfaces/breadcrumbs/"/>
[DebuggerStepThrough]
public static void AddBreadcrumb(
string message,
string category = null,
string type = null,
IDictionary<string, string> data = null,
BreadcrumbLevel level = default)
=> _hub.AddBreadcrumb(message, category, type, data, level);
/// <summary>
/// Adds a breadcrumb to the current scope
/// </summary>
/// <remarks>
/// This overload is intended to be used by integrations only.
/// The objective is to allow better testability by allowing control of the timestamp set to the breadcrumb.
/// </remarks>
/// <param name="clock">An optional <see cref="ISystemClock"/></param>
/// <param name="message">The message.</param>
/// <param name="type">The type.</param>
/// <param name="category">The category.</param>
/// <param name="data">The data.</param>
/// <param name="level">The level.</param>
[DebuggerStepThrough]
[EditorBrowsable(EditorBrowsableState.Never)]
public static void AddBreadcrumb(
ISystemClock clock,
string message,
string category = null,
string type = null,
IDictionary<string, string> data = null,
BreadcrumbLevel level = default)
=> _hub.AddBreadcrumb(clock, message, category, type, data, level);
/// <summary>
/// Runs the callback with a new scope which gets dropped at the end
/// </summary>
/// <remarks>
/// Pushes a new scope, runs the callback, pops the scope.
/// </remarks>
/// <see href="https://docs.sentry.io/learn/scopes/?platform=csharp#local-scopes"/>
/// <param name="scopeCallback">The callback to run with the one time scope.</param>
[DebuggerStepThrough]
public static void WithScope(Action<Scope> scopeCallback)
=> _hub.WithScope(scopeCallback);
/// <summary>
/// Configures the scope through the callback.
/// </summary>
/// <param name="configureScope">The configure scope callback.</param>
[DebuggerStepThrough]
public static void ConfigureScope(Action<Scope> configureScope)
=> _hub.ConfigureScope(configureScope);
/// <summary>
/// Configures the scope asynchronously
/// </summary>
/// <param name="configureScope">The configure scope callback.</param>
/// <returns>The Id of the event</returns>
[DebuggerStepThrough]
public static Task ConfigureScopeAsync(Func<Scope, Task> configureScope)
=> _hub.ConfigureScopeAsync(configureScope);
/// <summary>
/// Captures the event.
/// </summary>
/// <param name="evt">The event.</param>
/// <returns>The Id of the event</returns>
[DebuggerStepThrough]
public static SentryId CaptureEvent(SentryEvent evt)
=> _hub.CaptureEvent(evt);
/// <summary>
/// Captures the event using the specified scope.
/// </summary>
/// <param name="evt">The event.</param>
/// <param name="scope">The scope.</param>
/// <returns>The Id of the event</returns>
[DebuggerStepThrough]
[EditorBrowsable(EditorBrowsableState.Never)]
public static SentryId CaptureEvent(SentryEvent evt, Scope scope)
=> _hub.CaptureEvent(evt, scope);
/// <summary>
/// Captures the exception.
/// </summary>
/// <param name="exception">The exception.</param>
/// <returns>The Id of the event</returns>
[DebuggerStepThrough]
public static SentryId CaptureException(Exception exception)
=> _hub.CaptureException(exception);
/// <summary>
/// Captures the message.
/// </summary>
/// <param name="message">The message to send.</param>
/// <param name="level">The message level.</param>
/// <returns>The Id of the event</returns>
[DebuggerStepThrough]
public static SentryId CaptureMessage(string message, SentryLevel level = SentryLevel.Info)
=> _hub.CaptureMessage(message, level);
}
}