Skip to content

Commit 99e69ca

Browse files
author
Matt Watson
committed
New ProfileTracer
1 parent c11e84b commit 99e69ca

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

Dlls/StackifyLib/StackifyLib.dll

1.5 KB
Binary file not shown.

Src/StackifyLib/ProfileTracer.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

Src/StackifyLib/StackifyLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="Internal\Logs\LogQueue.cs" />
5858
<Compile Include="Logger.cs" />
5959
<Compile Include="Models\LogMessage.cs" />
60+
<Compile Include="ProfileTracer.cs" />
6061
<Compile Include="StringException.cs" />
6162
<Compile Include="Models\LogMsgGroup.cs" />
6263
<Compile Include="Extensions.cs" />

0 commit comments

Comments
 (0)