Dependency setup (e.g. logging) in Program.cs when EF Migrations do not go through Program.cs

I will try to explain my situation as best as possible, and I hope this makes sense.

My project is a .NET web interface. With a separate class library project that contains models, including DbContext material.

Problem No. 1 I want to be able to log into the console from inside Startup.cs

Reason: I am currently debugging the setting of a variable from an environment variable. I want to output what was configured for the console.

Example: How to write logs from Startup.cs

The solution now was to add an ILoggerFactory to the Service container in Program.cs as such:

var host = new WebHostBuilder()
        .UseKestrel()
        .ConfigureServices(s => {
            s.AddSingleton<IFormatter, LowercaseFormatter>();
        })
        .ConfigureLogging(f => f.AddConsole(LogLevel.Debug))
        .UseStartup<Startup>()
        .Build();

        host.Run();

Startup.cs, ILoggerFactory, , . :

public class Startup {

  ILogger _logger;
  IFormatter _formatter;

  public Startup(ILoggerFactory loggerFactory, IFormatter formatter){
    _logger = loggerFactory.CreateLogger<Startup>();
    _formatter = formatter;
  }

  public void ConfigureServices(IServiceCollection services)  {
    _logger.LogDebug($"Total Services Initially: {services.Count}");

    // register services
    //services.AddSingleton<IFoo, Foo>();
  }

  public void Configure(IApplicationBuilder app, IFormatter formatter) {
    // note: can request IFormatter here as well as via constructor
    _logger.LogDebug("Configure() started...");
    app.Run(async (context) => await context.Response.WriteAsync(_formatter.Format("Hi!")));
    _logger.LogDebug("Configure() complete.");
  }

- , , , .

.

dotnet ef --startup-project = myAPIProject

, EF Program.cs, Startup. ILoggerFactory, EF , .

- ?

+2
1

, .

, ILoggerFactory Startup.cs. ILogger :

private ILogger<Startup> _logger;
    public Startup(IHostingEnvironment env, ILogger<Startup> logger) {
        _env = env;
        _logger = logger;
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

            _configuration = builder.Build();
    }

My Program.cs :

    var host = new WebHostBuilder() 
            .UseKestrel()
            .UseConfiguration(config)
            .ConfigureServices(s => s.AddSingleton<IConfigurationRoot>(config))
            .ConfigureLogging(f => {
                f.AddConsole()
                .AddDebug();
            })
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();

, Startup ILogger DI ( EF-, ).

: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup ( ", " ).

... , .NET. , - , , .. . , , .

- " " .NET, , , . , #, , Runtime, .

(, ILoggerFactory, EF, , Program.cs)

. , . . .NET core, , ( ) , , .

+1

Source: https://habr.com/ru/post/1681898/


All Articles