简体   繁体   中英

Prevent .NET from logging to the console?

I have implemented dependency injection in my application. And I configuration services as follows.

IConfiguration configuration = context.Configuration;

services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
});

services.ConfigureRailIncApi(configuration);

services.Configure<EmailSettings>(configuration.GetSection("EmailSettings"));

services.AddSingleton(configuration);
services.AddSingleton(Logger);

services.AddSingleton<Application>();
services.AddSingleton<ServiceManager>();

This all works fine except now the framework is logging to the console, including the contents of any queries sent to the database.

Somehow the internal logging has been enabled but I don't see where I've enabled it.

How can I prevent .NET from logging to the screen?

Note: Logger is an instance of my own logging class ConsoleLogger . This class does not implement ILogger or use anything else from .NET. It is simply a custom class that also logs to the console and a file. Logging from this logger is working fine. And I've confirmed the .NET logs are not being sent to this class.

Update:

Here's my code that configures the host.

public void Configure(string[] args, Action< HostBuilderContext, IServiceCollection> configureServices, Action<IHostBuilder> configureHost)
{
    IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);

    hostBuilder.ConfigureServices((context, services) =>
    {
        services.AddSingleton(this);
        configureServices(context, services);
    });

    configureHost(hostBuilder);

    AppHost = hostBuilder.Build();
}

And here's the code that calls it.

ApplicationServices appServices = new();

appServices.Configure(args, (context, services) =>
{
    IConfiguration configuration = context.Configuration;

    services.AddDbContext<ApplicationDbContext>(options =>
    {
        options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
    });

    services.ConfigureRailIncApi(configuration);

    services.Configure<EmailSettings>(configuration.GetSection("EmailSettings"));

    services.AddSingleton(configuration);
    services.AddSingleton(Logger);

    services.AddSingleton<Application>();
    services.AddSingleton<ServiceManager>();
},
builder =>
{

});

Note

The CreateDefaultBuilder method:
...
...
Adds the following logging providers:

  • Console
  • Debug
  • EventSource
  • EventLog (only when running on Windows)

Reference Default builder settings

You would need to explicitly remove the default providers and add only the ones you want.

IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);

//...

hostBuilder.ConfigureLogging(logging => {
    logging.ClearProviders();

    //... add my providers here
});

//...

Note that if you want to avoid other extension adding their own providers, then perform the logging configuration last so that you are sure about which loggers were added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM