1
- using Microsoft . Diagnostics . Instrumentation . Extensions . Intercept ;
2
- using System ;
1
+ using System ;
3
2
using System . Collections . Generic ;
4
3
using System . Diagnostics ;
5
- using System . Linq ;
4
+ using System . Net . Mail ;
6
5
using System . Text ;
7
6
using System . Threading ;
8
- using System . Threading . Tasks ;
7
+
8
+ using Microsoft . ApplicationInsights ;
9
+ using Microsoft . ApplicationInsights . Channel ;
10
+ using Microsoft . ApplicationInsights . DataContracts ;
11
+ using Microsoft . ApplicationInsights . Extensibility ;
12
+ using Microsoft . ApplicationInsights . Extensibility . Implementation ;
13
+ using Microsoft . Diagnostics . Instrumentation . Extensions . Intercept ;
9
14
10
15
namespace SimpleConsoleApp
11
16
{
@@ -19,6 +24,8 @@ static void Main(string[] args)
19
24
return ;
20
25
}
21
26
27
+ TelemetryConfiguration . Active . InstrumentationKey = "test" ;
28
+
22
29
Console . WriteLine ( "Agent version: " + Decorator . GetAgentVersion ( ) ) ;
23
30
24
31
Decorator . InitializeExtension ( ) ;
@@ -36,8 +43,12 @@ public static void Execute()
36
43
{
37
44
try
38
45
{
39
- System . Net . Mail . SmtpClient client = new System . Net . Mail . SmtpClient ( ) ;
40
- client . Send ( null ) ;
46
+ System . Net . Mail . SmtpClient client = new System . Net . Mail . SmtpClient ( "host.myserver.abc" , 443 ) ;
47
+ client . Send ( new MailMessage (
48
+ from : "sergkanz@microsoft.com" ,
49
+ to : "chucknorris@microsoft.com" ,
50
+ subject : "This is RTIA example" ,
51
+ body : "Check out this example on github" ) ) ;
41
52
}
42
53
catch ( Exception )
43
54
{
@@ -47,24 +58,51 @@ public static void Execute()
47
58
48
59
public static object OnBegin ( object thisObj , object arg1 )
49
60
{
61
+ // diagnostics output
50
62
Console . WriteLine ( "Begin callback" ) ;
51
63
Console . WriteLine ( "Callstack: " + new StackTrace ( ) . ToString ( ) ) ;
52
64
53
- return null ;
65
+ // start the operation
66
+ var operation = new TelemetryClient ( ) . StartOperation < DependencyTelemetry > ( "Send" ) ;
67
+ operation . Telemetry . Target = ( ( SmtpClient ) thisObj ) . Host ;
68
+ if ( arg1 != null )
69
+ {
70
+ operation . Telemetry . Data = ( ( MailMessage ) arg1 ) . Subject ;
71
+ }
72
+
73
+ // save the operation in the local context
74
+ return operation ;
54
75
}
55
76
56
77
public static object OnEnd ( object context , object returnValue , object thisObj , object arg1 )
57
78
{
79
+ // diagnostics output
58
80
Console . WriteLine ( "End callback" ) ;
59
81
Console . WriteLine ( "Callstack: " + new StackTrace ( ) . ToString ( ) ) ;
60
82
83
+ // stop the operation. Getting the operation from the context
84
+ var operation = ( IOperationHolder < DependencyTelemetry > ) context ;
85
+ new TelemetryClient ( ) . StopOperation ( operation ) ;
86
+
87
+ // you must return original return value unless you want to change it
61
88
return returnValue ;
62
89
}
63
90
64
91
public static void OnException ( object context , object exception , object thisObj , object arg1 )
65
92
{
93
+ // diagnostics output
66
94
Console . WriteLine ( "Exception callback" ) ;
67
95
Console . WriteLine ( "Callstack: " + new StackTrace ( ) . ToString ( ) ) ;
96
+
97
+ // mark operation as failed and stop it. Getting the operation from the context
98
+ var operation = ( IOperationHolder < DependencyTelemetry > ) context ;
99
+ operation . Telemetry . Success = false ;
100
+ operation . Telemetry . ResultCode = exception . GetType ( ) . Name ;
101
+ new TelemetryClient ( ) . StopOperation ( operation ) ;
102
+
103
+ // this is just to trace the result:
104
+ Console . WriteLine ( "Telemetry item: " + Encoding . Default . GetString (
105
+ JsonSerializer . Serialize ( new List < ITelemetry > ( ) { operation . Telemetry } , false ) ) ) ;
68
106
}
69
107
}
70
108
}
0 commit comments