- Notifications
You must be signed in to change notification settings - Fork 843
/
Copy pathAdapterWithErrorHandler.cs
41 lines (37 loc) · 1.8 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Generated with Bot Builder V4 SDK Template for Visual Studio CoreBot v4.6.2
usingMicrosoft.Bot.Builder.Integration.AspNet.Core;
usingMicrosoft.Bot.Builder.TraceExtensions;
usingMicrosoft.Extensions.Configuration;
usingMicrosoft.Extensions.Logging;
usingSystem.Threading.Tasks;
namespaceMicrosoft.Teams.Samples.HelloWorld.Web
{
publicclassAdapterWithErrorHandler:BotFrameworkHttpAdapter
{
publicAdapterWithErrorHandler(IConfigurationconfiguration,ILogger<BotFrameworkHttpAdapter>logger)
:base(configuration,logger)
{
OnTurnError=async(turnContext,exception)=>
{
// Log the error with additional context for better traceability.
logger.LogError(exception,$"[OnTurnError] unhandled error : {exception.Message}");
// Optionally, send a user-friendly message to the user.
// Uncomment the line below for local debugging.
// await turnContext.SendActivityAsync($"Sorry, it looks like something went wrong. Exception: {exception.Message}");
// Attempt to send a trace activity that will be displayed in the Bot Framework Emulator.
try
{
awaitturnContext.TraceActivityAsync("OnTurnError Trace",exception.Message,"https://www.botframework.com/schemas/error","TurnError");
}
catch(System.ExceptiontraceEx)
{
// Log any error that might occur during the trace activity (e.g., failed to send trace).
logger.LogError(traceEx,"Error occurred while sending trace activity.");
}
};
}
}
}