Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Core & .NET 5 / Agent Configuration / Developer's Guide - Agent Configuration through Configuration Builder
In This Topic
    Developer's Guide - 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 Guide - 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.

    For examples of typical appsettings.json files, see Developer's Guide - Agent Configuration Common Scenarios.

    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.

    With HostBuilder Applications

    Most .NET Core 3.0 and later applications use HostBuilder to configure and initialize the application.  The default HostBuilder set up (the call to CreateDefaultBuilder) will add environment variable, command line, and appsettings.json support configuration support.  The Loupe Agent can then be activated and will load the configuration, like this:

    Host Builder Example for Program.cs
    Copy Code
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .AddLoupe()
                .AddLoupeLogging();
    

    With Console Applications

    For scenarios where the HostBuilder pattern isn't being used 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;
        }
    }
    
    See Also