To write log entries to a log store, such as Application Insights, from an ASP.NET application, two things must be done:
- Log entries need to be emitted from application code, where applicable, using the ILogger interface. This interface is available from the Microsoft.Extensions.Logging.Abstractions NuGet package.
- The Application Insights NuGet package (Microsoft.ApplicationInsights.AspNetCore) needs to be installed and Application Insights needs to be registered as LoggingProvider. This way, all logs sent to the preceding interface are forwarded to the Application Insights code. In turn, this code forwards all the logs to the Application Insights service.
The following example code shows you how to use the ILogger interface ...