6

I have an ASP.NET Core web project deployed to Azure with Application Insights configured. Insights is receiving data fine for requests etc. but I am unable to get it to display my logs.

I am using the vanilla Microsoft.Extensions.Logging framework and have a test error being logged on a controller action as

logger.LogError("Test application insights message");

In my Startup.cs Configure method I have set

loggerFactory.AddAzureWebAppDiagnostics();

... and can successfully see the error message appear in Azure logs streaming:

2017-04-14 11:06:00.313 +00:00 [Error] Test application insights message

However, I also want this message to appear in Application Insights trace but it is nowhere to be found. I think I need to configure the ILoggerFactory but not sure how and can't find any docs on the subject.

Thanks in advance for your help

cijothomas
  • 2,818
  • 1
  • 13
  • 25
  • Did you configure the storage? I'm assuming that's what you're referring to? you want it saved in Azure Storage account? – pqsk Apr 26 '17 at 00:09
  • @pqsk - no, not storage account, in Application Insights trace which you get to on the Application Insights blade on the Azure portal under "Search" – Alasdair Thomson Apr 27 '17 at 04:33
  • could you add a screenshot of how you have it all setup on the portal and some code on how you set it up (DI I assume). I think that would help to analyzing this problem – pqsk Apr 27 '17 at 14:27

2 Answers2

10

case anybody else trying to resolve this, I got the results I wanted with the following:

loggerFactory.AddApplicationInsights(app.ApplicationServices);
  • I'm trying to do the same as what you did, but I still don't see the logs showing up in my application insights. Would you be able to share other parts of your StartUp.cs (and possibly Program.cs) code? – KangarooWest Jul 10 '17 at 23:40
  • Hi @WesternAussie, nothing special about my Program.cs... just `.UseApplicationInsights();` on the hostBuilder object. In Startup.cs the only relevant pieces are `loggerFactory.AddAzureWebAppDiagnostics(); // Ensures that it goes to azure logs for streaming` and `loggerFactory.AddApplicationInsights(app.ApplicationServices); // and in to Trace on insights` – Alasdair Thomson Jul 13 '17 at 14:48
  • Thanks for this. Just spent a couple of hours trying to figure out why my Informational logging was showing up in Application Insights when running in Dev, but not in Staging (not that I want it to all the time, but nice to know how to do it if needed later). I got to the same answer but via: https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Logging – robaker Mar 06 '18 at 17:35
  • 1
    **ASP.NET Core 2.0** You don't have to install the provider package or call the `AddAzureWebAppDiagnostics` extension method. The provider is automatically available to your app when you deploy the app to Azure App Service https://github.com/aspnet/Docs/issues/5016 – SerjG Apr 06 '18 at 17:33
  • 1
    AddApplicationInsights is obsolete – Dainius Kreivys Feb 20 '19 at 09:19
0

For me, It only worked when I specified the InstrumentationKey to services.AddLogging in my Startup.cs like this:

services.AddApplicationInsightsTelemetry();

services.AddLogging(builder =>
{
    // Only Application Insights is registered as a logger provider, get the key from appSettings.json file and add application insight
    var instrumentationKey = Configuration.GetValue<string>("ApplicationInsights:InstrumentationKey");
    builder.AddApplicationInsights(instrumentationKey);
});
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112