在机器人中引发异常时没有日志

时间:2019-08-19 18:55:42

标签: azure botframework azure-web-sites azure-application-insights azure-bot-service

我们有一个部署到Azure的ASP.Net Core v4机器人。在Azure中使用测试功能时,它工作正常。然后我们部署到MS Teams。它的工作原理是找到,除了每条消息后都跟着另一条消息“对不起,看起来好像出了点问题”。该消息通常在引发异常时发送。我试图去Azure来查看日志,但它没有记录任何内容。

我们的代码中确实包含logger.LogError($"Exception caught : {exception.Message}");,我认为它将在生产中记录到某个地方。因此,我为该机器人打开了Application Insights,但是它没有记录任何异常。我尝试从Web服务器流式传输日志,但引发异常时它不记录任何内容。

我尝试查看“应用程序日志”和“ Web服务器日志”中的应用程序日志

以下是处理错误的代码:

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}");
            }
        }
    };
}

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

enter image description here

1 个答案:

答案 0 :(得分:1)

@JohnGardner是正确的。 Botframework catches all errors,因此您不会在典型的Azure App Service日志中可能看不到它们。

@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


您实际上应该能够在日志流>应用程序日志中看到此信息

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

enter image description here

在测试中:

enter image description here

在输出中:

enter image description here

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

enter image description here


我们当前建议的方法是使用Application Insights。您可以使用Sample 21.Corebot-App-Insights作为指导。主要修改是如何在其Startup.cs中添加App Insights:

完整

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

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