Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Core / Agent Configuration / Developer's Reference - Agent Configuration through Configuration Builder
In This Topic
    Developer's Reference - Agent Configuration through Configuration Builder
    In This Topic

    The .NET Core team came up with a new, more flexible way of handling configuration and options, allowing you to load settings from JSON, XML or even INI files, as well as environment variables and command line arguments. The Microsoft.Extensions.Configuration system is fully extensible as well, so you can grab other configuration packages from NuGet to support sources like Azure Key Vault or Kubernetes Secrets.

    Loupe supports this new configuration system and provides several helper types for working with ConfigurationBuilder.  To use this in your project, you’ll need references to the various Microsoft.Extensions.Configuration packages. If it’s an ASP.NET Core application you’ll already have them, but for a regular console application or service you’ll need to add package references for Microsoft.Extensions.Configuration.Json and Microsoft.Extensions.Configuration.Binder.

    You can also perform all configuration in your code without using a configuration file, see Developer's Reference - Agent Configuration through Code for details.

    Configuration Sections

    There are seven configuration areas supported by the agent.  These options can be changed in the application configuration file without recompiling.  Click on the Section name to see details of all the options in that section.

    Section Description
    listener Adjusts the configuration of the Loupe .NET Listener that captures information directly from the .NET runtime including system and network events.
    networkViewer Configures live sessions.
    packager Sets options to customize the end-user behavior of the packager and provide the best user experience.
    publisher Adjusts how the central publisher functions.  The publisher sits between all log messages and other information collected by the agent and the messengers that can display or record the information.
    properties Custom properties (name/value pairs) to be recorded with each session and displayed in the Loupe Desktop.
    server Configures a Loupe Server connection.
    sessionFile Adjusts how session files are managed.  The session file is the persistent on-disk record of the session.  If disabled, no session file will be recorded which means that there will be no data to package or access with the Loupe Desktop or Packager.

    Enabling Application Configuration

    To use the configuration builder you need to add at least one configuration source from NuGet, typically Microsoft.Extensions.Configuration.Json, and the configuration binder (Microsoft.Extensions.Configuration.Binder).  If you're developing an ASP.NET application this has been anticipated and these references already exist.

    Adding Loupe to ASP.NET Core

    To make it even easier to add and configure Loupe for ASP.NET Core some convenience methods are defined in the Loupe Agent.  These methods get invoked during the ConfigureServices method to add your own custom configuration.  Loupe includes an AddLoupe method for the services and for the configuration builder.

    ASP.NET Core Configure Services Example
    Copy Code
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLoupe() //Add Loupe Logging
            .AddAspNetCoreDiagnostics() //Add Loupe ASP.NET Core Monitoring
            .AddEntityFrameworkCoreDiagnostics(); //Add Loupe EF Core Monitoring
        
        services.AddLogging(builder =>
        {
            builder.AddLoupe();
        });
        
        services.AddMvc();
    }
    

    Adding Loupe to .NET Core Console Applications

    For console applications you'll need to do more work yourself.  Here's a code example with a LoadConfig method to fully load and merge the configuration from the relevant sources

    Console Application Configuration Binding Example
    Copy Code
    class ConfigBuilderProgram
    {
        static void Main(string[] args)
        {
            Log.StartSession(LoadConfig());
            try
            {
                //Here is the body of your console application
            }
            catch (Exception ex)
            {
                Log.RecordException(ex, "Main", false);
                Log.EndSession(SessionStatus.Crashed, "Exiting due to unhandled exception");
            }
            finally
            {
                Log.EndSession(SessionStatus.Normal, "Exiting test application");
            }
        }
        private static AgentConfiguration LoadConfig()
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();
            var agentConfig = new AgentConfiguration();
            config.GetSection("Loupe").Bind(agentConfig);
            return agentConfig;
        }
    }
    
    Example Minimal appsettings.json file
    Copy Code
    {
      "Loupe": {
        "Publisher": {
          "ProductName": "Loupe",
          "ApplicationName": "Agent Test",
          "ApplicationType": "console",
          "ApplicationVersion": "2.1.0.0",
          "ApplicationDescription": "Console test application for .NET Core"
        },
        "Server": {
          "UseGibraltarService": true,
          "Customer": "Your_Customer_Name"
        }
      }
    }
    
    See Also