This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
HockeyClient.cs
1220 lines (1084 loc) · 46.5 KB
/
HockeyClient.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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace Microsoft.HockeyApp
{
using Channel;
using DataContracts;
using Exceptions;
using Extensibility;
using Extensibility.Implementation;
using Extensibility.Implementation.Tracing;
using Extensions;
using Internal;
using Model;
using Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Implements the HockeyClient singleton
/// </summary>
public class HockeyClient : HockeyApp.IHockeyClient, IHockeyClientInternal, IHockeyClientConfigurable
{
private const int MaxQueueSize = 4096;
private static HockeyClient _instance = null;
/// <summary>
/// Telemetry buffer of items that were tracked by the user before configuration has been initialized.
/// </summary>
private readonly Queue<ITelemetry> queue = new Queue<ITelemetry>();
private ILog logger = HockeyLogManager.GetLog(typeof(HockeyClient));
private TelemetryContext context;
private ITelemetryChannel channel;
private HockeyClient()
{
}
#region Properties
/// <summary>
/// The current singleton instance of HockeyClient. Use the extension methods in the HockeyApp namespace
/// to work with the instance:
/// HockeyClient.Current.Configure(..) must be called first to initialize the client!
/// </summary>
public static IHockeyClient Current
{
get
{
if (_instance == null)
{
_instance = new HockeyClient();
}
return _instance;
}
}
/// <summary>
/// ApiBase of HockeyApp server
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1307:SpecifyStringComparison", MessageId = "System.String.EndsWith(System.String)")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1307:SpecifyStringComparison", MessageId = "System.String.IndexOf(System.String)", Justification = "ToDo: Perform refactoring after HA/AI SDK integration.")]
[Obsolete("Use Version-specific ApiBase!")]
public string ApiBase
{
get { return ApiBaseVersion2; }
private set
{
if (!String.IsNullOrEmpty(value))
{
string domain = value;
//needed for backwards compatibility
if (value.Contains("/api/"))
{
domain = value.Substring(0, value.IndexOf("/api/", StringComparison.OrdinalIgnoreCase) + 1);
}
ApiDomain = domain.EndsWith("/", StringComparison.OrdinalIgnoreCase) ? domain : domain + "/";
}
else
{
ApiDomain = value;
}
}
}
private string _apiDomain = SDKConstants.PublicApiDomain + "/";
/// <summary>
/// Base URL (prototcol+domainname) of HockeyApp server
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1307:SpecifyStringComparison", MessageId = "System.String.EndsWith(System.String)")]
public string ApiDomain
{
get { return _apiDomain; }
set
{
if (value != null)
{
_apiDomain = value.EndsWith("/") ? value : value + "/";
}
}
}
/// <summary>
/// API endpoint for API v2
/// </summary>
public string ApiBaseVersion2
{
get { return ApiDomain + "api/2/"; }
}
/// <summary>
/// API endpoint for API v3
/// </summary>
public string ApiBaseVersion3
{
get { return ApiDomain + "api/3/"; }
}
private string _userAgentString;
/// <summary>
/// User agent string
/// </summary>
public string UserAgentString
{
get
{
if (_userAgentString == null)
{
this._userAgentString = SDKConstants.UserAgentString;
if (this.PlatformHelper != null)
{
this._userAgentString = this.PlatformHelper.UserAgentString;
}
}
return _userAgentString;
}
set { _userAgentString = value; }
}
private string _sdkName;
/// <summary>
/// SDK info
/// </summary>
public string SdkName
{
get
{
if (_sdkName == null)
{
this._sdkName = SDKConstants.SdkName;
if (this.PlatformHelper != null)
{
this._sdkName = this.PlatformHelper.SDKName;
}
}
return _sdkName;
}
set { _sdkName = value; }
}
private string _sdkVersion;
/// <summary>
/// SDK Version
/// </summary>
public string SdkVersion
{
get
{
if (_sdkVersion == null)
{
this._sdkVersion = SDKConstants.SdkVersion;
if (this.PlatformHelper != null)
{
this._sdkVersion = this.PlatformHelper.SDKVersion;
}
}
return _sdkVersion;
}
set { _sdkVersion = value; }
}
private string _appIdentifier;
/// <summary>
/// Public identifier of your app
/// </summary>
public string AppIdentifier
{
get { return _appIdentifier; }
set
{
if (!String.IsNullOrEmpty(_appIdentifier))
{
throw new Exception("Repeated initialization of HockeyClient! Please make sure to call the Configure(..) method only once!");
}
_appIdentifier = value;
}
}
private string _versionInfo;
/// <summary>
/// Version of the app as string. Normally set automatically by platform-specific SDK
/// </summary>
public string VersionInfo
{
get
{
if (_versionInfo == null && this.PlatformHelper != null)
{
this._versionInfo = this.PlatformHelper.AppVersion;
}
return _versionInfo;
}
set { _versionInfo = value; }
}
/// <summary>
/// UserID of current app user (if provided)
/// </summary>
public string UserID { get; set; }
/// <summary>
/// Contact information for current user
/// </summary>
public string ContactInformation { get; set; }
//Operating system (set by platform-specific SDK if used)
private string _os;
/// <summary>
/// Name of platform OS
/// </summary>
public string Os
{
get
{
if (_os == null && this.PlatformHelper != null)
{
this._os = this.PlatformHelper.OSPlatform;
}
return _os;
}
set { _os = value; }
}
private string _osVersion;
/// <summary>
/// Operating system version (set by platform-specific SDK if used)
/// </summary>
public string OsVersion
{
get
{
// in TelemetryClient the OS version is correctly identified, use it if TelemetryClient initialized.
// otherwise go to PlatformHelper implementation.
if (IsTelemetryInitialized && Context != null && Context.Device != null && Context.Device != null)
{
return this.Context.Device.DeviceOSVersion;
}
if (_osVersion == null && this.PlatformHelper != null)
{
this._osVersion = this.PlatformHelper.OSVersion;
}
return _osVersion;
}
set
{
_osVersion = value;
}
}
private string _device;
/// <summary>
/// Device (set by platform-specific SDK if used)
/// </summary>
public string Device
{
get
{
if (_device == null && this.PlatformHelper != null)
{
this._device = this.PlatformHelper.Model;
}
return _device;
}
set { _device = value; }
}
private string _oem;
/// <summary>
/// Oem of Device (set by platform-specific SDK if used)
/// </summary>
public string Oem
{
get
{
if (_oem == null && this.PlatformHelper != null)
{
this._oem = this.PlatformHelper.Manufacturer;
}
return _oem;
}
set { _oem = value; }
}
/// <summary>
/// unique user id provided by platform (set by platform-specific SDK if used)
/// </summary>
public string Uuid { get; set; }
/// <summary>
/// Authorized user id (set during login process)
/// </summary>
public string Auid { get; internal set; }
/// <summary>
/// Identified user id (set during login process)
/// </summary>
public string Iuid { get; internal set; }
/// <summary>
/// Delegate which can be set to add a description to a stacktrace when app crashes
/// </summary>
public Func<Exception, string> DescriptionLoader { get; set; }
/// <summary>
/// Gets a value indicating whether telemetry client has been initialized.
/// </summary>
public bool IsTelemetryInitialized
{
get; set;
}
/// <summary>
/// Gets or sets the current context that will be used to augment telemetry you send.
/// </summary>
internal TelemetryContext Context
{
get
{
// In order to prevent a deadlock, we are calling async method from sync using Task.Run to offload a work to a ThreadPool
// thread which does not have a SynchronizationContext and there is no real risk for a deadlock.
// http://stackoverflow.com/questions/28305968/use-task-run-in-synchronous-method-to-avoid-deadlock-waiting-on-async-method
LazyInitializer.EnsureInitialized(ref this.context, () => { return Task.Run(async () => { return await this.CreateInitializedContextAsync(); }).Result; });
return this.context;
}
set
{
this.context = value;
}
}
/// <summary>
/// Gets or sets the default instrumentation key for all <see cref="ITelemetry"/> objects logged.
/// </summary>
internal string InstrumentationKey
{
get { return this.Context.InstrumentationKey; }
set { this.Context.InstrumentationKey = value; }
}
/// <summary>
/// Gets or sets the channel used by the client helper. Note that this doesn't need to be public as a customer can create a new client
/// with a new channel via telemetry configuration.
/// </summary>
internal ITelemetryChannel Channel
{
get
{
ITelemetryChannel output = this.channel;
if (output == null)
{
output = TelemetryConfiguration.Active.TelemetryChannel;
this.channel = output;
}
return output;
}
set
{
this.channel = value;
}
}
#endregion
/// <summary>
/// Check if this HockeyClient has already been initialized (used internally by platform SDKs)
/// </summary>
public void CheckForInitialization()
{
if (String.IsNullOrEmpty(_appIdentifier))
{
throw new Exception("HockeyClient not initialized! Please make sure to call the Configure(..) method first!");
}
}
#region Events
/// <summary>
/// Subscribe to this event to get all exceptions that are swallowed by HockeySDK.
/// Useful for debugging. Be sure to know what to do if you use this in production code.
/// </summary>
public event EventHandler<InternalUnhandledExceptionEventArgs> OnHockeySDKInternalException;
#endregion
#region Crashes
/// <summary>
/// Create a CrashData object from an Exception with the default CrashLogInformation
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
public ICrashData CreateCrashData(Exception ex)
{
return new CrashData(this, ex, this.PrefilledCrashLogInfo);
}
/// <summary>
/// Create a CrashData object from an Exception and a given CrashLogInformation
/// </summary>
/// <param name="ex"></param>
/// <param name="crashLogInfo"></param>
/// <returns></returns>
public ICrashData CreateCrashData(Exception ex, CrashLogInformation crashLogInfo)
{
return new CrashData(this, ex, crashLogInfo);
}
/// <summary>
/// create a CrashData object from a logString and stacktrace (used for Unity crashes)
/// </summary>
/// <param name="logString"></param>
/// <param name="stackTrace"></param>
/// <returns></returns>
public ICrashData CreateCrashData(string logString, string stackTrace)
{
return new CrashData(this, logString, stackTrace, this.PrefilledCrashLogInfo);
}
/// <summary>
/// Get an ICrashData object from crashlog-stream
/// </summary>
/// <param name="inputStream">stream from crashlog</param>
/// <returns>deserialized CrashData object</returns>
public ICrashData Deserialize(Stream inputStream)
{
return CrashData.Deserialize(inputStream);
}
/// <summary>
/// Retrieve filenames of crashlog files from storage
/// </summary>
/// <returns>crashlog-filenames (only name without folder)</returns>
public async Task<IEnumerable<string>> GetCrashFileNamesAsync()
{
return await this.PlatformHelper.GetFileNamesAsync(SDKConstants.CrashDirectoryName, SDKConstants.CrashFilePrefix + "*.log");
}
/// <summary>
/// Delete all crash-logs from storage
/// </summary>
/// <returns></returns>
public async Task DeleteAllCrashesAsync()
{
foreach (string filename in await this.GetCrashFileNamesAsync())
{
try
{
await this.PlatformHelper.DeleteFileAsync(filename, SDKConstants.CrashDirectoryName);
}
catch (Exception ex)
{
HandleInternalUnhandledException(ex);
}
}
}
/// <summary>
/// Check for available crash-logs in storage
/// </summary>
/// <returns>true if saved crashlogs are available</returns>
public async Task<bool> AnyCrashesAvailableAsync() { return (await GetCrashFileNamesAsync()).Any(); }
/// <summary>
/// Handle exception asyncronously
/// </summary>
/// <param name="ex">the exception that should be saved to a crashlog</param>
public async Task HandleExceptionAsync(Exception ex)
{
ICrashData cd = this.CreateCrashData(ex);
var crashId = Guid.NewGuid();
try
{
using (MemoryStream stream = new MemoryStream())
{
cd.Serialize(stream);
stream.Seek(0, SeekOrigin.Begin);
await this.PlatformHelper.WriteStreamToFileAsync(stream, string.Format("{0}{1}.log", SDKConstants.CrashFilePrefix, crashId), SDKConstants.CrashDirectoryName);
}
}
catch (Exception e)
{
HandleInternalUnhandledException(e);
}
}
/// <summary>
/// Handle exception syncronously (only for platforms that support sync write to storage
/// </summary>
/// <param name="ex">the exception that should be saved to a crashlog</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object[])")]
public void HandleException(Exception ex)
{
if (!this.PlatformHelper.PlatformSupportsSyncWrite)
{
throw new Exception("PlatformHelper implementation error.");
}
ICrashData cd = this.CreateCrashData(ex);
var crashId = Guid.NewGuid();
try
{
using (MemoryStream stream = new MemoryStream())
{
cd.Serialize(stream);
stream.Seek(0, SeekOrigin.Begin);
this.PlatformHelper.WriteStreamToFileSync(stream, string.Format("{0}{1}.log", SDKConstants.CrashFilePrefix, crashId), SDKConstants.CrashDirectoryName);
}
}
catch (Exception e)
{
HandleInternalUnhandledException(e);
}
}
#if NET_4_5
private readonly AsyncLock lck = new AsyncLock();
#endif
/// <summary>
/// Send crash-logs from storage and deletes the if they could be sent
/// </summary>
/// <returns>true if at least one Crashlog was transmitted to the server</returns>
public async Task<bool> SendCrashesAndDeleteAfterwardsAsync()
{
bool atLeatOneCrashSent = false;
#if NET_4_5
using(var releaser = await lck.LockAsync()) {
#else
if (Monitor.TryEnter(this))
{
try
{
#endif
logger.Info("Start send crashes to platform.");
if (NetworkInterface.GetIsNetworkAvailable())
{
foreach (string filename in await this.GetCrashFileNamesAsync())
{
logger.Info("Crashfile found: {0}", filename);
Exception error = null;
try //don't stop if one file fails
{
using (var stream = await this.PlatformHelper.GetStreamAsync(filename, SDKConstants.CrashDirectoryName))
{
ICrashData cd = this.Deserialize(stream);
await cd.SendDataAsync();
}
atLeatOneCrashSent = true;
}
catch (Exception ex)
{
HandleInternalUnhandledException(ex);
error = ex;
}
if (error != null && error is WebTransferException)
{
//will retry on next start
}
else
{
//either no error or the file seems corrupt => try to delete it
try
{
await this.PlatformHelper.DeleteFileAsync(filename, SDKConstants.CrashDirectoryName);
}
catch (Exception ex) {
HandleInternalUnhandledException(ex);
}
}
}
}
}
#if !NET_4_5
finally
{
try
{
Monitor.Exit(this);
}
catch (Exception ex)
{ //ignore. on next start it will try again.
HandleInternalUnhandledException(ex);
}
}
}
#endif
return atLeatOneCrashSent;
}
#endregion
#region Update
/// <summary>
/// Get available app versions from the server
/// </summary>
/// <returns></returns>
public async Task<IEnumerable<IAppVersion>> GetAppVersionsAsync()
{
StringBuilder url = new StringBuilder(this.ApiBaseVersion2 + "apps/" + this.AppIdentifier + ".json");
url.Append("?app_version=" + Uri.EscapeDataString(this.VersionInfo));
if (!String.IsNullOrEmpty(this.Os)) { url.Append("&os=" + Uri.EscapeDataString(this.Os)); }
if (!String.IsNullOrEmpty(this.OsVersion)) { url.Append("&os_version=" + Uri.EscapeDataString(this.OsVersion)); }
if (!String.IsNullOrEmpty(this.Device)) { url.Append("&device=" + Uri.EscapeDataString(this.Device)); }
if (!String.IsNullOrEmpty(this.Oem)) { url.Append("&oem=" + Uri.EscapeDataString(this.Oem)); }
if (!String.IsNullOrEmpty(this.SdkName)) { url.Append("&sdk=" + Uri.EscapeDataString(this.SdkName)); }
if (!String.IsNullOrEmpty(this.SdkVersion)) { url.Append("&sdk_version=" + Uri.EscapeDataString(this.SdkVersion)); }
url.Append("&lang=" + Uri.EscapeDataString(System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName));
if (!String.IsNullOrEmpty(this.Auid)) { url.Append("&=auid" + Uri.EscapeDataString(this.Auid)); }
else if (!String.IsNullOrEmpty(this.Iuid)) { url.Append("&=iuid" + Uri.EscapeDataString(this.Iuid)); }
else if (!String.IsNullOrEmpty(this.Uuid)) { url.Append("&=duid" + Uri.EscapeDataString(this.Uuid)); }
var request = WebRequest.CreateHttp(new Uri(this.ApiBaseVersion2 + "apps/" + this.AppIdentifier + ".json", UriKind.Absolute));
request.Method = "Get";
request.SetHeader(HttpRequestHeader.UserAgent.ToString(), this.UserAgentString);
var response = await request.GetResponseAsync();
IEnumerable<AppVersion> appVersions = await TaskEx.Run(() => AppVersion.FromJson(response.GetResponseStream()));
foreach (var ver in appVersions)
{
ver.PublicIdentifier = this.AppIdentifier; //the json response does not include the public app identifier
}
return appVersions;
}
#endregion
#region Feedback
/// <summary>
/// Create a feedback thread to post messages on
/// </summary>
/// <returns>an empty IFeedbackThread</returns>
public IFeedbackThread CreateNewFeedbackThread()
{
return FeedbackThread.CreateInstance();
}
/// <summary>
/// Try to open an existng Feedbackthread
/// </summary>
/// <param name="threadToken">thread token for this thread</param>
/// <returns>a populated feedback thread, null if the token is invalid or the thread closed.</returns>
public async Task<IFeedbackThread> OpenFeedbackThreadAsync(string threadToken)
{
if (String.IsNullOrWhiteSpace(threadToken))
{
throw new ArgumentException("Token must not be empty!");
}
FeedbackThread fbThread = null;
try
{
fbThread = await FeedbackThread.OpenFeedbackThreadAsync(this, threadToken);
}
catch (Exception e)
{
HandleInternalUnhandledException(e);
}
return fbThread;
}
#endregion
#region Authentication
private void FillEmptyUserAndContactInfo(string email)
{
if (String.IsNullOrEmpty(this.UserID))
{
this.UserID = email;
}
if (String.IsNullOrEmpty(this.ContactInformation))
{
this.ContactInformation = email;
}
}
/// <summary>
/// try to authorize a (hockeayapp) user by email and password
/// </summary>
/// <param name="email">email (hockeyapp user id)</param>
/// <param name="password">password of the user</param>
/// <returns>IAuthStatus. If successfull will contain authid and IsAuthorized will be true</returns>
public async Task<IAuthStatus> AuthorizeUserAsync(string email, string password)
{
var request = WebRequest.CreateHttp(new Uri(this.ApiBaseVersion3 + "apps/" +
this.AppIdentifier + "/identity/authorize", UriKind.Absolute));
byte[] credentialBuffer = new UTF8Encoding().GetBytes(email + ":" + password);
request.SetHeader(HttpRequestHeader.Authorization.ToString(), "Basic " + Convert.ToBase64String(credentialBuffer));
request.SetHeader(HttpRequestHeader.UserAgent.ToString(), this.UserAgentString);
request.Method = "POST";
var status = await AuthStatus.DoAuthRequestHandleResponseAsync(request);
if (status.IsAuthorized)
{
this.Auid = (status as AuthStatus).Auid;
this.FillEmptyUserAndContactInfo(email);
}
return status;
}
/// <summary>
/// Identify a user by his email-adress (hockeyapp id)
/// </summary>
/// <param name="email">email (hockeyapp user id)</param>
/// <param name="appSecret">app secret of the app</param>
/// <returns>IAuthStatus. If sucessful (hockeyapp user exists) IsIdentified is true.</returns>
public async Task<IAuthStatus> IdentifyUserAsync(string email, string appSecret)
{
var request = WebRequest.CreateHttp(new Uri(this.ApiBaseVersion3 + "apps/" +
this.AppIdentifier + "/identity/check", UriKind.Absolute));
request.SetHeader(HttpRequestHeader.UserAgent.ToString(), this.UserAgentString);
request.Method = "POST";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
var fields = new Dictionary<string, byte[]>();
fields.Add("authcode", Encoding.UTF8.GetBytes((appSecret + email).GetMD5HexDigest()));
fields.Add("email", Encoding.UTF8.GetBytes(email));
request.ContentType = "multipart/form-data; boundary=" + boundary;
IAuthStatus status;
using (Stream stream = await request.GetRequestStreamAsync())
{
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n";
//write form fields
foreach (var keyValue in fields)
{
stream.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, keyValue.Key);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
stream.Write(formitembytes, 0, formitembytes.Length);
stream.Write(keyValue.Value, 0, keyValue.Value.Length);
}
byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
stream.Write(trailer, 0, trailer.Length);
stream.Flush();
}
status = await AuthStatus.DoAuthRequestHandleResponseAsync(request);
if (status.IsIdentified)
{
this.Iuid = (status as AuthStatus).Iuid;
this.FillEmptyUserAndContactInfo(email);
}
return status;
}
#endregion
#region PlatformHelper
/// <summary>
/// Platform helper for internal use
/// </summary>
public IHockeyPlatformHelper PlatformHelper { get; set; }
CrashLogInformation? _crashLogInfo = null;
/// <summary>
/// A filled CrashLogInformation object
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
public CrashLogInformation PrefilledCrashLogInfo
{
get
{
if (!_crashLogInfo.HasValue)
{
this.CheckForInitialization();
if (PlatformHelper == null) { throw new Exception("HockeyClient PlatformHelper is null!"); }
_crashLogInfo = new CrashLogInformation()
{
PackageName = this.PlatformHelper.AppPackageName,
OperatingSystem = this.PlatformHelper.OSPlatform,
Windows = OsVersion,
Manufacturer = this.PlatformHelper.Manufacturer,
Model = this.PlatformHelper.Model,
ProductID = this.PlatformHelper.ProductID,
Version = this.PlatformHelper.AppVersion
};
}
return _crashLogInfo.Value;
}
}
#endregion
/// <summary>
/// Handle Exceptions that are swallowed because we don't want our SDK crash other apps
/// For internal use by platform SDKs
/// </summary>
/// <param name="unhandledException">the exception to propagate</param>
public void HandleInternalUnhandledException(Exception unhandledException)
{
logger.Error(unhandledException);
var args = new InternalUnhandledExceptionEventArgs() { Exception = unhandledException };
OnHockeySDKInternalException?.Invoke(this, args);
}
/// <summary>
/// Send a custom event for display in Events tab.
/// </summary>
/// <param name="eventName">Event name</param>
public void TrackEvent(string eventName)
{
this.TrackEvent(new EventTelemetry(eventName));
}
/// <summary>
/// Send an event telemetry for display in Diagnostic Search and aggregation in Metrics Explorer.
/// </summary>
/// <param name="eventName">A name for the event.</param>
/// <param name="properties">Named string values you can use to search and classify events.</param>
/// <param name="metrics">Measurements associated with this event.</param>
public void TrackEvent(string eventName, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null)
{
var telemetry = new EventTelemetry(eventName);
if (properties != null && properties.Count > 0)
{
Utils.CopyDictionary(properties, telemetry.Context.Properties);
}
if (metrics != null && metrics.Count > 0)
{
Utils.CopyDictionary(metrics, telemetry.Metrics);
}
this.TrackEvent(telemetry);
}
/// <summary>
/// Send a trace message for display in Diagnostic Search.
/// </summary>
/// <param name="message">Message to display.</param>
public void TrackTrace(string message)
{
this.TrackTrace(new TraceTelemetry(message));
}
/// <summary>
/// Send a trace message for display in Diagnostic Search.
/// </summary>
/// <param name="message">Message to display.</param>
/// <param name="severityLevel">Trace severity level.</param>
public void TrackTrace(string message, SeverityLevel severityLevel)
{
this.TrackTrace(new TraceTelemetry(message, severityLevel));
}
/// <summary>
/// Send a trace message for display in Diagnostic Search.
/// </summary>
/// <param name="message">Message to display.</param>
/// <param name="properties">Named string values you can use to search and classify events.</param>
public void TrackTrace(string message, IDictionary<string, string> properties)
{
TraceTelemetry telemetry = new TraceTelemetry(message);
if (properties != null && properties.Count > 0)
{
Utils.CopyDictionary(properties, telemetry.Context.Properties);
}
this.TrackTrace(telemetry);
}
/// <summary>
/// Send a trace message for display in Diagnostic Search.
/// </summary>
/// <param name="message">Message to display.</param>
/// <param name="severityLevel">Trace severity level.</param>
/// <param name="properties">Named string values you can use to search and classify events.</param>
public void TrackTrace(string message, SeverityLevel severityLevel, IDictionary<string, string> properties)
{
TraceTelemetry telemetry = new TraceTelemetry(message, severityLevel);
if (properties != null && properties.Count > 0)
{
Utils.CopyDictionary(properties, telemetry.Context.Properties);
}
this.TrackTrace(telemetry);
}
/// <summary>
/// Send a trace message for display in Diagnostic Search.
/// </summary>
/// <param name="telemetry">Message with optional properties.</param>
public void TrackTrace(TraceTelemetry telemetry)
{
telemetry = telemetry ?? new TraceTelemetry();
this.Track(telemetry);
}
/// <summary>
/// Send a <see cref="MetricTelemetry"/> for aggregation in Metric Explorer.
/// </summary>
/// <param name="name">Metric name.</param>
/// <param name="value">Metric value.</param>
/// <param name="properties">Named string values you can use to classify and filter metrics.</param>
public void TrackMetric(string name, double value, IDictionary<string, string> properties = null)
{
var telemetry = new MetricTelemetry(name, value);
if (properties != null && properties.Count > 0)
{
Utils.CopyDictionary(properties, telemetry.Properties);
}
this.TrackMetric(telemetry);
}
/// <summary>
/// Send information about the page viewed in the application.
/// </summary>
/// <param name="name">Name of the page.</param>
public void TrackPageView(string name)
{
this.Track(new PageViewTelemetry(name));
}
/// <summary>
/// Send a <see cref="MetricTelemetry"/> for aggregation in Metric Explorer.
/// </summary>
public void TrackMetric(MetricTelemetry telemetry)
{
if (telemetry == null)
{
telemetry = new MetricTelemetry();
}
this.Track(telemetry);
}
/// <summary>
/// Send an <see cref="ExceptionTelemetry"/> for display in Diagnostic Search.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="properties">Named string values you can use to classify and search for this exception.</param>
public void TrackException(Exception exception, IDictionary<string, string> properties = null)
{
if (exception == null)
{
exception = new Exception(Utils.PopulateRequiredStringValue(null, "message", typeof(ExceptionTelemetry).FullName));
}
var service = ServiceLocator.GetService<IUnhandledExceptionTelemetryModule>();
// if service is not implemented, use default exception telemetry
ITelemetry telemetry = service == null ? telemetry = new ExceptionTelemetry(exception) { HandledAt = ExceptionHandledAt.UserCode } : service.CreateCrashTelemetry(exception, ExceptionHandledAt.UserCode);
if (properties != null && properties.Count > 0)
{
Utils.CopyDictionary(properties, telemetry.Context.Properties);
}
this.Track(telemetry);
}