-
Notifications
You must be signed in to change notification settings - Fork 815
/
Copy pathAdapterWithErrorHandler.cs
72 lines (67 loc) · 3.51 KB
/
AdapterWithErrorHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Net.Http;
namespace PeoplePicker
{
/// <summary>
/// Custom Bot Framework adapter with error handling to log unhandled exceptions and send trace activities.
/// </summary>
public class AdapterWithErrorHandler : CloudAdapter
{
/// <summary>
/// Initializes a new instance of the <see cref="AdapterWithErrorHandler"/> class.
/// </summary>
/// <param name="configuration">Configuration for the bot environment.</param>
/// <param name="httpClientFactory">Factory for creating HTTP clients.</param>
/// <param name="logger">Logger for logging errors and events.</param>
/// <param name="conversationState">Optional conversation state for handling persistent conversations.</param>
public AdapterWithErrorHandler(IConfiguration configuration, IHttpClientFactory httpClientFactory, ILogger<IBotFrameworkHttpAdapter> logger, ConversationState conversationState = default)
: base(configuration, httpClientFactory, logger)
{
// Set up custom error handling for the bot.
OnTurnError = async (turnContext, exception) =>
{
// Log any unhandled exceptions during the turn.
// In production, consider logging this to Application Insights or other telemetry services.
// Azure Application Insights. Visit https://aka.ms/bottelemetry to see how
// to add telemetry capture to your bot.
logger.LogError(exception, $"[OnTurnError] Unhandled error: {exception.Message}");
// Uncomment below commented line for local debugging..
// await turnContext.SendActivityAsync($"Sorry, it looks like something went wrong. Exception Caught: {exception.Message}");
// Send a trace activity to the Bot Framework Emulator (if in use)
await SendTraceActivityAsync(turnContext, exception);
};
}
/// <summary>
/// Sends a trace activity when an error occurs, particularly useful in the Bot Framework Emulator.
/// </summary>
/// <param name="turnContext">The current turn context.</param>
/// <param name="exception">The exception that was thrown during the bot turn.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private static async Task SendTraceActivityAsync(ITurnContext turnContext, Exception exception)
{
// Only send a trace activity if we're talking to the Bot Framework Emulator
if (turnContext.Activity.ChannelId == Channels.Emulator)
{
// Create a trace activity to capture the error
Activity traceActivity = new Activity(ActivityTypes.Trace)
{
Label = "TurnError",
Name = "OnTurnError Trace",
Value = exception.Message,
ValueType = "https://www.botframework.com/schemas/error",
};
// Send the trace activity to the bot framework emulator
await turnContext.SendActivityAsync(traceActivity);
}
}
}
}