none
Adding Application Insights Logging to an Azure Web Service crashes application RRS feed

  • Question

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



    Thursday, April 4, 2019 1:02 AM

All replies

  • 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.

     

     


     


    Sunday, April 7, 2019 12:55 AM
    Owner