机器人抛出异常时没有日志

No logs when exception is thrown in bot

我们有一个 ASP.Net Core v4 机器人部署到 Azure。在 Azure 中使用测试功能时它工作正常。然后我们部署到 MS Teams。它可以找到,除了每条消息后跟另一条消息说 "Sorry, it looks like something went wrong." 该消息通常在抛出异常时发送。我尝试去 Azure 查看日志,但它没有记录任何内容。

我们的代码中确实有 logger.LogError($"Exception caught : {exception.Message}");,我认为它会在生产时将其记录在某处。所以我为机器人打开了 Application Insights,但它没有记录任何异常。我尝试从 Web 服务器流式传输日志,但在抛出异常时它不会记录任何内容。

我尝试从 "Application logs" 和 "Web server logs"

查看应用程序日志

这是处理错误的代码:

public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger, ConversationState conversationState = null)
    : base(configuration, logger)
{
    OnTurnError = async (turnContext, exception) =>
    {
        // Log any leaked exception from the application.
        logger.LogError($"Exception caught : {exception.Message}");

        // Send a catch-all apology to the user.
        var errorMessage = MessageFactory.Text(ErrorMsgText, ErrorMsgText, InputHints.ExpectingInput);
        await turnContext.SendActivityAsync(errorMessage);

        if (conversationState != null)
        {
            try
            {
                // Delete the conversationState for the current conversation to prevent the
                // bot from getting stuck in a error-loop caused by being in a bad state.
                // ConversationState should be thought of as similar to "cookie-state" in a Web pages.
                await conversationState.DeleteAsync(turnContext);
            }
            catch (Exception e)
            {
                logger.LogError($"Exception caught on attempting to Delete ConversationState : {e.Message}");
            }
        }
    };
}

这里是我们的机器人的应用程序服务的日志设置:

@JohnGardner 是正确的。 Botframework catches all errors,因此您 不会 可能不会在典型的 Azure 应用服务日志记录中看到它们。

@VinodkumarG 也是正确的,您可以在

中看到 logger.LogError($"Exception caught : {exception.Message}"); 生成的错误

Bot Management >> Build >>Open online code editor >> Output window

https://<yourEndpoint>.scm.azurewebsites.net/dev/wwwroot/:vs.output


您实际上应该能够在日志流 > 应用程序日志中看到这个

我将此添加到我的机器人中:

测试中:

在输出中:

在日志流 > 应用程序日志中:


我们目前推荐的方法是使用 Application Insights。您可以使用 Sample 21.Corebot-App-Insights as a guide. The primary modifications are how it adds App Insights in its Startup.cs:

完整

using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.ApplicationInsights;
using Microsoft.Bot.Builder.Integration.ApplicationInsights.Core;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.BotBuilderSamples.Bots;
using Microsoft.BotBuilderSamples.Dialogs;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.BotBuilderSamples
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // Create the credential provider to be used with the Bot Framework Adapter.
            services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();

            // Add Application Insights services into service collection
            services.AddApplicationInsightsTelemetry();

            // Create the telemetry client.
            services.AddSingleton<IBotTelemetryClient, BotTelemetryClient>();

            // Add ASP middleware to store the http body mapped with bot activity key in the httpcontext.items. This will be picked by the TelemetryBotIdInitializer
            services.AddTransient<TelemetrySaveBodyASPMiddleware>();

            // Add telemetry initializer that will set the correlation context for all telemetry items.
            services.AddSingleton<ITelemetryInitializer, OperationCorrelationTelemetryInitializer>();

            // Add telemetry initializer that sets the user ID and session ID (in addition to other bot-specific properties such as activity ID)
            services.AddSingleton<ITelemetryInitializer, TelemetryBotIdInitializer>();

            // Create the telemetry middleware to track conversation events
            services.AddSingleton<IMiddleware, TelemetryLoggerMiddleware>();

            // Create the Bot Framework Adapter with error handling enabled.
            services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

            // Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
            services.AddSingleton<IStorage, MemoryStorage>();

            // Create the User state. (Used in this bot's Dialog implementation.)
            services.AddSingleton<UserState>();

            // Create the Conversation state. (Used by the Dialog system itself.)
            services.AddSingleton<ConversationState>();

            // The Dialog that will be run by the bot.
            services.AddSingleton<MainDialog>();

            // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
            services.AddTransient<IBot, DialogAndWelcomeBot<MainDialog>>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseDefaultFiles();
            app.UseStaticFiles();

            //app.UseHttpsRedirection();
            app.UseBotApplicationInsights();
            app.UseMvc();
        }
    }
}

Diff vs CoreBot

您可能还会发现此博客 post 有用:Bot Analytics: Behind the Scenes