添加应用程序洞察日志记录到Azure Web服务会导致应用程序崩溃 [英] Adding Application Insights Logging to an Azure Web Service crashes application

查看:93
本文介绍了添加应用程序洞察日志记录到Azure Web服务会导致应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将我的Web服务加载到Azure Web服务中。 因此,我从模板中获取了一个新的Web API项目,对其进行了编译,然后将其发布到Azure Web服务。它按预期工作。



然后我拿了代码并按照参考用于排除503错误



希望以上信息有所帮助。如果您仍然看到问题,请退回。


 


 






 




I was having trouble getting my Web Service to load into an Azure Web Service.  So I took a fresh Web API project from the templates, compiled it, then published it to an Azure Web Service. It worked as expected.

Then I took the code and followed the instructions on this url.  Ran it locally.  It worked as expected writing simple logging messages to Application Insights.
Then I deployed this application and got a 503 Server Crash.  I'm afraid there's nothing in the logs to report, it appears to crash even before starting.

namespace WebApplication
{
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;

    public class Program
    {
        public static void Main(string[] args)
        {
            IWebHost webHost = CreateWebHostBuilder(args).Build();

            var logger = webHost.Services.GetRequiredService<ILogger<Program>>();
            logger.LogInformation("Starting Web Service.");
            logger.LogInformation("Information Message.");
            logger.LogDebug("Debug Message.");

            webHost.Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
                .ConfigureLogging((webHostBuilderContext, loggingBuilder) =>
                {
                    var appInsightKey = webHostBuilderContext.Configuration["ApplicationInsights:InstrumentationKey"];
                    loggingBuilder.AddApplicationInsights(appInsightKey);
                });
    }
}

解决方案

Hi GammaFour,

Following through the document with below code snippets for a Web API was able to register the traces in to Application Insights both locally and also when hosted to Azure as Web App service, unfortunately was not able to replicate the same behavior what you are having.

1. Install Microsoft.Extensions.Logging.ApplicationInsights NuGet package or update the package reference accordingly.

2. Updating Program.cs as below (exclude the filters while sending to Application Insights for now until its tested) 

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging(
            builder =>
            {
                // Providing an instrumentation key here is required if you are using
                // standalone package Microsoft.Extensions.Logging.ApplicationInsights
                // or if you want to capture logs from early in the application startup
                // pipeline from Startup.cs or Program.cs itself.
                builder.AddApplicationInsights("ikey");
            }
        );
}

3. Updating the below in the controller class

public class ValuesController : ControllerBase
{
    private readonly ILogger _logger;

    public ValuesController(ILogger<ValuesController> logger)
    {
        _logger = logger;
    }

    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        // All the following logs will be picked up by Application Insights.
        // and all of them will have ("MyKey", "MyValue") in Properties.
        using (_logger.BeginScope(new Dictionary<string, object> { { "MyKey", "MyValue" } }))
            {
                _logger.LogWarning("An example of a Warning trace..");
                _logger.LogError("An example of an Error level message");
            }
        return new string[] { "value1", "value2" };
    }
}

4.  Was able to send data to Application Insights locally

5. Published to Azure App Service and was able to browse the site and see the data in Application Insights.

  • Given that you are seeing 503 server crash, can you please validate if the publish to Azure App service was successful, please validate the files using the Advanced Tools  blade.
  • Within Kudu Got to Debug Console à PowerShell/CMD à site à wwwroot and validate if all the application files have successfully uploaded.
  • You can also use the Diagnose and solve problems blade to further validate if you see any error while publishing the app service
  • You can further enable the Application, Web Server logging and Failed request tracing using the Diagnostics blade
  • Once above logging is enabled , you can use the "Log stream" blade to further validate real time on what’s happening with the App service.
  • Also see if you can create a new Web App/AI instance and try to deploy and see if the same behavior is reoccurring, just to isolate the issue. 

Additional documentation reference for troubleshooting 503 errors

Hope the above information helps. Please revert back if you are still seeing the issue.

 

 


 



这篇关于添加应用程序洞察日志记录到Azure Web服务会导致应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆