-
Notifications
You must be signed in to change notification settings - Fork 91
/
RaygunClient.cs
798 lines (695 loc) · 27.7 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Mindscape.Raygun4Net.Messages;
using System.Web;
using System.Threading;
using System.Reflection;
using Mindscape.Raygun4Net.Builders;
using Mindscape.Raygun4Net.Breadcrumbs;
using Mindscape.Raygun4Net.Filters;
using Mindscape.Raygun4Net.Logging;
using Mindscape.Raygun4Net.Storage;
namespace Mindscape.Raygun4Net
{
public class RaygunClient : RaygunClientBase
{
internal const string UnhandledExceptionTag = "UnhandledException";
private static readonly RaygunBreadcrumbs _breadcrumbs = new RaygunBreadcrumbs(new DefaultBreadcrumbStorage());
private static object _sendLock = new object();
private readonly string _apiKey;
private readonly RaygunRequestMessageOptions _requestMessageOptions = new RaygunRequestMessageOptions();
private readonly List<Type> _wrapperExceptions = new List<Type>();
private IRaygunOfflineStorage _offlineStorage = new IsolatedRaygunOfflineStorage();
private readonly ThrottledBackgroundMessageProcessor _backgroundMessageProcessor;
private IWebProxy _webProxy;
/// <summary>
/// Gets or sets the username/password credentials which are used to authenticate with the system default Proxy server, if one is set
/// and requires credentials.
/// </summary>
public ICredentials ProxyCredentials { get; set; }
/// <summary>
/// Gets or sets an IWebProxy instance which can be used to override the default system proxy server settings
/// </summary>
public IWebProxy WebProxy
{
get => _webProxy;
set
{
_webProxy = value;
WebClientHelper.WebProxy = _webProxy;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="RaygunClient" /> class.
/// Uses the ApiKey specified in the config file.
/// </summary>
public RaygunClient() : this(RaygunSettings.Settings.ApiKey) { }
/// <summary>
/// Initializes a new instance of the <see cref="RaygunClient" /> class.
/// </summary>
/// <param name="apiKey">The API key.</param>
public RaygunClient(string apiKey)
{
_apiKey = apiKey;
_wrapperExceptions.Add(typeof(TargetInvocationException));
_wrapperExceptions.Add(typeof(HttpUnhandledException));
if (!string.IsNullOrEmpty(RaygunSettings.Settings.IgnoreSensitiveFieldNames))
{
var ignoredNames = RaygunSettings.Settings.IgnoreSensitiveFieldNames.Split(',');
IgnoreSensitiveFieldNames(ignoredNames);
}
if (!string.IsNullOrEmpty(RaygunSettings.Settings.IgnoreQueryParameterNames))
{
var ignoredNames = RaygunSettings.Settings.IgnoreQueryParameterNames.Split(',');
IgnoreQueryParameterNames(ignoredNames);
}
if (!string.IsNullOrEmpty(RaygunSettings.Settings.IgnoreFormFieldNames))
{
var ignoredNames = RaygunSettings.Settings.IgnoreFormFieldNames.Split(',');
IgnoreFormFieldNames(ignoredNames);
}
if (!string.IsNullOrEmpty(RaygunSettings.Settings.IgnoreHeaderNames))
{
var ignoredNames = RaygunSettings.Settings.IgnoreHeaderNames.Split(',');
IgnoreHeaderNames(ignoredNames);
}
if (!string.IsNullOrEmpty(RaygunSettings.Settings.IgnoreCookieNames))
{
var ignoredNames = RaygunSettings.Settings.IgnoreCookieNames.Split(',');
IgnoreCookieNames(ignoredNames);
}
if (!string.IsNullOrEmpty(RaygunSettings.Settings.IgnoreServerVariableNames))
{
var ignoredNames = RaygunSettings.Settings.IgnoreServerVariableNames.Split(',');
IgnoreServerVariableNames(ignoredNames);
}
IsRawDataIgnored = RaygunSettings.Settings.IsRawDataIgnored;
IsRawDataIgnoredWhenFilteringFailed = RaygunSettings.Settings.IsRawDataIgnoredWhenFilteringFailed;
UseXmlRawDataFilter = RaygunSettings.Settings.UseXmlRawDataFilter;
UseKeyValuePairRawDataFilter = RaygunSettings.Settings.UseKeyValuePairRawDataFilter;
_backgroundMessageProcessor = new ThrottledBackgroundMessageProcessor(
RaygunSettings.Settings.BackgroundMessageQueueMax,
RaygunSettings.Settings.BackgroundMessageWorkerCount,
Send);
ThreadPool.QueueUserWorkItem(state => { SendStoredMessages(); });
}
/// <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 HttpUnhandledException,
/// 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 HttpUnhandledException).
/// </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);
}
}
#region Message Scrubbing Properties
/// <summary>
/// Adds a list of keys to remove from the following sections of the <see cref="RaygunRequestMessage" />
/// <see cref="RaygunRequestMessage.Headers" />
/// <see cref="RaygunRequestMessage.QueryString" />
/// <see cref="RaygunRequestMessage.Cookies" />
/// <see cref="RaygunRequestMessage.Data" />
/// <see cref="RaygunRequestMessage.Form" />
/// <see cref="RaygunRequestMessage.RawData" />
/// </summary>
/// <param name="names">Keys to be stripped from the <see cref="RaygunRequestMessage" />.</param>
public void IgnoreSensitiveFieldNames(params string[] names)
{
_requestMessageOptions.AddSensitiveFieldNames(names);
}
/// <summary>
/// Adds a list of keys to remove from the <see cref="RaygunRequestMessage.QueryString" /> property of the <see cref="RaygunRequestMessage" />
/// </summary>
/// <param name="names">Keys to be stripped from the <see cref="RaygunRequestMessage.QueryString" /></param>
public void IgnoreQueryParameterNames(params string[] names)
{
_requestMessageOptions.AddQueryParameterNames(names);
}
/// <summary>
/// Adds a list of keys to ignore when attaching the Form data of an HTTP POST request. This allows
/// you to remove sensitive data from the transmitted copy of the Form on the HttpRequest by specifying the keys you want removed.
/// This method is only effective in a web context.
/// </summary>
/// <param name="names">Keys to be stripped from the copy of the Form NameValueCollection when sending to Raygun.</param>
public void IgnoreFormFieldNames(params string[] names)
{
_requestMessageOptions.AddFormFieldNames(names);
}
/// <summary>
/// Adds a list of keys to ignore when attaching the headers of an HTTP POST request. This allows
/// you to remove sensitive data from the transmitted copy of the Headers on the HttpRequest by specifying the keys you want removed.
/// This method is only effective in a web context.
/// </summary>
/// <param name="names">Keys to be stripped from the copy of the Headers NameValueCollection when sending to Raygun.</param>
public void IgnoreHeaderNames(params string[] names)
{
_requestMessageOptions.AddHeaderNames(names);
}
/// <summary>
/// Adds a list of keys to ignore when attaching the cookies of an HTTP POST request. This allows
/// you to remove sensitive data from the transmitted copy of the Cookies on the HttpRequest by specifying the keys you want removed.
/// This method is only effective in a web context.
/// </summary>
/// <param name="names">Keys to be stripped from the copy of the Cookies NameValueCollection when sending to Raygun.</param>
public void IgnoreCookieNames(params string[] names)
{
_requestMessageOptions.AddCookieNames(names);
}
/// <summary>
/// Adds a list of keys to ignore when attaching the server variables of an HTTP POST request. This allows
/// you to remove sensitive data from the transmitted copy of the ServerVariables on the HttpRequest by specifying the keys you want removed.
/// This method is only effective in a web context.
/// </summary>
/// <param name="names">Keys to be stripped from the copy of the ServerVariables NameValueCollection when sending to Raygun.</param>
public void IgnoreServerVariableNames(params string[] names)
{
_requestMessageOptions.AddServerVariableNames(names);
}
/// <summary>
/// Specifies whether or not RawData from web requests is ignored when sending reports to Raygun.
/// The default is false which means RawData will be sent to Raygun.
/// </summary>
public bool IsRawDataIgnored
{
get { return _requestMessageOptions.IsRawDataIgnored; }
set { _requestMessageOptions.IsRawDataIgnored = value; }
}
/// <summary>
/// Specifies whether or not RawData from web requests is ignored when sensitive values are seen and unable to be removed due to failing to parse the contents.
/// The default is false which means RawData will not be ignored when filtering fails.
/// </summary>
public bool IsRawDataIgnoredWhenFilteringFailed
{
get { return _requestMessageOptions.IsRawDataIgnoredWhenFilteringFailed; }
set { _requestMessageOptions.IsRawDataIgnoredWhenFilteringFailed = value; }
}
/// <summary>
/// Specifies whether or not RawData from web requests is filtered of sensitive values using an XML parser.
/// </summary>
/// <value><c>true</c> if use xml raw data filter; otherwise, <c>false</c>.</value>
public bool UseXmlRawDataFilter
{
get { return _requestMessageOptions.UseXmlRawDataFilter; }
set { _requestMessageOptions.UseXmlRawDataFilter = value; }
}
/// <summary>
/// Specifies whether or not RawData from web requests is filtered of sensitive values using an KeyValuePair parser.
/// </summary>
/// <value><c>true</c> if use key pair raw data filter; otherwise, <c>false</c>.</value>
public bool UseKeyValuePairRawDataFilter
{
get { return _requestMessageOptions.UseKeyValuePairRawDataFilter; }
set { _requestMessageOptions.UseKeyValuePairRawDataFilter = value; }
}
/// <summary>
/// Add an <see cref="IRaygunDataFilter"/> implementation to be used when capturing the raw data
/// of a HTTP request. This filter will be passed the request raw data and is expected to remove
/// or replace values whose keys are found in the list supplied to the Filter method.
/// </summary>
/// <param name="filter">Custom raw data filter implementation.</param>
public void AddRawDataFilter(IRaygunDataFilter filter)
{
_requestMessageOptions.AddRawDataFilter(filter);
}
#endregion // Message Scrubbing Properties
#region Breadcrumbs
public static void RecordBreadcrumb(string message)
{
_breadcrumbs.Record(new RaygunBreadcrumb { Message = message });
}
public static void RecordBreadcrumb(RaygunBreadcrumb crumb)
{
_breadcrumbs.Record(crumb);
}
public static void ClearBreadcrumbs()
{
_breadcrumbs.Clear();
}
#endregion // Breadcrumbs
#region Message Send Methods
/// <summary>
/// Transmits an exception to Raygun 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, null);
}
/// <summary>
/// Transmits an exception to Raygun 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, null);
}
/// <summary>
/// Transmits an exception to Raygun 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(exception, tags, userCustomData, null);
}
/// <summary>
/// Transmits an exception to Raygun 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>
/// <param name="userInfo">Information about the user including the identity string.</param>
public void Send(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo)
{
if (CanSend(exception))
{
StripAndSend(exception, tags, userCustomData, userInfo, null);
FlagAsSent(exception);
}
}
/// <summary>
/// Asynchronously transmits a message to Raygun.
/// </summary>
/// <param name="exception">The exception to deliver.</param>
public void SendInBackground(Exception exception)
{
SendInBackground(exception, null, (IDictionary)null, null);
}
/// <summary>
/// Asynchronously transmits an exception to Raygun.
/// </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, null);
}
/// <summary>
/// Asynchronously transmits an exception to Raygun.
/// </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)
{
SendInBackground(exception, tags, userCustomData, null);
}
/// <summary>
/// Asynchronously transmits an exception to Raygun.
/// </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>
/// <param name="userInfo">Information about the user including the identity string.</param>
public void SendInBackground(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo)
{
try
{
if (CanSend(exception))
{
try
{
var currentTime = DateTime.UtcNow;
StripAndSendInBackground(exception, tags, userCustomData, userInfo, currentTime);
}
catch (Exception)
{
// This will swallow any unhandled exceptions unless we explicitly want to throw on error.
// Otherwise this can bring the whole process down.
if (RaygunSettings.Settings.ThrowOnError)
{
throw;
}
}
FlagAsSent(exception);
}
}
catch (Exception)
{
// This will swallow any unhandled exceptions unless we explicitly want to throw on error.
// Otherwise this can bring the whole process down.
if (RaygunSettings.Settings.ThrowOnError)
{
throw;
}
}
}
/// <summary>
/// Asynchronously transmits a message to Raygun.
/// </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)
{
SendInBackground(() => raygunMessage);
}
public void SendInBackground(Func<RaygunMessage> raygunMessage)
{
if (!_backgroundMessageProcessor.Enqueue(raygunMessage))
{
RaygunLogger.Instance.Debug($"Could not add message to background queue. Dropping message: {raygunMessage}");
}
}
private void StripAndSend(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo, DateTime? currentTime)
{
var contextId = GetContextId();
var requestMessage = BuildRequestMessage();
IList<RaygunBreadcrumb> breadcrumbs = BuildBreadCrumbList();
foreach (var e in StripWrapperExceptions(exception))
{
Send(BuildMessage(e, tags, userCustomData, userInfo, currentTime, x =>
{
x.Details.Request = requestMessage;
x.Details.Breadcrumbs = breadcrumbs;
x.Details.ContextId = contextId;
}));
}
}
private void StripAndSendInBackground(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo, DateTime? currentTime)
{
var contextId = GetContextId();
var requestMessage = BuildRequestMessage();
IList<RaygunBreadcrumb> breadcrumbs = BuildBreadCrumbList();
foreach (var e in StripWrapperExceptions(exception))
{
SendInBackground(() => BuildMessage(e, tags, userCustomData, userInfo, currentTime, x =>
{
x.Details.Request = requestMessage;
x.Details.Breadcrumbs = breadcrumbs;
x.Details.ContextId = contextId;
}));
}
}
private static IList<RaygunBreadcrumb> BuildBreadCrumbList()
{
IList<RaygunBreadcrumb> breadCrumbs = null;
foreach (var breadCrumb in _breadcrumbs)
{
breadCrumbs ??= new List<RaygunBreadcrumb>();
breadCrumbs.Add(breadCrumb);
}
return breadCrumbs ?? Array.Empty<RaygunBreadcrumb>();
}
/// <summary>
/// Posts a RaygunMessage to the Raygun 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 override void Send(RaygunMessage raygunMessage)
{
if (!ValidateApiKey())
{
RaygunLogger.Instance.Warning("Failed to send error report due to invalid API key");
return;
}
bool canSend = OnSendingMessage(raygunMessage);
if (!canSend)
{
return;
}
string message = null;
try
{
message = SimpleJson.SerializeObject(raygunMessage);
}
catch (Exception ex)
{
RaygunLogger.Instance.Error($"Failed to serialize report due to: {ex.Message}");
if (RaygunSettings.Settings.ThrowOnError)
{
throw;
}
}
if (string.IsNullOrEmpty(message))
{
return;
}
bool successfullySentReport = true;
try
{
Send(message);
}
catch (Exception ex)
{
successfullySentReport = false;
RaygunLogger.Instance.Error($"Failed to send report to Raygun due to: {ex.Message}");
SaveMessage(message);
if (RaygunSettings.Settings.ThrowOnError)
{
throw;
}
}
if (successfullySentReport)
{
SendStoredMessages();
}
}
private void Send(string message)
{
RaygunLogger.Instance.Verbose("Sending Payload --------------");
RaygunLogger.Instance.Verbose(message);
RaygunLogger.Instance.Verbose("------------------------------");
if (WebProxy != null)
{
WebClientHelper.WebProxy = WebProxy;
}
WebClientHelper.Send(message, _apiKey, ProxyCredentials);
}
#endregion // Message Send Methods
#region Message Building Methods
private RaygunRequestMessage BuildRequestMessage()
{
RaygunRequestMessage requestMessage = null;
HttpContext context = HttpContext.Current;
if (context != null)
{
HttpRequest request = null;
try
{
request = context.Request;
}
catch (HttpException ex)
{
RaygunLogger.Instance.Error($"Error retrieving HttpRequest {ex.Message}");
}
if (request != null)
{
requestMessage = RaygunRequestMessageBuilder.Build(request, _requestMessageOptions);
}
}
return requestMessage;
}
protected RaygunMessage BuildMessage(Exception exception, IList<string> tags, IDictionary userCustomData = null, RaygunIdentifierMessage userInfoMessage = null, DateTime? currentTime = null, Action<RaygunMessage> customise = null)
{
RaygunMessageBuilder builder = RaygunMessageBuilder.New;
var message = builder
.SetTimeStamp(currentTime)
.SetEnvironmentDetails()
.SetMachineName(Environment.MachineName)
.SetExceptionDetails(exception)
.SetClientDetails()
.SetVersion(ApplicationVersion)
.SetTags(tags)
.SetUserCustomData(userCustomData)
.SetUser(userInfoMessage ?? UserInfo ?? (!string.IsNullOrEmpty(User) ? new RaygunIdentifierMessage(User) : null))
.Customise(customise)
.Build();
var customGroupingKey = OnCustomGroupingKey(exception, message);
if (string.IsNullOrEmpty(customGroupingKey) == false)
{
message.Details.GroupingKey = customGroupingKey;
}
return message;
}
protected IEnumerable<Exception> StripWrapperExceptions(Exception exception)
{
if (exception != null && _wrapperExceptions.Any(wrapperException => exception.GetType() == wrapperException && (exception.InnerException != null || exception is ReflectionTypeLoadException)))
{
AggregateException aggregate = exception as AggregateException;
if (aggregate != null)
{
foreach (Exception e in aggregate.InnerExceptions)
{
foreach (Exception ex in StripWrapperExceptions(e))
{
yield return ex;
}
}
}
else
{
ReflectionTypeLoadException rtle = exception as ReflectionTypeLoadException;
if (rtle != null)
{
int index = 0;
foreach (Exception e in rtle.LoaderExceptions)
{
try
{
e.Data["Type"] = rtle.Types[index];
}
catch
{
}
foreach (Exception ex in StripWrapperExceptions(e))
{
yield return ex;
}
index++;
}
}
else
{
foreach (Exception e in StripWrapperExceptions(exception.InnerException))
{
yield return e;
}
}
}
}
else
{
yield return exception;
}
}
private string GetContextId()
{
return HttpContext.Current?.Session?.SessionID;
}
#endregion // Message Building Methods
#region Message Offline Storage
private void SaveMessage(string message)
{
if (!RaygunSettings.Settings.CrashReportingOfflineStorageEnabled)
{
RaygunLogger.Instance.Warning("Offline storage is disabled, skipping saving report.");
return;
}
if (!ValidateApiKey())
{
RaygunLogger.Instance.Warning("Failed to save report due to invalid API key.");
return;
}
// Avoid writing and reading from disk at the same time with `SendStoredMessages`.
lock (_sendLock)
{
try
{
if (!_offlineStorage.Store(message, _apiKey))
{
RaygunLogger.Instance.Warning("Failed to save report to offline storage");
}
}
catch (Exception ex)
{
RaygunLogger.Instance.Error($"Failed to save report to offline storage due to: {ex.Message}");
}
}
}
private void SendStoredMessages()
{
if (!RaygunSettings.Settings.CrashReportingOfflineStorageEnabled)
{
RaygunLogger.Instance.Warning("Offline storage is disabled, skipping sending stored reports.");
return;
}
if (!ValidateApiKey())
{
RaygunLogger.Instance.Warning("Failed to send offline reports due to invalid API key.");
return;
}
lock (_sendLock)
{
try
{
var files = _offlineStorage.FetchAll(_apiKey);
foreach (var file in files)
{
try
{
// Send the stored report.
Send(file.Contents);
// Remove the stored report from local storage.
if (_offlineStorage.Remove(file.Name, _apiKey))
{
RaygunLogger.Instance.Info("Successfully removed report from offline storage.");
}
else
{
RaygunLogger.Instance.Warning("Failed to remove report from offline storage.");
}
}
catch (Exception ex)
{
RaygunLogger.Instance.Error($"Failed to send stored report to Raygun due to: {ex.Message}");
// If just one message fails to send, then don't delete the message,
// and don't attempt sending anymore until later.
return;
}
}
}
catch (Exception ex)
{
RaygunLogger.Instance.Error($"Failed to send stored report to Raygun due to: {ex.Message}");
}
}
}
#endregion // Message Offline Storage
protected bool ValidateApiKey()
{
return !string.IsNullOrEmpty(_apiKey);
}
protected override bool CanSend(Exception exception)
{
if (RaygunSettings.Settings.ExcludeErrorsFromLocal && HttpContext.Current != null)
{
try
{
if (HttpContext.Current.Request.IsLocal)
{
return false;
}
}
catch
{
if (RaygunSettings.Settings.ThrowOnError)
{
throw;
}
}
}
return base.CanSend(exception);
}
}
}