|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.Remoting.Messaging; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace StackifyLib |
| 10 | +{ |
| 11 | + public class ProfileTracer |
| 12 | + { |
| 13 | + private string _methodDisplayText = null; |
| 14 | + private bool ignoreChildFrames = false; |
| 15 | + |
| 16 | + private string _requestReportingCategory = null; |
| 17 | + private string _appReportingCategory = null; |
| 18 | + |
| 19 | + private bool _customMetricCount = false; |
| 20 | + private bool _customMetricTime = false; |
| 21 | + private bool _autoReportZeroIfNothingReported = false; |
| 22 | + |
| 23 | + private string _customMetricCategory = null; |
| 24 | + private string _customMetricName = null; |
| 25 | + |
| 26 | + private Guid _transactionID = Guid.NewGuid(); |
| 27 | + private Guid _RequestID = Guid.Empty; |
| 28 | + |
| 29 | + public ProfileTracer(string methodDisplayText, string requestLevelReportingCategory, string appLevelReportingCategory) |
| 30 | + { |
| 31 | + _methodDisplayText = methodDisplayText; |
| 32 | + _requestReportingCategory = requestLevelReportingCategory; |
| 33 | + _appReportingCategory = appLevelReportingCategory; |
| 34 | + |
| 35 | + Object correltionManagerId = CallContext.LogicalGetData("E2ETrace.ActivityID"); |
| 36 | + |
| 37 | + if (correltionManagerId != null && correltionManagerId is Guid && ((Guid) correltionManagerId) != Guid.Empty) |
| 38 | + { |
| 39 | + _RequestID = (Guid)correltionManagerId; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public static ProfileTracer Create(string methodDisplayText) |
| 44 | + { |
| 45 | + ProfileTracer tracer = new ProfileTracer(methodDisplayText, null, null); |
| 46 | + return tracer; |
| 47 | + } |
| 48 | + |
| 49 | + public static ProfileTracer Create(string methodDisplayText, string requestLevelReportingCategory, string appLevelReportingCategory = null) |
| 50 | + { |
| 51 | + ProfileTracer tracer = new ProfileTracer(methodDisplayText, requestLevelReportingCategory, appLevelReportingCategory); |
| 52 | + return tracer; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + public ProfileTracer CreateMetric(string categoryName, string metricName, bool trackCount = true, bool trackTime = true, bool autoReportZeroIfNothingReported = false) |
| 57 | + { |
| 58 | + _customMetricCategory = categoryName; |
| 59 | + _customMetricName = metricName; |
| 60 | + _customMetricCount = trackCount; |
| 61 | + _customMetricTime = trackTime; |
| 62 | + _autoReportZeroIfNothingReported = autoReportZeroIfNothingReported; |
| 63 | + |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public ProfileTracer IgnoreChildFrames(bool value = true) |
| 68 | + { |
| 69 | + ignoreChildFrames = value; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public void Exec(Action action) |
| 74 | + { |
| 75 | + string actionID = _transactionID.ToString(); |
| 76 | + string requestID = _RequestID.ToString(); |
| 77 | + |
| 78 | + ExecInternal(action, _methodDisplayText, ignoreChildFrames ? 1 : 0, _requestReportingCategory, _appReportingCategory, actionID, requestID); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + [MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] |
| 83 | + private void ExecInternalComplete(string actionID, string requestID) |
| 84 | + { |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + //Method the profiler looks for |
| 89 | + [MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] |
| 90 | + private void ExecInternal(Action action, string traceDisplay, int suppressChildren, string requestLevelReportingCategory, string appLevelReportingCategory, string actionID, string requestID) |
| 91 | + { |
| 92 | + |
| 93 | + if (_customMetricTime) |
| 94 | + { |
| 95 | + DateTimeOffset now = DateTimeOffset.UtcNow; |
| 96 | + action(); |
| 97 | + |
| 98 | + Metrics.Time(_customMetricCategory, _customMetricName + " Time", now); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + action(); |
| 103 | + } |
| 104 | + |
| 105 | + if (_customMetricCount) |
| 106 | + { |
| 107 | + Metrics.Count(_customMetricCategory, _customMetricName, 1, _autoReportZeroIfNothingReported); |
| 108 | + } |
| 109 | + |
| 110 | + ExecInternalComplete(actionID, requestID); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments