< Summary

Information
Class: NexusLabs.Needlr.Hosting.CreateHostOptionsExtensions
Assembly: NexusLabs.Needlr.Hosting
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Hosting/CreateHostOptionsExtensions.cs
Line coverage
100%
Covered lines: 108
Uncovered lines: 0
Coverable lines: 108
Total lines: 329
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Hosting/CreateHostOptionsExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.Hosting;
 3using Microsoft.Extensions.Logging;
 4
 5namespace NexusLabs.Needlr.Hosting;
 6
 7/// <summary>
 8/// Provides extension methods for configuring <see cref="CreateHostOptions"/>.
 9/// </summary>
 10public static class CreateHostOptionsExtensions
 11{
 12    /// <summary>
 13    /// Configures the options to use a console logger for startup logging.
 14    /// </summary>
 15    /// <param name="options">The options to configure.</param>
 16    /// <param name="name">The name of the logger. Defaults to "Startup".</param>
 17    /// <param name="level">The minimum log level. Defaults to <see cref="LogLevel.Debug"/>.</param>
 18    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the console logger configured.</returns>
 19    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> is null.</exception>
 20    /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is null or whitespace.</exception>
 21    public static CreateHostOptions UsingStartupConsoleLogger(
 22        this CreateHostOptions options,
 23        string name = "Startup",
 24        LogLevel level = LogLevel.Debug)
 25    {
 126        ArgumentNullException.ThrowIfNull(options);
 127        ArgumentException.ThrowIfNullOrWhiteSpace(name);
 28
 129        using var loggerFactory = LoggerFactory
 230            .Create(builder => builder
 231            .AddConsole()
 232            .SetMinimumLevel(level));
 133        var logger = loggerFactory.CreateLogger(name);
 34
 135        var newOptions = options with
 136        {
 137            Logger = logger
 138        };
 39
 140        return newOptions;
 141    }
 42
 43    /// <summary>
 44    /// Configures the options to use the specified logger for startup diagnostics.
 45    /// Use this when you already have an <see cref="ILogger"/> instance and want to
 46    /// pass it directly instead of having Needlr create a console logger internally.
 47    /// </summary>
 48    /// <param name="options">The options to configure.</param>
 49    /// <param name="logger">The logger to use during startup.</param>
 50    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the logger configured.</returns>
 51    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="logger"/> is n
 52    /// <example>
 53    /// <code>
 54    /// var host = new Syringe()
 55    ///     .ForHost()
 56    ///     .UsingOptions(() => CreateHostOptions.Default
 57    ///         .UsingLogger(myLogger))
 58    ///     .BuildHost();
 59    /// </code>
 60    /// </example>
 61    public static CreateHostOptions UsingLogger(
 62        this CreateHostOptions options,
 63        ILogger logger)
 64    {
 565        ArgumentNullException.ThrowIfNull(options);
 466        ArgumentNullException.ThrowIfNull(logger);
 67
 368        return options with { Logger = logger };
 69    }
 70
 71    /// <summary>
 72    /// Configures the options to use the current process's command line arguments,
 73    /// automatically stripping the executable path that appears at index 0 of
 74    /// <see cref="Environment.GetCommandLineArgs"/>.
 75    /// </summary>
 76    /// <param name="options">The options to configure.</param>
 77    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the current process's command line arguments.</r
 78    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> is null.</exception>
 79    /// <example>
 80    /// <code>
 81    /// var host = new Syringe()
 82    ///     .ForHost()
 83    ///     .UsingOptions(() => CreateHostOptions.Default
 84    ///         .UsingCurrentProcessArgs())
 85    ///     .BuildHost();
 86    /// </code>
 87    /// </example>
 88    public static CreateHostOptions UsingCurrentProcessArgs(
 89        this CreateHostOptions options)
 90    {
 591        ArgumentNullException.ThrowIfNull(options);
 92
 493        var args = Environment.GetCommandLineArgs().Skip(1).ToArray();
 494        return options.UsingArgs(args);
 95    }
 96
 97    /// <summary>
 98    /// Configures the options to use the specified command line arguments.
 99    /// </summary>
 100    /// <param name="options">The options to configure.</param>
 101    /// <param name="args">The command line arguments to use.</param>
 102    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the command line arguments configured.</returns>
 103    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="args"/> is nul
 104    public static CreateHostOptions UsingArgs(
 105        this CreateHostOptions options,
 106        string[] args)
 107    {
 5108        ArgumentNullException.ThrowIfNull(options);
 5109        ArgumentNullException.ThrowIfNull(args);
 110
 5111        var newOptions = options with
 5112        {
 5113            Settings = new HostApplicationBuilderSettings
 5114            {
 5115                Args = args,
 5116                ApplicationName = options.Settings.ApplicationName,
 5117                ContentRootPath = options.Settings.ContentRootPath,
 5118                EnvironmentName = options.Settings.EnvironmentName,
 5119                Configuration = options.Settings.Configuration,
 5120                DisableDefaults = options.Settings.DisableDefaults
 5121            }
 5122        };
 123
 5124        return newOptions;
 125    }
 126
 127    /// <summary>
 128    /// Configures the options to use the specified application name.
 129    /// </summary>
 130    /// <param name="options">The options to configure.</param>
 131    /// <param name="applicationName">The application name to use.</param>
 132    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the application name configured.</returns>
 133    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="applicationNam
 134    public static CreateHostOptions UsingApplicationName(
 135        this CreateHostOptions options,
 136        string applicationName)
 137    {
 5138        ArgumentNullException.ThrowIfNull(options);
 5139        ArgumentNullException.ThrowIfNull(applicationName);
 140
 5141        var newOptions = options with
 5142        {
 5143            Settings = new HostApplicationBuilderSettings
 5144            {
 5145                Args = options.Settings.Args,
 5146                ApplicationName = applicationName,
 5147                ContentRootPath = options.Settings.ContentRootPath,
 5148                EnvironmentName = options.Settings.EnvironmentName,
 5149                Configuration = options.Settings.Configuration,
 5150                DisableDefaults = options.Settings.DisableDefaults
 5151            }
 5152        };
 153
 5154        return newOptions;
 155    }
 156
 157    /// <summary>
 158    /// Configures the options to use the specified environment name.
 159    /// </summary>
 160    /// <param name="options">The options to configure.</param>
 161    /// <param name="environmentName">The environment name to use.</param>
 162    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the environment name configured.</returns>
 163    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="environmentNam
 164    public static CreateHostOptions UsingEnvironmentName(
 165        this CreateHostOptions options,
 166        string environmentName)
 167    {
 3168        ArgumentNullException.ThrowIfNull(options);
 3169        ArgumentNullException.ThrowIfNull(environmentName);
 170
 3171        var newOptions = options with
 3172        {
 3173            Settings = new HostApplicationBuilderSettings
 3174            {
 3175                Args = options.Settings.Args,
 3176                ApplicationName = options.Settings.ApplicationName,
 3177                ContentRootPath = options.Settings.ContentRootPath,
 3178                EnvironmentName = environmentName,
 3179                Configuration = options.Settings.Configuration,
 3180                DisableDefaults = options.Settings.DisableDefaults
 3181            }
 3182        };
 183
 3184        return newOptions;
 185    }
 186
 187    /// <summary>
 188    /// Configures the options to use the specified content root path.
 189    /// </summary>
 190    /// <param name="options">The options to configure.</param>
 191    /// <param name="contentRootPath">The content root path to use.</param>
 192    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the content root path configured.</returns>
 193    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="contentRootPat
 194    public static CreateHostOptions UsingContentRootPath(
 195        this CreateHostOptions options,
 196        string contentRootPath)
 197    {
 1198        ArgumentNullException.ThrowIfNull(options);
 1199        ArgumentNullException.ThrowIfNull(contentRootPath);
 200
 1201        var newOptions = options with
 1202        {
 1203            Settings = new HostApplicationBuilderSettings
 1204            {
 1205                Args = options.Settings.Args,
 1206                ApplicationName = options.Settings.ApplicationName,
 1207                ContentRootPath = contentRootPath,
 1208                EnvironmentName = options.Settings.EnvironmentName,
 1209                Configuration = options.Settings.Configuration,
 1210                DisableDefaults = options.Settings.DisableDefaults
 1211            }
 1212        };
 213
 1214        return newOptions;
 215    }
 216
 217    /// <summary>
 218    /// Adds a pre-plugin registration callback to the options.
 219    /// </summary>
 220    /// <param name="options">The options to configure.</param>
 221    /// <param name="callback">The callback to add for pre-plugin registration.</param>
 222    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callback added.</returns>
 223    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callback"/> is
 224    public static CreateHostOptions UsingPrePluginRegistrationCallback(
 225        this CreateHostOptions options,
 226        Action<IServiceCollection> callback)
 227    {
 2228        ArgumentNullException.ThrowIfNull(options);
 2229        ArgumentNullException.ThrowIfNull(callback);
 2230        return options.UsingPrePluginRegistrationCallbacks(callback);
 231    }
 232
 233    /// <summary>
 234    /// Adds multiple pre-plugin registration callbacks to the options.
 235    /// </summary>
 236    /// <param name="options">The options to configure.</param>
 237    /// <param name="callbacks">The callbacks to add for pre-plugin registration.</param>
 238    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns>
 239    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i
 240    public static CreateHostOptions UsingPrePluginRegistrationCallbacks(
 241        this CreateHostOptions options,
 242        params Action<IServiceCollection>[] callbacks)
 243    {
 3244        ArgumentNullException.ThrowIfNull(options);
 3245        ArgumentNullException.ThrowIfNull(callbacks);
 3246        return options.UsingPrePluginRegistrationCallbacks(callbacks.AsEnumerable());
 247    }
 248
 249    /// <summary>
 250    /// Adds multiple pre-plugin registration callbacks to the options.
 251    /// </summary>
 252    /// <param name="options">The options to configure.</param>
 253    /// <param name="callbacks">The callbacks to add for pre-plugin registration.</param>
 254    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns>
 255    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i
 256    public static CreateHostOptions UsingPrePluginRegistrationCallbacks(
 257        this CreateHostOptions options,
 258        IEnumerable<Action<IServiceCollection>> callbacks)
 259    {
 3260        ArgumentNullException.ThrowIfNull(options);
 3261        ArgumentNullException.ThrowIfNull(callbacks);
 262
 3263        var allCallbacks = new List<Action<IServiceCollection>>(options.PrePluginRegistrationCallbacks);
 3264        allCallbacks.AddRange(callbacks);
 265
 3266        var newOptions = options with
 3267        {
 3268            PrePluginRegistrationCallbacks = allCallbacks
 3269        };
 270
 3271        return newOptions;
 272    }
 273
 274    /// <summary>
 275    /// Adds a post-plugin registration callback to the options.
 276    /// </summary>
 277    /// <param name="options">The options to configure.</param>
 278    /// <param name="callback">The callback to add for post-plugin registration.</param>
 279    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callback added.</returns>
 280    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callback"/> is
 281    public static CreateHostOptions UsingPostPluginRegistrationCallback(
 282        this CreateHostOptions options,
 283        Action<IServiceCollection> callback)
 284    {
 7285        ArgumentNullException.ThrowIfNull(callback);
 6286        return options.UsingPostPluginRegistrationCallbacks(callback);
 287    }
 288
 289    /// <summary>
 290    /// Adds multiple post-plugin registration callbacks to the options.
 291    /// </summary>
 292    /// <param name="options">The options to configure.</param>
 293    /// <param name="callbacks">The callbacks to add for post-plugin registration.</param>
 294    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns>
 295    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i
 296    public static CreateHostOptions UsingPostPluginRegistrationCallbacks(
 297        this CreateHostOptions options,
 298        params Action<IServiceCollection>[] callbacks)
 299    {
 9300        ArgumentNullException.ThrowIfNull(options);
 9301        ArgumentNullException.ThrowIfNull(callbacks);
 8302        return options.UsingPostPluginRegistrationCallbacks(callbacks.AsEnumerable());
 303    }
 304
 305    /// <summary>
 306    /// Adds multiple post-plugin registration callbacks to the options.
 307    /// </summary>
 308    /// <param name="options">The options to configure.</param>
 309    /// <param name="callbacks">The callbacks to add for post-plugin registration.</param>
 310    /// <returns>A new instance of <see cref="CreateHostOptions"/> with the callbacks added.</returns>
 311    /// <exception cref="ArgumentNullException">Thrown when <paramref name="options"/> or <paramref name="callbacks"/> i
 312    public static CreateHostOptions UsingPostPluginRegistrationCallbacks(
 313        this CreateHostOptions options,
 314        IEnumerable<Action<IServiceCollection>> callbacks)
 315    {
 10316        ArgumentNullException.ThrowIfNull(options);
 10317        ArgumentNullException.ThrowIfNull(callbacks);
 318
 9319        var allCallbacks = new List<Action<IServiceCollection>>(options.PostPluginRegistrationCallbacks);
 9320        allCallbacks.AddRange(callbacks);
 321
 9322        var newOptions = options with
 9323        {
 9324            PostPluginRegistrationCallbacks = allCallbacks
 9325        };
 326
 9327        return newOptions;
 328    }
 329}

Methods/Properties

UsingStartupConsoleLogger(NexusLabs.Needlr.Hosting.CreateHostOptions,System.String,Microsoft.Extensions.Logging.LogLevel)
UsingLogger(NexusLabs.Needlr.Hosting.CreateHostOptions,Microsoft.Extensions.Logging.ILogger)
UsingCurrentProcessArgs(NexusLabs.Needlr.Hosting.CreateHostOptions)
UsingArgs(NexusLabs.Needlr.Hosting.CreateHostOptions,System.String[])
UsingApplicationName(NexusLabs.Needlr.Hosting.CreateHostOptions,System.String)
UsingEnvironmentName(NexusLabs.Needlr.Hosting.CreateHostOptions,System.String)
UsingContentRootPath(NexusLabs.Needlr.Hosting.CreateHostOptions,System.String)
UsingPrePluginRegistrationCallback(NexusLabs.Needlr.Hosting.CreateHostOptions,System.Action`1<Microsoft.Extensions.DependencyInjection.IServiceCollection>)
UsingPrePluginRegistrationCallbacks(NexusLabs.Needlr.Hosting.CreateHostOptions,System.Action`1<Microsoft.Extensions.DependencyInjection.IServiceCollection>[])
UsingPrePluginRegistrationCallbacks(NexusLabs.Needlr.Hosting.CreateHostOptions,System.Collections.Generic.IEnumerable`1<System.Action`1<Microsoft.Extensions.DependencyInjection.IServiceCollection>>)
UsingPostPluginRegistrationCallback(NexusLabs.Needlr.Hosting.CreateHostOptions,System.Action`1<Microsoft.Extensions.DependencyInjection.IServiceCollection>)
UsingPostPluginRegistrationCallbacks(NexusLabs.Needlr.Hosting.CreateHostOptions,System.Action`1<Microsoft.Extensions.DependencyInjection.IServiceCollection>[])
UsingPostPluginRegistrationCallbacks(NexusLabs.Needlr.Hosting.CreateHostOptions,System.Collections.Generic.IEnumerable`1<System.Action`1<Microsoft.Extensions.DependencyInjection.IServiceCollection>>)