Add the the following NuGet packages to get Loupe and its relevant dependencies:
- Loupe.Agent.AspNetCore: The main Loupe extension for ASP.NET Core, which will also pull in the main Loupe agent itself.
- Loupe.Extensions.Logging: Connects Loupe with the Microsoft.Extensions.Logging infrastructure which is used by ASP.NET for common logging.
- Loupe.Agent.Core.Services: A small package with configuration builders for ASP.NET Core.
- Loupe.Agent.EntityFrameworkCore (Optional): Connects Loupe with EF Core to record performance and diagnostic information for EF Core.
- Loupe.Agent.PerformanceCounters (Optional): Records a collection of Windows Performance Counters on Windows systems.
The recommended way to activate the ASP.NET Core Agent is to add it to the Loupe Agent Builder during the Host setup, typically in Program.cs like this:
Host Builder Example for Program.cs |
Copy Code
|
---|---|
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .AddLoupe(builder => builder.AddAspNetCoreDiagnostics() .AddClientLogging() //The Loupe endpoint for client logging .AddEntityFrameworkCoreDiagnostics() //EF Core monitoring .AddPerformanceCounters()) //Windows Perf Counter monitoring .AddLoupeLogging(); |
The Loupe Agent for ASP.NET Core needs to access the HttpContext to monitor requests. This isn't always available by default in the ASP.NET Core Dependency Injection context, so we need to add it during the services collection in case it isn't pulled in by any other service. To do this, add the HttpContext Accessor at the top of the ConfigureServices call in the Startup class, like this:
HttpContext Accessor for Startup.cs |
Copy Code
|
---|---|
public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); //The rest of your services } |
Adding Client-side Logging
The Loupe Agent for ASP.NET Core can create a logging endpoint so browser applications can add messages into the Loupe log. In addition to adding the client side logging to your host builder (as done above) the endpoint has to be mapped and a cookie added to the request handling pipeline.
To add the Loupe cookie-handling middleware in the app pipeline call UseLoupeCookies in your Configure method of the startup class used for the web site, typically in Startup.cs.. This should go before any Authentication or Routing call but after a Static Files call, if present. For example:
Loupe Cookie Handling |
Copy Code
|
---|---|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // ... app.UseStaticFiles(); app.UseLoupeCookies(); // Add Loupe cookie-handling app.UseRouting(); app.UseAuthorization(); // ... } |
To complete adding the endpoint to receive log messages from the client call MapLoupeClientLogger method in the Configure method as well. For example:
Typical Configuration Method |
Copy Code
|
---|---|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); endpoints.MapLoupeClientLogger(); }); } |
.NET Core uses a different, more flexible, approach to application configuration than .NET Framework applications. Instead of XML config files .NET Core has an entire extensible configuration subsystem that can be used. In most situations you'll do a combination of coded configuration and JSON configuration. For details, see Developer's Guide - Agent Configuration.
Once you've instrumented an application you'll want to set up a connection with Loupe Server so you can collect data from everywhere your application runs and start managing errors. Add this configuration to your appsettings.json file to send data to this server automatically in the background. You'll need to make a few changes to this JSON to have it work:
- Replace "Your Loupe Service Name" with the name of your Loupe Cloud-hosted Service. If you don't have one yet, sign up for a free trial service.
- Replace "Your Product" and "Your Application" with friendly values for your application. A good analogy for Product is "Office" and Application is "Word", "Excel", and "PowerPoint".
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } }, "Loupe": { "Publisher": { "ProductName": "Your Product", "ApplicationName": "Your Application", "ApplicationType": "AspNet", "ApplicationVersionNumber": "1.0.1" }, "Server": { "UseGibraltarService": true, "CustomerName": "Your Loupe Service Name", "AutoSendSessions": true, "SendAllApplications": true } } }