-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathDataverseTraceLogger.cs
756 lines (678 loc) · 34.9 KB
/
DataverseTraceLogger.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
using Microsoft.Extensions.Logging;
using Microsoft.PowerPlatform.Dataverse.Client.Utils;
using Microsoft.Rest;
using Microsoft.Xrm.Sdk;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.ServiceModel;
using System.Linq;
using System.Text;
namespace Microsoft.PowerPlatform.Dataverse.Client
{
/// <summary>
/// Log Entry
/// </summary>
[LocalizableAttribute(false)]
internal sealed class DataverseTraceLogger : TraceLoggerBase
{
// Internal connection of exceptions since last clear.
private List<Exception> _ActiveExceptionsList;
private ILogger _logger;
#region Properties
/// <summary>
/// Last Error from Dataverse
/// </summary>
public new string LastError
{
get { return base.LastError; }
}
/// <summary>
/// Default TraceSource Name
/// </summary>
public string DefaultTraceSourceName
{
get { return "Microsoft.PowerPlatform.Dataverse.Client.ServiceClient"; }
}
/// <summary>
/// Collection of logs captured to date.
/// </summary>
public ConcurrentQueue<Tuple<DateTime, string>> Logs { get; private set; } = new ConcurrentQueue<Tuple<DateTime, string>>();
/// <summary>
/// Defines to the maximum amount of time in Minuets that logs will be kept in memory before being purged
/// </summary>
public TimeSpan LogRetentionDuration { get; set; } = TimeSpan.FromMinutes(5);
/// <summary>
/// Enables or disabled in-memory log capture.
/// Default is false.
/// </summary>
public bool EnabledInMemoryLogCapture { get; set; } = false;
#endregion
#region Public Methods
/// <summary>
/// Constructs the CdsTraceLogger class.
/// </summary>
/// <param name="traceSourceName"></param>
public DataverseTraceLogger(string traceSourceName = "")
: base()
{
if (string.IsNullOrWhiteSpace(traceSourceName))
{
TraceSourceName = DefaultTraceSourceName;
}
else
{
TraceSourceName = traceSourceName;
}
_ActiveExceptionsList = new List<Exception>();
base.Initialize();
}
public DataverseTraceLogger(ILogger logger)
{
_logger = logger;
TraceSourceName = DefaultTraceSourceName;
_ActiveExceptionsList = new List<Exception>();
base.Initialize();
}
public override void ResetLastError()
{
if (base.LastError.Length > 0)
base.LastError = base.LastError.Remove(0, LastError.Length - 1);
LastException = null;
_ActiveExceptionsList.Clear();
}
/// <summary>
/// Clears log cache.
/// </summary>
public void ClearLogCache()
{
if (Logs != null)
{
Logs = new ConcurrentQueue<Tuple<DateTime, string>>();
}
}
/// <summary>
/// Log a Message
/// </summary>
/// <param name="message"></param>
public override void Log(string message)
{
TraceEvent(TraceEventType.Information, (int)TraceEventType.Information, message, null);
}
/// <summary>
/// Log a Trace event
/// </summary>
/// <param name="message"></param>
/// <param name="eventType"></param>
public override void Log(string message, TraceEventType eventType)
{
if (eventType == TraceEventType.Error)
{
Log(message, eventType, new Exception(message));
}
else
{
TraceEvent(eventType, (int)eventType, message, null);
}
}
/// <summary>
/// Log a Trace event
/// </summary>
/// <param name="message"></param>
/// <param name="eventType"></param>
/// <param name="exception"></param>
public override void Log(string message, TraceEventType eventType, Exception exception)
{
if (exception == null && !String.IsNullOrEmpty(message))
{
exception = new Exception(message);
}
StringBuilder detailedDump = new StringBuilder();
StringBuilder lastMessage = new StringBuilder();
lastMessage.AppendLine(message); // Added to fix missing last error line.
detailedDump.AppendLine(message); // Added to fix missing error line.
if (!(exception != null && _ActiveExceptionsList.Contains(exception))) // Skip this line if its already been done.
GetExceptionDetail(exception, detailedDump, 0, lastMessage);
TraceEvent(eventType, (int)eventType, detailedDump.ToString(), exception);
if (eventType == TraceEventType.Error)
{
base.LastError += lastMessage.ToString();
if (!(exception != null && _ActiveExceptionsList.Contains(exception))) // Skip this line if its already been done.
{
// check and or alter the exception is its and HTTPOperationExecption.
if (exception is HttpOperationException httpOperationException)
{
JObject contentBody = JObject.Parse(httpOperationException.Response.Content);
Utils.DataverseOperationException webApiExcept = new Utils.DataverseOperationException(string.IsNullOrEmpty(contentBody["error"]["message"]?.ToString()) ? "Not Provided" : GetFirstLineFromString(contentBody["error"]["message"]?.ToString()).Trim(), httpOperationException);
LastException = webApiExcept;
}
else
LastException = exception;
}
}
_ActiveExceptionsList.Add(exception);
detailedDump.Clear();
lastMessage.Clear();
}
/// <summary>
/// Log an error with an Exception
/// </summary>
/// <param name="exception"></param>
public override void Log(Exception exception)
{
if (exception != null && _ActiveExceptionsList.Contains(exception))
return; // already logged this one .
StringBuilder detailedDump = new StringBuilder();
StringBuilder lastMessage = new StringBuilder();
GetExceptionDetail(exception, detailedDump, 0, lastMessage);
TraceEvent(TraceEventType.Error, (int)TraceEventType.Error, detailedDump.ToString(), exception);
base.LastError += lastMessage.ToString();
LastException = exception;
_ActiveExceptionsList.Add(exception);
detailedDump.Clear();
lastMessage.Clear();
}
/// <summary>
/// log retry message
/// </summary>
/// <param name="retryCount">retryCount</param>
/// <param name="req">request</param>
/// <param name="retryPauseTimeRunning">Value used by the retry system while the code is running, this value can scale up and down based on throttling limits.</param>
/// <param name="isTerminalFailure">represents if it is final retry failure</param>
/// <param name="isThrottled">If set, indicates that this was caused by a throttle</param>
/// <param name="webUriMessageReq"></param>
public void LogRetry(int retryCount, OrganizationRequest req, TimeSpan retryPauseTimeRunning, bool isTerminalFailure = false, bool isThrottled = false, string webUriMessageReq = "")
{
string reqName = req != null ? req.RequestName : webUriMessageReq;
if (retryCount == 0)
{
Log($"No retry attempted for Command {reqName}", TraceEventType.Verbose);
}
else if (isTerminalFailure == true)
{
Log($"Retry Completed at Retry No={retryCount} for Command {reqName}", TraceEventType.Verbose);
}
else
{
Log($"Retry No={retryCount} Retry=Started IsThrottle={isThrottled} Delay={retryPauseTimeRunning} for Command {reqName}", TraceEventType.Warning);
}
}
/// <summary>
/// log exception message
/// </summary>
/// <param name="req">request</param>
/// <param name="ex">exception</param>
/// <param name="errorStringCheck">errorStringCheck</param>
/// <param name="webUriMessageReq"></param>
public void LogException(OrganizationRequest req, Exception ex, string errorStringCheck, string webUriMessageReq = "")
{
if (req != null)
{
Log(string.Format(CultureInfo.InvariantCulture, "************ {3} - {2} : {0} |=> {1}", errorStringCheck, ex.Message, req.RequestName, ex.GetType().Name), TraceEventType.Error, ex);
}
else if (ex is HttpOperationException httpOperationException)
{
JObject contentBody = JObject.Parse(httpOperationException.Response.Content);
DataverseOperationException ex01 = DataverseOperationException.GenerateClientOperationException(httpOperationException);
Log(string.Format(CultureInfo.InvariantCulture, "************ {3} - {2} : {0} |=> {1}", errorStringCheck, DataverseTraceLogger.GetFirstLineFromString(contentBody["error"]["message"].ToString()).Trim(), webUriMessageReq, ex.GetType().Name), TraceEventType.Error, ex01);
}
else
{
Log(string.Format(CultureInfo.InvariantCulture, "************ {3} - {2} : {0} |=> {1}", errorStringCheck, ex.Message, "UNKNOWN", ex.GetType().Name), TraceEventType.Error, ex);
}
}
/// <summary>
/// log failure message
/// </summary>
/// <param name="req">request</param>
/// <param name="requestTrackingId">requestTrackingId</param>
/// <param name="sessionTrackingId">This ID is used to support Dataverse Telemetry</param>
/// <param name="disableConnectionLocking">Connection locking disabled</param>
/// <param name="LockWait">LockWait</param>
/// <param name="logDt">logDt</param>
/// <param name="ex">ex</param>
/// <param name="errorStringCheck">errorStringCheck</param>
/// <param name="isTerminalFailure">represents if it is final retry failure</param>
/// <param name="webUriMessageReq">.</param>
public void LogFailure(OrganizationRequest req, Guid requestTrackingId, Guid? sessionTrackingId, bool disableConnectionLocking, TimeSpan LockWait, Stopwatch logDt, Exception ex, string errorStringCheck, bool isTerminalFailure = false, string webUriMessageReq = "")
{
if (req != null)
{
Log(string.Format(CultureInfo.InvariantCulture, "{6}Failed to Execute Command - {0}{1} : {5}RequestID={2} {3}: {8} duration={4} ExceptionMessage = {7}",
req.RequestName,
disableConnectionLocking ? " : DisableCrossThreadSafeties=true :" : string.Empty,
requestTrackingId.ToString(),
LockWait == TimeSpan.Zero ? string.Empty : string.Format(": LockWaitDuration={0} ", LockWait.ToString()),
logDt.Elapsed.ToString(),
sessionTrackingId.HasValue && sessionTrackingId.Value != Guid.Empty ? $"SessionID={sessionTrackingId} : " : "",
isTerminalFailure ? "[TerminalFailure] " : "",
ex.Message,
errorStringCheck),
TraceEventType.Error, ex);
}
else if (ex is HttpOperationException httpOperationException)
{
string errorMessage = "SERVER ";
DataverseOperationException ex01 = DataverseOperationException.GenerateClientOperationException(httpOperationException);
try
{
JObject contentBody = JObject.Parse(httpOperationException.Response.Content);
errorMessage = DataverseTraceLogger.GetFirstLineFromString(contentBody["error"]["message"].ToString()).Trim();
}
catch (Exception)
{
// unable to parse server response
}
Log(string.Format(CultureInfo.InvariantCulture, "{6}Failed to Execute Command - {0}{1} : {5}RequestID={2} {3}: {8} duration={4} ExceptionMessage = {7}",
webUriMessageReq,
disableConnectionLocking ? " : DisableCrossThreadSafeties=true :" : string.Empty,
requestTrackingId.ToString(),
string.Empty,
logDt.Elapsed.ToString(),
sessionTrackingId.HasValue && sessionTrackingId.Value != Guid.Empty ? $"SessionID={sessionTrackingId.Value} : " : "",
isTerminalFailure ? "[TerminalFailure] " : "",
errorMessage,
errorStringCheck),
TraceEventType.Error, ex);
}
}
/// <summary>
/// Logs data to memory.
/// </summary>
/// <param name="eventType"></param>
/// <param name="id"></param>
/// <param name="message"></param>
/// <param name="ex"></param>
private void TraceEvent(TraceEventType eventType, int id, string message, Exception ex)
{
Source.TraceEvent(eventType, id, message);
LogLevel logLevel = TranslateTraceEventType(eventType);
if (_logger != null && _logger.IsEnabled(logLevel))
{
_logger.Log(logLevel, id, ex, message);
}
if (EnabledInMemoryLogCapture)
{
Logs.Enqueue(Tuple.Create<DateTime, string>(DateTime.UtcNow,
string.Format(CultureInfo.InvariantCulture, "[{0}][{1}] {2}", eventType, id, message)));
DateTime expireDateTime = DateTime.UtcNow.Subtract(LogRetentionDuration);
bool CleanUpLog = true;
while (CleanUpLog)
{
Tuple<DateTime, string> peekOut = null;
if (Logs.TryPeek(out peekOut))
{
if (peekOut.Item1 <= expireDateTime)
{
Tuple<DateTime, string> tos;
Logs.TryDequeue(out tos);
Debug.WriteLine($"Flushing LogEntry from memory: {tos.Item2}"); // Write flush events out to debug log.
tos = null;
}
else
{
CleanUpLog = false;
}
}
else
{
CleanUpLog = false;
}
}
}
}
#endregion
/// <summary>
/// Disassembles the Exception into a readable block
/// </summary>
/// <param name="objException">Exception to work with</param>
/// <param name="sw">Writer to write too</param>
/// <param name="level">depth</param>
/// <param name="lastErrorMsg">Last Writer to write too</param>
private void GetExceptionDetail(object objException, StringBuilder sw, int level, StringBuilder lastErrorMsg)
{
if (objException == null)
return;
if (objException is FaultException<OrganizationServiceFault>)
{
FaultException<OrganizationServiceFault> OrgFault = (FaultException<OrganizationServiceFault>)objException;
string ErrorDetail = GenerateOrgErrorDetailsInfo(OrgFault.Detail.ErrorDetails);
FormatExceptionMessage(
OrgFault.Source != null ? OrgFault.Source.ToString().Trim() : "Not Provided",
OrgFault.TargetSite != null ? OrgFault.TargetSite.Name.ToString() : "Not Provided",
OrgFault.Detail != null ? string.Format(CultureInfo.InvariantCulture, "Message: {0}\nErrorCode: {1}{4}\nTrace: {2}{3}", OrgFault.Detail.Message, OrgFault.Detail.ErrorCode, OrgFault.Detail.TraceText, string.IsNullOrEmpty(ErrorDetail) ? "" : $"\n{ErrorDetail}", OrgFault.Detail.ActivityId == null ? "" : $"\nActivityId: {OrgFault.Detail.ActivityId}") :
string.IsNullOrEmpty(OrgFault.Message) ? "Not Provided" : OrgFault.Message.ToString().Trim(),
string.IsNullOrEmpty(OrgFault.HelpLink) ? "Not Provided" : OrgFault.HelpLink.ToString().Trim(),
string.IsNullOrEmpty(OrgFault.StackTrace) ? "Not Provided" : OrgFault.StackTrace.ToString().Trim()
, sw, level);
lastErrorMsg.Append(OrgFault.Detail != null ? OrgFault.Detail.Message :
string.IsNullOrEmpty(OrgFault.Message) ? string.Empty : OrgFault.Message.ToString().Trim());
if (lastErrorMsg.Length > 0 && (OrgFault.InnerException != null || OrgFault.Detail != null && OrgFault.Detail.InnerFault != null))
lastErrorMsg.Append(" => ");
level++;
if ((OrgFault.InnerException != null || OrgFault.Detail != null && OrgFault.Detail.InnerFault != null))
GetExceptionDetail(OrgFault.Detail != null && OrgFault.Detail.InnerFault != null ? OrgFault.Detail.InnerFault : (object)OrgFault.InnerException,
sw, level, lastErrorMsg);
return;
}
else
{
if (objException is OrganizationServiceFault)
{
OrganizationServiceFault oFault = (OrganizationServiceFault)objException;
string ErrorDetail = GenerateOrgErrorDetailsInfo(oFault.ErrorDetails);
FormatOrgFaultMessage(
string.Format(CultureInfo.InvariantCulture, "Message: {0}\nErrorCode: {1}{4}\nTrace: {2}{3}", oFault.Message, oFault.ErrorCode, oFault.TraceText, string.IsNullOrEmpty(ErrorDetail) ? "" : $"\n{ErrorDetail}", oFault.ActivityId == null ? "" : $"\nActivityId: {oFault.ActivityId}"),
oFault.Timestamp.ToString(),
oFault.ErrorCode.ToString(),
string.IsNullOrEmpty(oFault.HelpLink) ? "Not Provided" : oFault.HelpLink.ToString().Trim(),
string.IsNullOrEmpty(oFault.TraceText) ? "Not Provided" : oFault.TraceText.ToString().Trim(), sw, level);
level++;
lastErrorMsg.Append(oFault.Message);
if (lastErrorMsg.Length > 0 && oFault.InnerFault != null)
lastErrorMsg.Append(" => ");
if (oFault.InnerFault != null)
GetExceptionDetail(oFault.InnerFault, sw, level, lastErrorMsg);
return;
}
else
{
if (objException is HttpOperationException httpOperationException)
{
JObject contentBody = null;
if (!string.IsNullOrEmpty(httpOperationException.Response.Content))
contentBody = JObject.Parse(httpOperationException.Response.Content);
var ErrorBlock = contentBody?["error"];
FormatExceptionMessage(
httpOperationException.Source != null ? httpOperationException.Source.ToString().Trim() : "Not Provided",
httpOperationException.TargetSite != null ? httpOperationException.TargetSite.Name?.ToString() : "Not Provided",
string.IsNullOrEmpty(ErrorBlock?["message"]?.ToString()) ? "Not Provided" : string.Format("Message: {0}{1}\n", GetFirstLineFromString(ErrorBlock?["message"]?.ToString()).Trim(), httpOperationException.Response != null && httpOperationException.Response.Headers.ContainsKey("REQ_ID") ? $"\nActivityId: {ExtractString(httpOperationException.Response.Headers["REQ_ID"])}" : ""),
string.IsNullOrEmpty(httpOperationException.HelpLink) ? "Not Provided" : httpOperationException.HelpLink.ToString().Trim(),
string.IsNullOrEmpty(ErrorBlock?["stacktrace"]?.ToString()) ? "Not Provided" : ErrorBlock["stacktrace"]?.ToString().Trim()
, sw, level);
lastErrorMsg.Append(string.IsNullOrEmpty(httpOperationException.Message) ? "Not Provided" : httpOperationException.Message.ToString().Trim());
// WebEx currently only returns 1 level of error.
var InnerError = contentBody?["error"]["innererror"];
if (lastErrorMsg.Length > 0 && InnerError != null)
{
level++;
lastErrorMsg.Append(" => ");
FormatExceptionMessage(
httpOperationException.Source != null ? httpOperationException.Source.ToString().Trim() : "Not Provided",
httpOperationException.TargetSite != null ? httpOperationException.TargetSite.Name?.ToString() : "Not Provided",
string.IsNullOrEmpty(InnerError?["message"]?.ToString()) ? "Not Provided" : GetFirstLineFromString(InnerError?["message"]?.ToString()).Trim(),
string.IsNullOrEmpty(InnerError?["@Microsoft.PowerApps.CDS.HelpLink"]?.ToString()) ? "Not Provided" : GetFirstLineFromString(InnerError?["@Microsoft.PowerApps.CDS.HelpLink"]?.ToString()).Trim(),
string.IsNullOrEmpty(InnerError?["stacktrace"]?.ToString()) ? "Not Provided" : InnerError?["stacktrace"]?.ToString().Trim()
, sw, level);
}
}
else
{
if (objException is DataverseOperationException cdsOpExecp)
{
FormatSvcFaultMessage(
string.IsNullOrEmpty(cdsOpExecp.Message) ? "Not Provided" : cdsOpExecp.Message.ToString().Trim(),
string.IsNullOrEmpty(cdsOpExecp.Source) ? "Not Provided" : cdsOpExecp.Source.ToString().Trim(),
cdsOpExecp.HResult == -1 ? "Not Provided" : cdsOpExecp.HResult.ToString().Trim(),
cdsOpExecp.Data,
string.IsNullOrEmpty(cdsOpExecp.HelpLink) ? "Not Provided" : cdsOpExecp.HelpLink.ToString().Trim(),
sw,
level);
lastErrorMsg.Append(string.IsNullOrEmpty(cdsOpExecp.Message) ? "Not Provided" : cdsOpExecp.Message.ToString().Trim());
if (lastErrorMsg.Length > 0 && cdsOpExecp.InnerException != null)
lastErrorMsg.Append(" => ");
level++;
if (cdsOpExecp.InnerException != null)
GetExceptionDetail(cdsOpExecp.InnerException, sw, level, lastErrorMsg);
}
else
{
if (objException is Exception)
{
Exception generalEx = (Exception)objException;
FormatExceptionMessage(
generalEx.Source != null ? generalEx.Source.ToString().Trim() : "Not Provided",
generalEx.TargetSite != null ? generalEx.TargetSite.Name.ToString() : "Not Provided",
string.IsNullOrEmpty(generalEx.Message) ? "Not Provided" : generalEx.Message.ToString().Trim(),
string.IsNullOrEmpty(generalEx.HelpLink) ? "Not Provided" : generalEx.HelpLink.ToString().Trim(),
string.IsNullOrEmpty(generalEx.StackTrace) ? "Not Provided" : generalEx.StackTrace.ToString().Trim()
, sw, level);
lastErrorMsg.Append(string.IsNullOrEmpty(generalEx.Message) ? "Not Provided" : generalEx.Message.ToString().Trim());
if (lastErrorMsg.Length > 0 && generalEx.InnerException != null)
lastErrorMsg.Append(" => ");
level++;
if (generalEx.InnerException != null)
GetExceptionDetail(generalEx.InnerException, sw, level, lastErrorMsg);
}
}
}
}
}
return;
}
private static string ExtractString(IEnumerable<string> enumerable)
{
string sOut = string.Empty;
if (enumerable != null)
{
List<string> lst = new List<string>(enumerable);
foreach (var itm in lst.Distinct())
{
if (string.IsNullOrEmpty(sOut))
sOut += $"{itm}";
else
sOut += $"|{itm}";
}
}
return sOut;
}
/// <summary>
/// returns the first line from the text block.
/// </summary>
/// <param name="textBlock"></param>
/// <returns></returns>
internal static string GetFirstLineFromString(string textBlock)
{
if (!string.IsNullOrEmpty(textBlock))
{
try
{
int iCopyToo = textBlock.IndexOf(Environment.NewLine);
if (iCopyToo > 0)
return textBlock.Substring(0, textBlock.IndexOf(Environment.NewLine));
}
catch { } // No Op let it fall though
}
return textBlock;
}
/// <summary>
/// Formats the detail collection from a service exception.
/// </summary>
/// <param name="errorDetails"></param>
/// <returns></returns>
private static string GenerateOrgErrorDetailsInfo(ErrorDetailCollection errorDetails)
{
if (errorDetails != null && errorDetails.Count > 0)
{
StringBuilder sw = new StringBuilder();
sw.AppendLine("Error Details\t:");
foreach (var itm in errorDetails)
{
string valueText = itm.Value != null ? itm.Value.ToString() : "Not Set";
sw.AppendLine($"{itm.Key}\t: {valueText}");
}
return sw.ToString();
}
return string.Empty;
}
/// <summary>
/// Creates the exception message.
/// </summary>
/// <param name="source">Source of Exception</param>
/// <param name="targetSite">Target of Exception</param>
/// <param name="message">Exception Message</param>
/// <param name="stackTrace">StackTrace</param>
/// <param name="helpLink">Url for help. </param>
/// <param name="sw">Writer to write too</param>
/// <param name="level">Depth of Exception</param>
private static void FormatExceptionMessage(string source, string targetSite, string message, string helpLink, string stackTrace, StringBuilder sw, int level)
{
if (level != 0)
sw.AppendLine($"Inner Exception Level {level}\t: ");
sw.AppendLine("Source: " + source);
sw.AppendLine("Method: " + targetSite);
sw.AppendLine("DateUTC: " + DateTime.UtcNow.ToShortDateString());
sw.AppendLine("TimeUTC: " + DateTime.UtcNow.ToLongTimeString());
sw.AppendLine("Error: " + message);
sw.AppendLine($"HelpLink Url: {helpLink}");
sw.AppendLine("Stack Trace: " + stackTrace);
sw.AppendLine("======================================================================================================================");
}
/// <summary>
/// Formats an Exception specific to an organization fault.
/// </summary>
/// <param name="message">Exception Message</param>
/// <param name="timeOfEvent">Time occurred</param>
/// <param name="errorCode">Error code of message</param>
/// <param name="traceText">Message Text</param>
/// <param name="helpLink">Help Link URL</param>
/// <param name="sw">Writer to write too</param>
/// <param name="level">Depth</param>
private static void FormatOrgFaultMessage(string message, string timeOfEvent, string errorCode, string traceText, string helpLink, StringBuilder sw, int level)
{
if (level != 0)
sw.AppendLine($"Inner Exception Level {level}\t: ");
sw.AppendLine("==OrganizationServiceFault Info=======================================================================================");
sw.AppendLine("Error: " + message);
sw.AppendLine("Time: " + timeOfEvent);
sw.AppendLine("ErrorCode: " + errorCode);
sw.AppendLine("DateUTC: " + DateTime.UtcNow.ToShortDateString());
sw.AppendLine("TimeUTC: " + DateTime.UtcNow.ToLongTimeString());
sw.AppendLine($"HelpLink Url: {helpLink}");
sw.AppendLine("Trace: " + traceText);
sw.AppendLine("======================================================================================================================");
}
/// <summary>
/// Formats an Exception specific to an organization fault.
/// </summary>
/// <param name="message">Exception Message</param>
/// <param name="source">Source of error.</param>
/// <param name="errorCode">Error code of message</param>
/// <param name="dataItems">Data Items</param>
/// <param name="helpLink">Help Link</param>
/// <param name="sw">Writer to write too</param>
/// <param name="level">Depth</param>
private static void FormatSvcFaultMessage(string message, string source, string errorCode, System.Collections.IDictionary dataItems, string helpLink, StringBuilder sw, int level)
{
if (level != 0)
sw.AppendLine($"Inner Exception Level {level}\t: ");
sw.AppendLine("==DataverseOperationException Info=======================================================================================");
sw.AppendLine($"Source: {source}");
sw.AppendLine("Error: " + message);
sw.AppendLine("ErrorCode: " + errorCode);
sw.AppendLine("DateUTC: " + DateTime.UtcNow.ToShortDateString());
sw.AppendLine("TimeUTC: " + DateTime.UtcNow.ToLongTimeString());
sw.AppendLine($"HelpLink Url: {helpLink}");
if (dataItems != null && dataItems.Count > 0)
{
sw.AppendLine("DataverseErrorDetail:");
foreach (System.Collections.DictionaryEntry itm in dataItems)
{
sw.AppendLine($"\t{itm.Key}: {itm.Value}");
}
}
sw.AppendLine("======================================================================================================================");
}
private static LogLevel TranslateTraceEventType(TraceEventType traceLevel)
{
switch (traceLevel)
{
case TraceEventType.Critical:
return LogLevel.Critical;
case TraceEventType.Error:
return LogLevel.Error;
case TraceEventType.Warning:
return LogLevel.Warning;
case TraceEventType.Information:
return LogLevel.Information;
case TraceEventType.Verbose:
return LogLevel.Debug;
default:
return LogLevel.None;
}
}
}
/// <summary>
/// This class provides an override for the default trace settings.
/// These settings must be set before the components in the control are used for them to be effective.
/// </summary>
public class TraceControlSettings
{
private static string _traceSourceName = "Microsoft.PowerPlatform.Dataverse.Client.ServiceClient";
/// <summary>
/// Returns the Registered Trace Listeners in the override object.
/// </summary>
internal static Dictionary<string, TraceListener> RegisterdTraceListeners
{
get
{
return TraceSourceSettingStore.GetTraceSourceSettings(_traceSourceName) != null ?
TraceSourceSettingStore.GetTraceSourceSettings(_traceSourceName).TraceListeners : null;
}
}
/// <summary>
/// Override Trace Level setting.
/// </summary>
public static SourceLevels TraceLevel { get; set; }
/// <summary>
/// Builds the base trace settings
/// </summary>
static TraceControlSettings()
{
TraceLevel = SourceLevels.Off;
}
/// <summary>
/// Closes any trace listeners that were configured
/// </summary>
public static void CloseListeners()
{
if (RegisterdTraceListeners != null && RegisterdTraceListeners.Count > 0)
foreach (TraceListener itm in RegisterdTraceListeners.Values)
{
itm.Close();
}
}
/// <summary>
/// Adds a listener to the trace listen array
/// </summary>
/// <param name="listenerToAdd">Trace Listener you wish to add</param>
/// <returns>true on success, false on fail.</returns>
public static bool AddTraceListener(TraceListener listenerToAdd)
{
try
{
Trace.AutoFlush = true;
var traceSourceSetting = TraceSourceSettingStore.GetTraceSourceSettings(_traceSourceName);
if (traceSourceSetting == null)
{
traceSourceSetting = new TraceSourceSetting(_traceSourceName, TraceLevel);
}
if (traceSourceSetting.TraceListeners == null)
traceSourceSetting.TraceListeners = new Dictionary<string, TraceListener>();
if (traceSourceSetting.TraceListeners.ContainsKey(listenerToAdd.Name))
return true;
traceSourceSetting.TraceListeners.Add(listenerToAdd.Name, listenerToAdd);
TraceSourceSettingStore.AddTraceSettingsToStore(traceSourceSetting);
return true;
}
catch
{
return false;
}
}
}
}