- Basics about Application Insights
- Connecting to Application Insights using instrumentation key
- Analyze Application Insights data in the Azure Portal and Visual Studio
-
Open Azure Portal and sign in.
-
Add Application Insights named
PracticalDevOps-Dev
to the resource groupPracticalDevOps-Dev
.
-
Add the
InstrumentationKey
setting to yourweb.config
file.<?xml version="1.0" encoding="utf-8"?> ... <configuration> <appSettings> <add key="MinimumNumberOfBooks" value="1"/> <add key="MaximumNumberOfBooks" value="5"/> <add key="BookNameTokenUrl" value="..."/> <add key="InstrumentationKey" value="ba2c0764-5cfb-4741-a8e1-fb150b175a7d"/> </appSettings> ... </configuration>
-
Add the
Microsoft.ApplicationInsights.Web
NuGet package to the Books project -
Add code setting the instrumentation key to
Startup.cs
:public void Configuration(IAppBuilder app) { TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["InstrumentationKey"]; // Allow CORS app.UseCors(CorsOptions.AllowAll); ... }
-
Add custom event tracking to
Controllers/BooksController.cs
:[HttpGet] [Route("books")] public async Task<IEnumerable<Book>> Get() { var numberOfBooks = new Random().Next(this.options.MinimumNumberOfBooks, this.options.MaximumNumberOfBooks + 1); var telemetryClient = new TelemetryClient(); telemetryClient.TrackEvent($"Generating {numberOfBooks} books"); var result = new Book[numberOfBooks]; ... }
[HttpPost] [Route("books")] public IHttpActionResult Post(Book newBook) { var telemetryClient = new TelemetryClient(); telemetryClient.TrackEvent($"Trying to add a book"); // For demo purposes, return an HTTP 500 error (used to demonstrate logging) return this.InternalServerError(); }
-
Copy Request tracking middleware implementation from Assets/Exercise-4-Telemetry that logs information about all requests to Application Insights.
-
Configure OWIN to use the request tracking middleware by updating
Configuration
method inStartup.cs
:public void Configuration(IAppBuilder app) { TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["InstrumentationKey"]; app.UseApplicationInsights(); // Allow CORS app.UseCors(CorsOptions.AllowAll); ... }
-
Run all your tests to make sure you did not break something.
-
Run application locally and refresh
http://localhost:2690/api/books
multiple times. -
See if your application telemetry appears.
-
Open Application Insights Search in Visual Studio while debugging your application. Refresh
http://localhost:2690/api/books
multiple times. See if your application telemetry appears.
If you have time left, you could additionally cover topics like:
- Describe how to link Application Insights with wide-spread logging frameworks like NLog (Microsoft.ApplicationInsights.NLogTarget NuGet package)
- Demonstrate PowerBI and Application Insights
- Show Application Insights Analytics