@@ -24,7 +24,7 @@ public class ProfileTracer
2424 private string _RequestID = null ;
2525 internal bool IsOperation { get ; set ; }
2626#if NET45 || NETSTANDARD1_3
27- // private static EtwEventListener _etwEventListener = null;
27+ private static EtwEventListener _etwEventListener = null ;
2828#endif
2929 internal ProfileTracer ( string methodDisplayText , string requestLevelReportingCategory , string appLevelReportingCategory )
3030 {
@@ -114,6 +114,18 @@ public static ProfileTracer CreateAsCodeBlock(string methodDisplayText)
114114 return tracer ;
115115 }
116116
117+ /// <summary>
118+ /// Used by non web apps to define transactions in code that are turned in to operations to be tracked in Stackify APM or Prefix
119+ /// </summary>
120+ /// <param name="operationName"></param>
121+ /// <param name="uniqueOperationID"></param>
122+ /// <returns></returns>
123+ public static ProfileTracer CreateAsTrackedFunction ( string functionName )
124+ {
125+ ProfileTracer tracer = new ProfileTracer ( functionName , "Tracked Function" , null ) ;
126+ return tracer ;
127+ }
128+
117129 /// <summary>
118130 /// Used by non web apps to define transactions in code that are turned in to operations to be tracked in Stackify APM or Prefix
119131 /// </summary>
@@ -123,15 +135,15 @@ public static ProfileTracer CreateAsCodeBlock(string methodDisplayText)
123135 public static ProfileTracer CreateAsOperation ( string operationName , string uniqueOperationID = null )
124136 {
125137#if NET45 || NETSTANDARD1_3
126- //if (_etwEventListener == null)
127- // _etwEventListener = new EtwEventListener();
138+ if ( _etwEventListener == null )
139+ _etwEventListener = new EtwEventListener ( ) ;
128140#endif
129141 ProfileTracer tracer = new ProfileTracer ( operationName , null , null ) ;
130142 tracer . IsOperation = true ;
131143
132144 if ( ! string . IsNullOrEmpty ( uniqueOperationID ) )
133145 {
134- tracer . _transactionID = uniqueOperationID ;
146+ tracer . _RequestID = uniqueOperationID ;
135147 }
136148
137149 return tracer ;
@@ -143,13 +155,23 @@ public static ProfileTracer CreateAsOperation(string operationName, string uniqu
143155 /// <param name="methodDisplayText"></param>
144156 /// <returns></returns>
145157
146- public static ProfileTracer CreateAsCodeBlock ( string methodDisplayText , string requestLevelReportingCategory , string appLevelReportingCategory = null )
158+ public static ProfileTracer CreateAsDependency ( string methodDisplayText , string requestLevelReportingCategory , string appLevelReportingCategory = null )
147159 {
148160 ProfileTracer tracer = new ProfileTracer ( methodDisplayText , requestLevelReportingCategory , appLevelReportingCategory ) ;
149161 return tracer ;
150162 }
151163
152164
165+ public ProfileTracer SetUniqueOperationID ( string uniqueOperationID )
166+ {
167+ if ( ! string . IsNullOrEmpty ( uniqueOperationID ) )
168+ {
169+ this . _RequestID = uniqueOperationID ;
170+ }
171+
172+ return this ;
173+ }
174+
153175 public ProfileTracer CreateMetric ( string categoryName , string metricName , bool trackCount = true , bool trackTime = true , bool autoReportZeroIfNothingReported = false )
154176 {
155177 _customMetricCategory = categoryName ;
@@ -206,6 +228,14 @@ private Task ExecInternal2(string values, Func<Task> action)
206228 return t ;
207229 }
208230
231+ [ MethodImpl ( MethodImplOptions . PreserveSig | MethodImplOptions . NoInlining | MethodImplOptions . NoOptimization ) ]
232+ private Task < T > ExecInternal2 < T > ( string values , Func < Task < T > > action )
233+ {
234+ Task < T > t = action ( ) ;
235+ ExecInternalTaskStarted2 ( _transactionID + "|" + t . Id + "|" + IsOperation ) ;
236+ return t ;
237+ }
238+
209239
210240 [ MethodImpl ( MethodImplOptions . PreserveSig | MethodImplOptions . NoInlining | MethodImplOptions . NoOptimization ) ]
211241 private Task ExecInternalOperation ( string values , Func < Task > action )
@@ -215,6 +245,14 @@ private Task ExecInternalOperation(string values, Func<Task> action)
215245 return t ;
216246 }
217247
248+ [ MethodImpl ( MethodImplOptions . PreserveSig | MethodImplOptions . NoInlining | MethodImplOptions . NoOptimization ) ]
249+ private Task < T > ExecInternalOperation < T > ( string values , Func < Task < T > > action )
250+ {
251+ Task < T > t = action ( ) ;
252+ ExecInternalTaskStarted2 ( _transactionID + "|" + t . Id + "|" + IsOperation ) ;
253+ return t ;
254+ }
255+
218256
219257 [ MethodImpl ( MethodImplOptions . PreserveSig | MethodImplOptions . NoInlining | MethodImplOptions . NoOptimization ) ]
220258 private void ExecInternalTaskStarted2 ( string values )
@@ -253,7 +291,38 @@ public void Exec(Action action)
253291
254292 }
255293
294+ public Task < T > ExecAsync < T > ( Func < Task < T > > task )
295+ {
296+ DateTimeOffset now = DateTimeOffset . UtcNow ;
297+
298+ Task < T > t ;
299+
300+
301+ if ( this . IsOperation )
302+ {
303+ t = ExecInternalOperation < T > ( _methodDisplayText + "|" + ( ignoreChildFrames ? 1 : 0 ) . ToString ( ) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionID + "|" + _RequestID + "|" + IsOperation , task ) ;
304+ }
305+ else
306+ {
307+ t = ExecInternal2 < T > ( _methodDisplayText + "|" + ( ignoreChildFrames ? 1 : 0 ) . ToString ( ) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionID + "|" + _RequestID + "|" + IsOperation , task ) ;
308+ }
309+
310+ t . ContinueWith ( ( tend ) =>
311+ {
312+ if ( _customMetricTime )
313+ {
314+ Metrics . Time ( _customMetricCategory , _customMetricName + " Time" , now ) ;
315+ }
316+ ExecInternalComplete2 ( _transactionID + "|" + _RequestID + "|" + tend . Id + "|" + IsOperation ) ;
317+ } ) ;
318+
319+ if ( _customMetricCount )
320+ {
321+ Metrics . Count ( _customMetricCategory , _customMetricName , 1 , _autoReportZeroIfNothingReported ) ;
322+ }
256323
324+ return t ;
325+ }
257326
258327 public Task ExecAsync ( Func < Task > task )
259328 {
0 commit comments