Enabling diagnostics with Application Insights

Application Insights is an analytics platform that monitors the use of applications and provides insights on their performance and behavior. Application Insights has rich diagnostics capabilities and can be used to query logs and metrics that are sent to it. Application Insights is hosted in Microsoft Azure, but allows you to work with it locally and use its features from inside Visual Studio.

To use Application Insights in your application, follow these steps:

  1. Right-click the project in the Solution Explorer.
  2. Click on Add | Application Insights Telemetry...:

  1. In the Application Insights Configuration window that was opened, click on the Get Started button:

  1. At this point, we will use Application Insights locally. Scroll down to the bottom and click on the link to set Application Insights to local mode:

After completing these steps, you should see that the Application Insights packages were added to your project references, and the addition of Application Insights in the creation of the WebHostBuilder in the Program.cs file:

public static IWebHostBuilder CreateWebHostBuiler(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>();
To configure the log level for Application Insights, you can use the following snippet in the appsettings.json file:
"Application Insights": {
    "LogLevel": {
        "Default": "Debug"
    }
}
  1. Add the Application Insights logger to the ASP.NET Logger Factory by modifying your Startup.Configure() method, like this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
ILoggerFactory loggerFactory)
{
var appInsightsLogLevel =
Configuration.GetValue<LogLevel>("Logging:Application Insights:LogLevel:Default", );
loggerFactory.AddApplicationInsights(app.ApplicationServices,appInsightsLogLevel);

// Rest of code
}

At the time of writing this block, the Application Insights logger doesn't reflect the log level in the appsettings.json file automatically and instead uses a default level of Warning. The preceding code overcomes this limitation by reading the log level from the configuration property. Note that if the log level doesn't exist in the configuration file, the default log level will be Trace.

To configure the log level for Application Insights, you can use the following snippet in the appsettings.json file:
"Application Insights": {
    "LogLevel": {
        "Default": "Debug"
    }
}
  1. Run the application and send some requests to it, and you should see the Application Insights button in the Visual Studio toolbar:

  1. Clicking on this button will open the Application Insights Search window, which will display all the log events in the current debug session:

Through this window, you can search and filter log events, and drill down to specific events to see all the data arguments that were sent with each event.

In Chapter 14, Moving Your Solution to the Cloud, you will create the Application Insights service on Azure and connect your application to it so that you can run the same search and analytics in your production environment.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.115