-
Notifications
You must be signed in to change notification settings - Fork 91
/
RaygunClient.cs
278 lines (247 loc) · 9.85 KB
/
RaygunClient.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
using System;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Mindscape.Raygun4Net.Messages;
using System.Reflection;
using System.IO.IsolatedStorage;
using System.IO;
using System.Text;
using System.Net;
using System.Linq;
namespace Mindscape.Raygun4Net
{
public class RaygunClient : RaygunClientBase
{
private readonly string _apiKey;
private readonly List<Type> _wrapperExceptions = new List<Type>();
public RaygunClient (string apiKey)
{
_apiKey = apiKey;
_wrapperExceptions.Add(typeof(TargetInvocationException));
_wrapperExceptions.Add(typeof(AggregateException));
}
private bool ValidateApiKey()
{
if (string.IsNullOrEmpty(_apiKey))
{
System.Diagnostics.Debug.WriteLine("ApiKey has not been provided, exception will not be logged");
return false;
}
return true;
}
/// <summary>
/// Gets or sets the user identity string.
/// </summary>
public string User { get; set; }
/// <summary>
/// Gets or sets information about the user including the identity string.
/// </summary>
public RaygunIdentifierMessage UserInfo { get; set; }
/// <summary>
/// Gets or sets a custom application version identifier for all error messages sent to the Raygun.io endpoint.
/// </summary>
public string ApplicationVersion { get; set; }
/// <summary>
/// Adds a list of outer exceptions that will be stripped, leaving only the valuable inner exception.
/// This can be used when a wrapper exception, e.g. TargetInvocationException or AggregateException,
/// contains the actual exception as the InnerException. The message and stack trace of the inner exception will then
/// be used by Raygun for grouping and display. The above two do not need to be added manually,
/// but if you have other wrapper exceptions that you want stripped you can pass them in here.
/// </summary>
/// <param name="wrapperExceptions">Exception types that you want removed and replaced with their inner exception.</param>
public void AddWrapperExceptions(params Type[] wrapperExceptions)
{
foreach (Type wrapper in wrapperExceptions)
{
if (!_wrapperExceptions.Contains(wrapper))
{
_wrapperExceptions.Add(wrapper);
}
}
}
/// <summary>
/// Specifies types of wrapper exceptions that Raygun should send rather than stripping out and sending the inner exception.
/// This can be used to remove the default wrapper exceptions (TargetInvocationException and AggregateException).
/// </summary>
/// <param name="wrapperExceptions">Exception types that should no longer be stripped away.</param>
public void RemoveWrapperExceptions(params Type[] wrapperExceptions)
{
foreach (Type wrapper in wrapperExceptions)
{
_wrapperExceptions.Remove(wrapper);
}
}
/// <summary>
/// Transmits an exception to Raygun.io synchronously, using the version number of the originating assembly.
/// </summary>
/// <param name="exception">The exception to deliver.</param>
public override void Send(Exception exception)
{
Send(exception, null, (IDictionary)null);
}
/// <summary>
/// Transmits an exception to Raygun.io synchronously specifying a list of string tags associated
/// with the message for identification. This uses the version number of the originating assembly.
/// </summary>
/// <param name="exception">The exception to deliver.</param>
/// <param name="tags">A list of strings associated with the message.</param>
public void Send(Exception exception, IList<string> tags)
{
Send(exception, tags, (IDictionary)null);
}
/// <summary>
/// Transmits an exception to Raygun.io synchronously specifying a list of string tags associated
/// with the message for identification, as well as sending a key-value collection of custom data.
/// This uses the version number of the originating assembly.
/// </summary>
/// <param name="exception">The exception to deliver.</param>
/// <param name="tags">A list of strings associated with the message.</param>
/// <param name="userCustomData">A key-value collection of custom data that will be added to the payload.</param>
public void Send(Exception exception, IList<string> tags, IDictionary userCustomData)
{
Send(BuildMessage(exception, tags, userCustomData));
}
/// <summary>
/// Asynchronously transmits a message to Raygun.io.
/// </summary>
/// <param name="exception">The exception to deliver.</param>
public void SendInBackground(Exception exception)
{
SendInBackground(exception, null, (IDictionary)null);
}
/// <summary>
/// Asynchronously transmits a message to Raygun.io.
/// </summary>
/// <param name="exception">The exception to deliver.</param>
/// <param name="tags">A list of strings associated with the message.</param>
public void SendInBackground(Exception exception, IList<string> tags)
{
SendInBackground(exception, tags, (IDictionary)null);
}
/// <summary>
/// Asynchronously transmits a message to Raygun.io.
/// </summary>
/// <param name="exception">The exception to deliver.</param>
/// <param name="tags">A list of strings associated with the message.</param>
/// <param name="userCustomData">A key-value collection of custom data that will be added to the payload.</param>
public void SendInBackground(Exception exception, IList<string> tags, IDictionary userCustomData)
{
ThreadPool.QueueUserWorkItem(c => Send(BuildMessage(exception, tags, userCustomData)));
}
/// <summary>
/// Asynchronously transmits a message to Raygun.io.
/// </summary>
/// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
/// set to a valid DateTime and as much of the Details property as is available.</param>
public void SendInBackground(RaygunMessage raygunMessage)
{
ThreadPool.QueueUserWorkItem(c => Send(raygunMessage));
}
private static RaygunClient _client;
/// <summary>
/// Gets the <see cref="RaygunClient"/> created by the Attach method.
/// </summary>
public static RaygunClient Current
{
get { return _client; }
}
/// <summary>
/// Causes Raygun to listen to and send all unhandled exceptions and unobserved task exceptions.
/// </summary>
/// <param name="apiKey">Your app api key.</param>
public static void Attach(string apiKey)
{
Detach();
_client = new RaygunClient(apiKey);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
/// <summary>
/// Detaches Raygun from listening to unhandled exceptions and unobserved task exceptions.
/// </summary>
public static void Detach()
{
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException -= TaskScheduler_UnobservedTaskException;
}
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
if (e.Exception != null)
{
_client.Send(e.Exception);
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception)
{
_client.Send(e.ExceptionObject as Exception);
}
}
protected RaygunMessage BuildMessage(Exception exception, IList<string> tags, IDictionary userCustomData)
{
exception = StripWrapperExceptions(exception);
var message = RaygunMessageBuilder.New
.SetEnvironmentDetails()
.SetMachineName(Environment.MachineName)
.SetExceptionDetails(exception)
.SetClientDetails()
.SetVersion(ApplicationVersion)
.SetTags(tags)
.SetUserCustomData(userCustomData)
.SetUser(UserInfo ?? (!String.IsNullOrEmpty(User) ? new RaygunIdentifierMessage(User) : null))
.Build();
return message;
}
private Exception StripWrapperExceptions(Exception exception)
{
if (exception != null && _wrapperExceptions.Any(wrapperException => exception.GetType() == wrapperException && exception.InnerException != null))
{
return StripWrapperExceptions(exception.InnerException);
}
return exception;
}
/// <summary>
/// Posts a RaygunMessage to the Raygun.io api endpoint.
/// </summary>
/// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
/// set to a valid DateTime and as much of the Details property as is available.</param>
public void Send(RaygunMessage raygunMessage)
{
if (ValidateApiKey())
{
bool canSend = OnSendingMessage(raygunMessage);
if (canSend)
{
string message = null;
try
{
message = SimpleJson.SerializeObject(raygunMessage);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(string.Format("Error Serializing Exception {0}", ex.Message));
}
if (!String.IsNullOrWhiteSpace(message))
{
using (var client = new WebClient())
{
client.Headers.Add("X-ApiKey", _apiKey);
client.Encoding = System.Text.Encoding.UTF8;
try
{
client.UploadString(RaygunSettings.Settings.ApiEndpoint, message);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));
}
}
}
}
}
}
}
}