Winning with Application Insights Logging
Tags: azure, coding, serverless
Published: 2021-07-29 10:48:53

---
canonical_url: https://www.serverless360.com/blog/application-insights-logging-to-monitor-business-application
---

## **Introduction**

This article will focus on using Azure Application Insights to monitor the real-world business apps. Azure Application Insights is an Application Performance Management (APM) service for developers and DevOps professionals. It is used to monitor an app that is live in real-time.

It supports a variety of platforms including .NET, Node.js, Java, and Python hosted on-premises, hybrid, or any public cloud. Application Insights can be used in two ways to monitor your Application. They are,

  * Codeless Monitoring
  * Code-based Monitoring

## **What Can Be Monitored?**

[Azure Application insight] (https://www.serverless360.com/compare-application-insights) is ideal for developers to monitor how an application works in real-time from performance to error logs. As an APM tool, it can monitor

  * Request rates, response times, and failure rates
  * Exceptions
  * Pageviews and load performance
  * Diagnostic trace logs
  * Custom events and metrics and many more

User can achieve all the above-mentioned monitoring goals with the help of tools in Application insights like Application Map, Live Metrics, Dashboards, etc.,

## **Codeless Monitoring**

In Codeless Monitoring, Users can get to monitor the Application without any modification to the code, provided the application is hosted in an Azure Service like Azure Web App or Azure Virtual Machine. These resources are natively integrated with the Application Insights that can be enabled from the Azure portal itself. Now let us see how to enable Azure Application insights for an [Azure Web App] (https://www.serverless360.com/azure-web-apps-monitoring-management)

## **How Do You Implement Application Insights in Web API?**

  * Go to your Azure Web App -> Application Insights
  * Click **Turn on Application insights.**

![] (https://www.serverless360.com/wp-content/uploads/2020/08/1-1.png)

  * You can either choose the existing Application Insights instance or create a new one by providing the Name and Location. And Click on **Apply** to turn on the Application Insights for this Web-App.

![] (https://www.serverless360.com/wp-content/uploads/2020/08/2-1.png)

The Web app will now be enabled with the Application Insights and the user can achieve monitoring goals like live monitoring and failure detection.

## **Code-based Monitoring**

Codeless Application monitoring will be very useful if the application is hosted in Azure Resources like VMs and App Services. Whereas, if the Application is hosted in any other Cloud Service provider or hosting service, then code-based monitoring will be the go-to option. With this monitoring solution, it is possible to monitor the applications or APIs built using

  * ASP.NET Core
  * ASP.NET Framework
  * JAVA
  * Python
  * Node.JS runtime

With the help of Application Insights Logging, it is also possible to monitor background services and Client-side JavaScript with the help of Application Insights. Now let us take a .NET Core application and see how to integrate Application Insights.

## **How Do I Enable Application Insights in Azure?**

  * Create an Azure Application Insights instance in Azure Portal. To create it, click on **Create a Resource** menu in the left Navigation and Type **Application Insights** in the Search box.

![] (https://www.serverless360.com/wp-content/uploads/2020/08/3-2.png)

  * Now, Give the Name and other details like Resource Group Name, etc., Click Create

  * Once the Application Insights instance is created, go to Properties in the left side navigation bar to get the Instrumentation key that will be required for our Application to establish the connection.

![] (https://www.serverless360.com/wp-content/uploads/2020/08/4.png)

  * Open your .NET Core Application and install the library **“ApplicationInsights.AspNetCore”** from NuGet Package.

  * Add **AddApplicationInsightsTelemetry();** to the **ConfigureServices()** method in your **Startup** class

<pre>// This method gets invoked by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // The following line enables Application Insights telemetry collection.
        services.AddApplicationInsightsTelemetry();

        // This code adds other services for your application.
        services.AddMvc();
    }
</pre>

  * To establish a connection between the Application and the Azure Application Insights, we need to provide the Instrumentation key in json as an Environment variable as below

<pre>{
      "ApplicationInsights": {
        "InstrumentationKey": "&lt; Application Insight Instrumenation – Key &gt;"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      }
    }
</pre>

  * Now, run the Application. Go to the Application Insights instance that was created in the Azure Portal. Go to Live Metrics in the Left Navigation bar.

 ![] (https://www.serverless360.com/wp-content/uploads/2020/08/5.png)

  * Live Monitoring is on, and you can infer the Application performance from the widgets on key performance indices.

  ![] (https://www.serverless360.com/wp-content/uploads/2020/08/6.png)

With the help of Live Monitoring, it is possible to monitor a variety of Metrics like Request rate, Request Failure rate, Outgoing requests, Failed Requests, and many more.

## **How Do I Log Exceptions in Application Insights?**

By configuring the Application as mentioned above, the user can monitor all the requests and logs sent to Azure Application insights. But when the need is to monitor only the logs which are in the Warning or Error state, it can be achieved by filtering the Log information based on Severity as mentioned below

To filter the logging data, the user must create a builder in the CreateWebHosting() method and add a filter as below code:

<pre>//Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;
      WebHost.CreateDefaultBuilder(args)
        .UseStartup<startup>()
      .ConfigureLogging(
            builder =&gt;
            {
                builder.AddApplicationInsights("ikey");        
        builder.AddFilter<microsoft.extensions.logging.applicationinsights.applicationinsightsloggerprovider>
                                 ("", LogLevel.Warning);
            }
        );
}

Once the filtering has been set, the user can log the necessary information from the Controller as below
//ValuesController.cs
public class ValuesController : ControllerBase
{
    private readonly ILogger _logger;

    public ValuesController(ILogger<valuescontroller> logger)
    {
        _logger = logger;
    }

    // GET api/values
    [HttpGet]
    public ActionResult<ienumerable<string>&gt; Get()
    {
        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" };
    }
} 
</string,></ienumerable<string></valuescontroller></microsoft.extensions.logging.applicationinsights.applicationinsightsloggerprovider></startup></pre>

It is also possible to log the data from our logging services by providing the necessary information. This Azure Application Insights Logging can be useful when the need is to monitor the performance of an Application.

## **How Do I See Exceptions in Application Insights?**

Open the Application Insights Search window in Visual Studio, and set it to display events from your app. While you’re debugging, you can do this just by clicking the Application Insights button. Notice that you can filter the report to show just exceptions.

Users can even use the application insights created in Azure Portal to view all the errors happening in their system.

## **How Do I View App Insight Logs?**

In the Azure portal, browse to the required resource group and select the required resource like Function App, Web App, etc.,

In the monitoring section, select the logs option and all the logs captured will be displayed.

But When the Application comprises a variety of Azure Serverless Resources and the need is to monitor the whole Application along with Azure resources, Application Insights Logging cannot be the right solution for this scenario.

<blockquote><em>Here comes the <a href="https://www.serverless360.com/business-activity-monitoring" rel="noopener noreferrer" target="_blank">Business activity monitoring</a> in Serverless360 to perform end to end tracking on Azure Serverless Applications.</em></blockquote>

## **Business Activity Monitoring**

Business Activity Monitoring in [Serverless360] (https://www.serverless360.com/) is a tool to perform end to end tracking of the Azure Serverless applications. Serverless360 BAM can be instrumented in your business application using the Exposed APIs also available as .Net SDK and Logic App Connector.  Implementing Business Activity Monitoring can provide visibility on the messages flowing through the components of the business application like never before.

## **Business Transactions**

Every [Transaction] (https://docs.serverless360.com/docs/bam-end-to-end-tracking) of an Application will be tracked in a separate section where the status of the transactions can be viewed along with the status of each stage in a transaction.

![] (https://www.serverless360.com/wp-content/uploads/2020/08/7.png)

It is also possible to view the data flowing through every stage along with the tracking of key business properties.

## **Analytics & Monitoring**

[Business Activity Monitoring] (https://www.serverless360.com/business-activity-monitoring) in Serverless360 also offers the capability to monitor the transactions when there is a failure or exception. Users will be notified through the configured Notification channels whenever there is an exception in any of the Business transaction. Calendric view in [Business process monitor] (https://docs.serverless360.com/docs/bam-monitoring) helps to understand transaction monitoring history.

![] (https://www.serverless360.com/wp-content/uploads/2020/08/8.png)

Consider when there is a need to analyse how the application is performing based on the custom property tracked. Serverless360 [Dashboard] (https://docs.serverless360.com/docs/bam-analytics) helps the user to create widgets and analyze business transactions in a graphical view. User can give the input as a BAM query and get the data as advanced graphical widgets as below

![] (https://www.serverless360.com/wp-content/uploads/2020/08/9.png)

## **Summary**

Application Insights along with Logging is the best tool for live monitoring of Applications much needed for developers, whereas it is too complex for an operations or support user. Their need would be a tool like [Serverless360] (https://www.serverless360.com/) to monitor and track the whole Serverless Application with better monitoring and management capabilities. To know more about Serverless360 on how it is better compared with other monitoring solutions offered by Azure, Follow the Links below

* [Azure Application Insights vs Serverless360] (https://www.serverless360.com/compare-application-insights)
* [Azure Monitor Vs Serverless360] (https://www.serverless360.com/azure-monitor-and-serverless360-comparison)
* [Service Bus explorer Vs Serveless360] (https://www.serverless360.com/compare-service-bus-explorer)