Loupe works with ASP.NET Core to provide a complete set of Telemetry:
First, add the Loupe Agent to your ASP.NET Application following the instructions in Using Loupe with ASP.NET Core. This ensures the ASP.NET Diagnostics are correctly set up.
Loupe provides a logging API you can invoke directly with a minimum of ceremony. It doesn't require dependency injection and handles scenarios like the logging system being disabled. To use it, just call one of the Log methods directly from your code, like this:
Log.Warning("Web Site.Customer", "Customer not found", "There is no customer in the system with Id {Id}", id);
ASP.NET Core provides a very good logging library out-of-the-box; you can find the full source for it at github.com/aspnet/Extensions. Unlike third-party logging systems, including Loupe’s own Log methods, the Microsoft.Extensions.Logging library is used extensively throughout the whole of ASP.NET Core and Entity Framework Core, as well as a lot of NuGet packages and other libraries, so you can get log messages from everywhere without needing to worry about Trace sources and listeners and things like that.
To write your own messages to the logs, you need an instance of ILogger or ILogger<T>. Usually you can get this via dependency injection (which is also supported out-of-the-box in ASP.NET Core). These interfaces provide familiar methods for logging at different levels, from Trace for the most verbose of logs, to Critical for only the most important messages.
Here is an example of using ILogger in an ASP.NET Core 2.1 API controller to record a warning message when a Customer is not found:
ILogger Example in Controller |
Copy Code
|
---|---|
[Route("customers")] public class CustomerController : Controller { private read only CustomerContext _context; private readonly ILogger<CustomerController> _logger; public CustomerController(CustomerContext context, ILogger<CustomerController> logger) { _context = context; _logger = logger; } [HttpGet("{id}")] public async Task<ActionResult<Customer>> Get(int id) { var customer = await _context.Customers.FindAsync(id); if (customer != null) { return customer; } _logger.LogWarning("Customer not found: {Id}", id); return NotFound(); } } |
If your ASP.NET Application uses authentication, you will want to take advantage of Loupe's ApplicationUser feature to provide additional information and humanize the users. This can significantly improve support and data analysis with Loupe Server. For an overview, see Developer's Guide - Capturing Application Users.
When Loupe is integrated with ASP.NET Core as shown above, it automatically adds a ClaimsPrincipalResolver to identify the current user (under the assumption the application is using the default ClaimsPricipal) and a very simple ClaimsPrincipalApplicationUserProvider to capture the user name. If you are adding any additional user information you will want to either implement an ApplicationUserProvider or provide a delegate to the AddAspNetCoreDiagnostics builder method to provide your extended application user information.