Integrating Azure Application Insights

In this section, we'll add logging to your Azure Functions using Azure Application Insights. We'll need to receive vital information about what our software is doing, so that we can triage issues. And to ensure that it happens correctly, we'll need the Azure Functions created earlier. Let's begin by implementing the following steps:

  1. Create an Application Insights instance by going on to the portal and creating one from the marketplace. Use the .NET app setting. Note down the instrumentation key from the overview panel; you'll need it later.
  2. Install a NuGet package, Microsoft.ApplicationInsights.
  3. Add the line of code in the screenshot (the private static string with the application insights key in it) to PostTransactions.cs. Then, create a private static TelemetryClient, using that key for its InstrumentationKey property.
Refer to the complete code placed at: Code/Serverless-Architectures-with-Azure/Lesson
2/BeginningAzureServerlessArchitecture/PostTransactionsExCStep3.cs.

Go to https://goo.gl/AFwL27 to access the code.

For local development, it can be easier to just copy and paste your instrumentation key directly into the TelemetryClient. In production, you should use app settings, which would be controlled by a variable in your
release process:

  1. Import MicrosoftExtensions.Logging. Change the TraceWriter to an ILogger.
  1. Change all of the previous log methods to LogInformation:
  1. Wrap your JsonConvert.DeserializeObject statement in a try-catch block. In the catch block, log that there has been an error with log LogError() . Next, track the event of a bad request with telemetry.TrackEvent() and track the exception using telemetry.TrackException(e). Finally, return a BadRequest HTTP response.
  1. Add a null check on the object inside the try block—this is in case Newtonsoft.Json fails quietly. Throw an exception if the object is null. You could also check individual properties and throw exceptions for them:

  1. Deploy to Azure, go to the app settings of the Function App, and add the setting APPINSIGHTS_INSTRUMENTATIONKEY; with the key you got earlier.
The Application Insights Key identifies your individual instance of Application Insights. This essentially just identifies your account to the underlying shared Application Insights architecture and ensures you only see your apps logs and no-one else's.

Link for the code: https://raw.githubusercontent.com/TrainingByPackt/Serverless-Architectures-with-Azure/master/Lesson%202/BeginningAzureServerlessArchitecture/PostTransactionsExCStep6.cs.

Outcome

Azure Functions submitted logs to Azure Application Insights. Now that you are successfully submitting logs to Application Insights, it's time to see what insights we can get from this and how we might action them. Hopefully, you can see that when incorrectly formatted JSON payloads are submitted to the function, we will be tracking that exception, so we will have a look at how to observe those logs. If you can imagine the situation of having an Application Insights dashboard on a screen in your office and seeing a series of malformed payload exceptions appear, the following steps would allow you to study the cause of that error and troubleshoot it.

..................Content has been hidden....................

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