AppInsights and logging
Using the build-in Microsoft logger
For our web applications, we use Application Insights for log messages and metrics. We also want to use Azure Log Stream and Live Metrics. Finding relevant and up to date documentation on how to get this setup is hard. It has a tendency to change a little from one major version of ASP.NET Core to another.
Since our team is not the only one who has struggled with this, I decided to document how I got it to work for ASP.NET Core 3.1.
In this post, I’ll show how you can set it up using the build-in Microsoft logger. In the next post, I’ll show you how you can set it up using Serilog
What we are going to do
I assume that you already have setup an Application Insights resource
- Create a new ASP.NET Core Web Application
- Publish it to Azure to make sure that it works
- Hook up Application Insights using the instrumentation key
- Verify that log message gets to Application Insights and that we get Live Metrics
- Setup Azure Log Stream
Create new ASP.NET Core Web application
Open Visual Studio 2019 (I’m using version 16.4.5)
Create a new ASP.NET Core Web Application. Choose ASP.NET Core 3.1 and Web Application (MVC).
Remove the appSettings.Development.json file (not necessary, but if you want to do some local debugging you don’t have to change settings in two files)
In the home controller, create a log message using the logger:
public IActionResult Index() { _logger.LogInformation("*** MSLOG *** We just called the Index action"); return View(); }
Publish
Publish your application to Azure and check that it works:
Add Application Insights
Install the Microsoft.ApplicationInsights.AspNetCore package. Current version is 2.13.1.
To get telemetry data, add AddApplicationInsightsTelemetry in Startup.cs. Use the instrumentation key from Application Insights:
public void ConfigureServices(IServiceCollection services) { services.AddApplicationInsightsTelemetry("9****015-****-****-a9c1-c669****fca3"); services.AddControllersWithViews(); }
To get your log messages in Application Insights, add an ApplicationInsights provider and your instrumentation key. This is done in Program.cs:
Host.CreateDefaultBuilder(args) .ConfigureLogging((context, builder) => { // Providing an instrumentation key here is required if you're 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("9****015-****-****-a9c1-c669****fca3", options => { options.FlushOnDispose = true; }); // Optional: Apply filters to control what logs are sent to Application Insights. // The following configures LogLevel Information or above to be sent to // Application Insights for all categories. builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider> ("", LogLevel.Information); } ) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
Verify Live Metrics and log entries
Publish the changes to Azure.
Go to Live Metrics in Azure. Click Home a few times to see that you get metrics:
Open Application Insights and do a Search. Check that you have entries:
Wait until you see our log message “*** MSLOG *** We just called the Index action”. This can take several minutes. Be patient!
Azure Log Stream
You can already have a look at Azure Log Stream. It will show you all the messages that are being sent to the application logs in the file system. The Log Stream is really just a “shortcut” so that you don’t have to go through the process of locating and opening these files manually.
Before you can see anything, you have to turn on Application Service Logs:
then go to the Log Stream blade:
Notice that you can’t see the log message from the Home controller. To be able to see the messages created by the logger, you have to add the AzureWebAppDiagnostics provider that is found in the Microsoft.Extensions.Logging.AzureAppServices Nuget. Add this Nuget to your program, and add the following lines in Program.cs (ConfigureLogging):
... builder.AddAzureWebAppDiagnostics(); builder.AddFilter<Microsoft.Extensions.Logging.AzureAppServices.FileLoggerProvider> ("", LogLevel.Information); ...
You should now be able to see the log message from the Home controller in the Log Stream:
0 Comments on “AppInsights and logging”