Skip to content

Commit

Permalink
add Telemetry extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonh-msft committed Oct 24, 2016
1 parent 8b98584 commit 0c9dc50
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions CSharp/core-AppInsights/AppInsightsBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StateDialog.cs" />
<Compile Include="TelemetryExtensions.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
Expand Down
76 changes: 76 additions & 0 deletions CSharp/core-AppInsights/TelemetryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Bot.Builder.Dialogs;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace AppInsightsBot
{
public static class TelemetryExtensions
{
public static TraceTelemetry CreateTraceTelemetry(this IDialogContext ctx, string message = null, IDictionary<string, string> properties = null)
{
var t = new TraceTelemetry(message);
t.Properties.Add("ConversationData", JsonConvert.SerializeObject(ctx.ConversationData));
t.Properties.Add("PrivateConversationData", JsonConvert.SerializeObject(ctx.PrivateConversationData));
t.Properties.Add("UserData", JsonConvert.SerializeObject(ctx.UserData));

var m = ctx.MakeMessage();
t.Properties.Add("ConversationId", m.Conversation.Id);
t.Properties.Add("UserId", m.Recipient.Id);

if (properties != null)
{
foreach (var p in properties)
{
t.Properties.Add(p);
}
}

return t;
}

public static EventTelemetry CreateEventTelemetry(this IDialogContext ctx, string message = null, IDictionary<string, string> properties = null)
{
var t = new EventTelemetry(message);
t.Properties.Add("ConversationData", JsonConvert.SerializeObject(ctx.ConversationData));
t.Properties.Add("PrivateConversationData", JsonConvert.SerializeObject(ctx.PrivateConversationData));
t.Properties.Add("UserData", JsonConvert.SerializeObject(ctx.UserData));

var m = ctx.MakeMessage();
t.Properties.Add("ConversationId", m.Conversation.Id);
t.Properties.Add("UserId", m.Recipient.Id);

if (properties != null)
{
foreach (var p in properties)
{
t.Properties.Add(p);
}
}

return t;
}

public static ExceptionTelemetry CreateExceptionTelemetry(this IDialogContext ctx, System.Exception ex, IDictionary<string, string> properties = null)
{
var t = new ExceptionTelemetry(ex);
t.Properties.Add("ConversationData", JsonConvert.SerializeObject(ctx.ConversationData));
t.Properties.Add("PrivateConversationData", JsonConvert.SerializeObject(ctx.PrivateConversationData));
t.Properties.Add("UserData", JsonConvert.SerializeObject(ctx.UserData));

var m = ctx.MakeMessage();
t.Properties.Add("ConversationId", m.Conversation.Id);
t.Properties.Add("UserId", m.Recipient.Id);

if (properties != null)
{
foreach (var p in properties)
{
t.Properties.Add(p);
}
}

return t;
}
}
}

0 comments on commit 0c9dc50

Please sign in to comment.