Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform logging in ConfigureServices method of Startup.cs in ASP.NET Core 5.0

Constructor injection of a logger into Startup works in earlier versions of ASP.NET Core because a separate DI container is created for the Web Host. As of now only one container is created for Generic Host, see the breaking change announcement.

Startup.cs

public class Startup
{
    /// <summary> The configuration. </summary>
    public IConfiguration Configuration { get; }
    
    /// <summary> The web host environment. </summary>
    public IWebHostEnvironment WebHostEnvironment { get; }

    public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
    {
        Configuration = configuration;
        WebHostEnvironment = webHostEnvironment;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddServices(Configuration);    // This is a custom method, that adds multiple services to the container.
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) =>
    app.UseComponents(env, Configuration, logger);
}

Now as per the MSDN, I have modified my Startup.cs as follows:

public class Startup
{
    /// <summary> The configuration. </summary>
    public IConfiguration Configuration { get; }
    
    public ILogger<Startup> Logger { get; set; }

    /// <summary> The web host environment. </summary>
    public IWebHostEnvironment WebHostEnvironment { get; }

    public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
    {
        Configuration = configuration;
        WebHostEnvironment = webHostEnvironment;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton((container) =>
        {
            var logger = container.GetRequiredService<ILogger<MyService>>();
            return new Startup(Configuration, WebHostEnvironment) { Logger = logger };
        });    // I know this is incorrect, but that my question- how do I correctly access logger in here?
        services.AddServices(Configuration, Logger);    // Logger => null
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) =>
    app.UseComponents(env, Configuration, logger);
}

StartupExtension.ConfigureServices.cs

public static partial class StartupExtension
{
    #region Internal Method
    /// <summary> Adds all the services required by the DemoUsageApp. </summary>
    /// <param name="services">The services, <see cref="IServiceCollection"/>.</param>
    /// <param name="configuration">The configuration, <see cref="IConfiguration"/>.</param>
    /// <param name="logger">The logger, <see cref="ILogger{Startup}"/>.</param>
    /// <returns></returns>
    internal static IServiceCollection AddServices(this IServiceCollection services, IConfiguration configuration, ILogger logger)
    {
        services.AddDotNetCoreServices(configuration, logger);
        services.AddCrossCuttingServices(configuration, logger);

        return services;
    }
    #endregion Internal Method

    #region Private Methods
    /// <summary> Adds .NET Core services. </summary>
    /// <param name="services">The services, <see cref="IServiceCollection"/>.</param>
    /// <param name="configuration">The configuration, <see cref="IConfiguration"/>.</param>
    /// <param name="logger">The logger, <see cref="ILogger{Startup}"/>.</param>
    /// <returns></returns>
    private static IServiceCollection AddDotNetCoreServices(this IServiceCollection services, IConfiguration configuration, ILogger logger)
    {
        logger.LogInformation("----  Adding .NET Core components  ----");

        services.AddMvc();
        services.AddControllers();

        logger.LogInformation("----  Successfully added .NET Core components  ----");

        return services;
    }

    /// <summary> Adds Cross-Cutting services. </summary>
    /// <param name="services">The services, <see cref="IServiceCollection"/>.</param>
    /// <param name="configuration">The configuration, <see cref="IConfiguration"/>.</param>
    /// <param name="logger">The logger, <see cref="ILogger{Startup}"/>.</param>
    /// <returns></returns>
    private static IServiceCollection AddCrossCuttingServices(this IServiceCollection services, IConfiguration configuration, ILogger logger)
    {
        logger.LogInformation("----  Adding Cross-Cutting components  ----");

        services.AddSql(configuration, logger);    //This is a extension methods, that adds Sql services (which are also completely custom) to the application.

        logger.LogInformation("----  Successfully added Cross-Cutting components  ----");

        return services;
    }
    #endregion Private Methods
}

Question- How do I access Logger in ConfigureServices method of Startup.cs so that I can pass it to other custom extension methods that add custom services to the container?

NOTE: Solution requested for Microsoft and Serilog Logger (optional).

Update 1: As @MathewBeck pointed out in his 2nd comment, I already have the following structure of Program.cs:

public class Program
{
    #region Public Methods
    /// <summary> Main method, the entry-point of the application. </summary>
    /// <param name="args">The args <see cref="string[]"/></param>
    public static void Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();
        var logger = host.Services.GetRequiredService<ILogger<Program>>();
        logger.LogInformation("Host created.");

        host.Run();
    }

    /// <summary> Creates the host builder. </summary>
    /// <param name="args">The args <see cref="string[]"/></param>
    /// <returns></returns>
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            //.ConfigureLogging(loggingBuilder =>
            //{
            //    loggingBuilder.ClearProviders();
            //    loggingBuilder.AddConsole();
            //    loggingBuilder.AddDebug();
            //    loggingBuilder.AddEventLog();
            //    loggingBuilder.AddEventSourceLogger();
            //})
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.UseConfiguration(GetConfiguration());
            });
    #endregion Public Methods

    #region Private Methods
    /// <summary> Gets the configuration. </summary>
    /// <returns></returns>
    private static IConfiguration GetConfiguration()
    {
        var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", true, true)
            .AddJsonFile($"appsettings.{env}.json", true, true);

        return builder.Build();
    }
    #endregion Private Methods
} 
like image 660
phougatv Avatar asked Nov 14 '22 20:11

phougatv


1 Answers

ASP.NET 5.0

I have used Control Startup class activation feature introduced in .NET 5.0 -

To quote MSDN documentation -

An additional UseStartup overload has been added that lets an app provide a factory method for controlling Startup class activation. Controlling Startup class activation is useful to pass additional parameters to Startup that are initialized along with the host

public class Program
{
    public static async Task Main(String[] args)
    {
        var loggerFactory = LoggerFactory
            .Create(builder =>
            {
                builder.ClearProviders();
                builder.AddConsole();
            });
        var logger = loggerFactory.CreateLogger<Program>();
        var host = Host.CreateDefaultBuilder()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup(context => new Startup(ILogger<Program> logger, /*based on the ctor of startup one can pass the required arguments here*/)
            })
            .Build();

        await host.RunAsync();
    }
}
like image 80
phougatv Avatar answered Jan 22 '23 06:01

phougatv