Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Core / Developer's Guide - .NET Core Generic Host
In This Topic
    Developer's Guide - .NET Core Generic Host
    In This Topic

    .NET Core 3.0 introduced the new Generic Host - HostBuilder.  It brings the same setup and startup flexibility ASP.NET Core 2.0 introduced to console applications, services, and the rest of the .NET Core ecosystem.  Loupe provides the same extension methods for HostBuilder as it does for ASP.NET core, simplifying code patterns.

    For example, here's a simple way to set up the runtime environment and launch a console application using HostBuiler:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .AddLoupe()
                .AddLoupeLogging()
                .ConfigureServices((hostContext, services) =>
                {
                   services.AddHostedService<Worker>();
                });
    }
    

    By using AddLoupe it will setup the Loupe agent with the default configuration which will look to appsettings.json for its configuration (and Environment Variables for overrides). The example also adds the Loupe provider for Microsoft.Extensions.Logging with AddLoupeLogging.

    Adding Loupe to your ASP.NET Application

    Add the the following NuGet packages to get Loupe and its relevant dependencies:

    1. Loupe.Agent.Core: The main Loupe Agent for .NET Core.
    2. Loupe.Extensions.Logging: Connects Loupe with the Microsoft.Extensions.Logging infrastructure which is used by ASP.NET for common logging.
    3. Loupe.Agent.Core.Services: A small package with configuration builders for ASP.NET Core.

    Configuring Loupe

    .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:

    {
      "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
        }
      }
    }
    

    You can combine configuration sources by overriding values using Environment Variables.  For example, you may want to set the environment and promotion level at runtime by setting the following variables:

    Environment Variable Meaning Example
    Loupe__Publisher__EnvironmentName The place where the application is running, without quotes Azure-USEast2
    Loupe__Publisher__PromotionLevelName The promotion level the application is running as, without quotes Production

    Note the pattern is two underscores (__) between each level of the configuration hierarchy, starting with Loupe. Values are not case sensitive.

    See Also