1+ #if NET45 || NET451
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Diagnostics ;
5+ using System . Linq ;
6+ using System . Reflection ;
7+ using System . Text ;
8+
9+ namespace StackifyLib . Utils
10+ {
11+ public class AsyncTracer
12+ {
13+ private static bool _AsyncTracerEnabled = false ;
14+ private static DateTime _LastEnabledTest = DateTime . MinValue ;
15+
16+ public static bool EnsureAsyncTracer ( )
17+ {
18+ if ( _AsyncTracerEnabled )
19+ return _AsyncTracerEnabled ;
20+
21+ if ( _LastEnabledTest > DateTime . UtcNow . AddMinutes ( - 5 ) )
22+ {
23+ return _AsyncTracerEnabled ;
24+ }
25+
26+ try
27+ {
28+ var t = Type . GetType ( "System.Threading.Tasks.AsyncCausalityTracer" ) ;
29+ if ( t != null )
30+ {
31+ var field = t . GetField ( "f_LoggingOn" , BindingFlags . NonPublic | BindingFlags . Static ) ;
32+
33+ if ( field == null )
34+ {
35+ StackifyLib . Utils . StackifyAPILogger . Log ( "Unable to enable the AsyncCausalityTracer, f_LoggingOn field not found" ) ;
36+ return _AsyncTracerEnabled ;
37+ }
38+
39+
40+ if ( field . FieldType . Name == "Boolean" )
41+ {
42+ bool current = ( bool ) field . GetValue ( null ) ;
43+
44+ if ( ! current )
45+ {
46+ field . SetValue ( null , true ) ;
47+ }
48+ }
49+ else
50+ {
51+ field . SetValue ( null , ( byte ) 4 ) ;
52+ }
53+
54+ _AsyncTracerEnabled = true ;
55+
56+
57+
58+ }
59+ else
60+ {
61+ _AsyncTracerEnabled = true ; //never going to work.
62+ StackifyLib . Utils . StackifyAPILogger . Log ( "Unable to enable the AsyncCausalityTracer, class not found" ) ;
63+ }
64+ }
65+ catch ( Exception ex )
66+ {
67+ StackifyLib . Utils . StackifyAPILogger . Log ( "EnsureAsyncTracer Exception: " + ex . ToString ( ) ) ;
68+ Debug . WriteLine ( ex ) ;
69+ }
70+ _LastEnabledTest = DateTime . UtcNow ;
71+
72+ return _AsyncTracerEnabled ;
73+ }
74+ }
75+ }
76+ #endif
0 commit comments