Skip to content

Commit 6fcfabb

Browse files
authored
Create CustomActivityLogger.cs
1 parent e6720f9 commit 6fcfabb

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using Microsoft.ApplicationInsights;
2+
using Microsoft.Azure;
3+
using Microsoft.Bot.Builder.Dialogs;
4+
using Microsoft.Bot.Builder.History;
5+
using Microsoft.Bot.Connector;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Security.Cryptography;
10+
using System.Text;
11+
using System.Threading.Tasks;
12+
using System.Web;
13+
14+
namespace Microsoft.Bot.Web.CustomizationBotBuilder
15+
{
16+
public class CustomActivityLogger : IActivityLogger
17+
{
18+
public static string GetValidCloudBlobContainerName(string containerName)
19+
{
20+
using (MD5 md5 = new MD5CryptoServiceProvider())
21+
{
22+
byte[] inputBytes = Encoding.UTF8.GetBytes(containerName);
23+
byte[] hashBytes = md5.ComputeHash(inputBytes);
24+
25+
StringBuilder sb = new StringBuilder();
26+
foreach (byte b in hashBytes)
27+
{
28+
sb.Append(b.ToString("X2"));
29+
}
30+
return sb.ToString().ToLower();
31+
}
32+
}
33+
public Task LogAsync(IActivity activity)
34+
{
35+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(activity, Newtonsoft.Json.Formatting.Indented);
36+
var telemerty = new TelemetryClient(new ApplicationInsights.Extensibility.TelemetryConfiguration(CloudConfigurationManager.GetSetting("APPINSIGHTS_INSTRUMENTATIONKEY")));
37+
if (activity.AsMessageActivity() != null)
38+
{
39+
var message = activity.AsMessageActivity();
40+
var properties = new Dictionary<string, string>
41+
{
42+
{ "Conversation ID", message.Conversation.Id },
43+
{ "Channel ID", message.ChannelId },
44+
{ "Text", message.Text},
45+
{ "Activity ID", message.Id},
46+
{ "From ID", message.From.Id},
47+
{ "From Name", message.From.Name },
48+
{ "Recipient ID", message.Recipient.Id},
49+
{ "Bot ID", CloudConfigurationManager.GetSetting("BotId")},
50+
{ "Conversation Reference", json},
51+
{ "Attachments", message.Attachments != null ? Newtonsoft.Json.JsonConvert.SerializeObject(message.Attachments): string.Empty },
52+
{ "Timestamp", message.Timestamp.HasValue? message.Timestamp.Value.ToString():string.Empty },
53+
{ "Entities", message.Entities != null? Newtonsoft.Json.JsonConvert.SerializeObject(message.Entities): string.Empty }
54+
};
55+
var messageLocale = message.Locale;
56+
var country = string.Empty;
57+
var timezone = string.Empty;
58+
if (message.Entities != null && message.Entities.Count() > 0)
59+
{
60+
var entity = message.Entities.Where(x => x.Type == "clientInfo").FirstOrDefault();
61+
if (entity != null)
62+
{
63+
messageLocale = entity.Properties["locale"]?.ToString();
64+
country = entity.Properties["country"]?.ToString();
65+
timezone = entity.Properties["timezone"]?.ToString();
66+
}
67+
}
68+
if (string.IsNullOrEmpty(messageLocale))
69+
{
70+
var summary = message.From.Properties["summary"];
71+
if (summary != null)
72+
{
73+
messageLocale = summary.Value<string>("locale");
74+
}
75+
}
76+
77+
properties.Add("Locale", messageLocale);
78+
properties.Add("Country", country);
79+
properties.Add("Timezone", timezone);
80+
properties.Add("Speak", message.Speak);
81+
properties.Add("ChannelData", message.ChannelData != null ? Newtonsoft.Json.JsonConvert.SerializeObject(message.ChannelData) : string.Empty);
82+
properties.Add("ContainerId", GetValidCloudBlobContainerName(message.Conversation.Id));
83+
84+
bool isBotToUser = GetIsBotToUser(activity);
85+
86+
if (isBotToUser)
87+
{
88+
properties.Add("User ID", message.Recipient.Id);
89+
if (!string.IsNullOrEmpty(message.ReplyToId))
90+
{
91+
properties.Add("Reply to ID", message.ReplyToId);
92+
}
93+
telemerty.TrackEvent("Answer", properties);
94+
}
95+
else
96+
{
97+
properties.Add("User ID", message.From.Id);
98+
telemerty.TrackEvent("Question", properties);
99+
}
100+
}
101+
102+
if (activity.AsTraceActivity() != null)
103+
{
104+
var trace = activity.AsTraceActivity();
105+
106+
var properties = new Dictionary<string, string>
107+
{
108+
{ "Conversation ID", trace.Conversation.Id },
109+
{ "Channel ID", trace.ChannelId },
110+
{ "From ID", trace.From.Id},
111+
{ "From Name", trace.From.Name},
112+
{ "Recipient ID", trace.Recipient.Id},
113+
{ "Bot ID", CloudConfigurationManager.GetSetting("BotId")},
114+
{ "Label", trace.Label},
115+
{ "Conversation Reference", json},
116+
{ "Timestamp", trace.Timestamp.HasValue? trace.Timestamp.Value.ToString():string.Empty },
117+
{ "ReplyToId", trace.ReplyToId },
118+
{ "Entities", trace.Entities != null? Newtonsoft.Json.JsonConvert.SerializeObject(trace.Entities): string.Empty }
119+
};
120+
121+
if (trace.ValueType == LuisDialog<string>.LuisTraceType)
122+
{
123+
if (trace.Value is LuisTraceInfo traceInfo)
124+
{
125+
properties.Add("Luis Model ID", traceInfo.LuisModel.ModelID);
126+
properties.Add("Top Scoring Intent", traceInfo.LuisResult.TopScoringIntent.Intent);
127+
properties.Add("Top Scoring Intent Score", traceInfo.LuisResult.TopScoringIntent.Score?.ToString());
128+
properties.Add("Query", traceInfo.LuisResult.Query);
129+
}
130+
}
131+
properties.Add("ContainerId", GetValidCloudBlobContainerName(trace.Conversation.Id));
132+
133+
telemerty.TrackEvent("LuisTrace", properties);
134+
}
135+
136+
return Task.CompletedTask;
137+
}
138+
139+
private static bool GetIsBotToUser(IActivity activity)
140+
{
141+
return activity.From?.Id?.ToLower().Contains(CloudConfigurationManager.GetSetting("BotId").ToLower()) == true ||
142+
activity.From?.Name?.ToLower().Contains(CloudConfigurationManager.GetSetting("BotId").ToLower()) == true ||
143+
activity.From?.Role?.ToLower() == "bot" ||
144+
(activity.ChannelId == ChannelIds.Skype && activity.From?.Id?.ToLower() != activity.Conversation.Id?.ToLower()) ||
145+
(activity.ChannelId == ChannelIds.Emulator && activity.From?.Id != "default-user");
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)