Skip to content

Commit b46722c

Browse files
Sergey KanzhelevSergey Kanzhelev
authored andcommitted
all comments are there
1 parent 0431b7e commit b46722c

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

SimpleConsoleApp/Program.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
using Microsoft.Diagnostics.Instrumentation.Extensions.Intercept;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Diagnostics;
5-
using System.Linq;
4+
using System.Net.Mail;
65
using System.Text;
76
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;
914

1015
namespace SimpleConsoleApp
1116
{
@@ -19,6 +24,8 @@ static void Main(string[] args)
1924
return;
2025
}
2126

27+
TelemetryConfiguration.Active.InstrumentationKey = "test";
28+
2229
Console.WriteLine("Agent version: " + Decorator.GetAgentVersion());
2330

2431
Decorator.InitializeExtension();
@@ -36,8 +43,12 @@ public static void Execute()
3643
{
3744
try
3845
{
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"));
4152
}
4253
catch (Exception)
4354
{
@@ -47,24 +58,51 @@ public static void Execute()
4758

4859
public static object OnBegin(object thisObj, object arg1)
4960
{
61+
// diagnostics output
5062
Console.WriteLine("Begin callback");
5163
Console.WriteLine("Callstack: " + new StackTrace().ToString());
5264

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;
5475
}
5576

5677
public static object OnEnd(object context, object returnValue, object thisObj, object arg1)
5778
{
79+
// diagnostics output
5880
Console.WriteLine("End callback");
5981
Console.WriteLine("Callstack: " + new StackTrace().ToString());
6082

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
6188
return returnValue;
6289
}
6390

6491
public static void OnException(object context, object exception, object thisObj, object arg1)
6592
{
93+
// diagnostics output
6694
Console.WriteLine("Exception callback");
6795
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)));
68106
}
69107
}
70108
}

SimpleConsoleApp/SimpleConsoleApp.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
<HintPath>..\packages\Microsoft.ApplicationInsights.Agent.Intercept.2.0.5\lib\net45\Microsoft.AI.Agent.Intercept.dll</HintPath>
5858
<Private>True</Private>
5959
</Reference>
60+
<Reference Include="Microsoft.ApplicationInsights, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
61+
<HintPath>..\packages\Microsoft.ApplicationInsights.2.2.0-beta4\lib\net46\Microsoft.ApplicationInsights.dll</HintPath>
62+
<Private>True</Private>
63+
</Reference>
6064
<Reference Include="System" />
6165
<Reference Include="System.Core" />
6266
<Reference Include="System.Xml.Linq" />
@@ -71,6 +75,7 @@
7175
</ItemGroup>
7276
<ItemGroup>
7377
<None Include="App.config" />
78+
<None Include="example.json" />
7479
<None Include="nuget.config" />
7580
<None Include="packages.config">
7681
<SubType>Designer</SubType>

SimpleConsoleApp/example.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Microsoft.ApplicationInsights.test.RemoteDependency",
3+
"time": "2016-11-18T00:24:20.0997535Z",
4+
"iKey": "test",
5+
"tags": {
6+
"ai.internal.sdkVersion": "dotnet:2.2.0-48047",
7+
"ai.internal.nodeName": "sergkanz-vm.redmond.corp.microsoft.com",
8+
"ai.operation.id": "2ot/xSKqO2w=",
9+
"ai.cloud.roleInstance": "sergkanz-vm.redmond.corp.microsoft.com",
10+
"ai.operation.name": "Send"
11+
},
12+
"data": {
13+
"baseType": "RemoteDependencyData",
14+
"baseData": {
15+
"ver": 2,
16+
"name": "Send",
17+
"id": "2ot/xSKqO2w=",
18+
"data": "This is RTIA example",
19+
"duration": "00:00:00.9100000",
20+
"resultCode": "SmtpException",
21+
"success": false,
22+
"target": "host.myserver.abc"
23+
}
24+
}
25+
}

SimpleConsoleApp/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Microsoft.ApplicationInsights" version="2.2.0-beta4" targetFramework="net46" />
34
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.5" targetFramework="net46" />
45
<package id="Microsoft.ApplicationInsights.Agent_x64" version="2.0.5" targetFramework="net46" />
56
<package id="Microsoft.ApplicationInsights.Agent_x86" version="2.0.5" targetFramework="net46" />

0 commit comments

Comments
 (0)