Home > Software design >  Which package can I find ConfigureHostBuilder in?
Which package can I find ConfigureHostBuilder in?

Time:10-19

The following is part of a Class Library. I installed the following packages but it still cannot resolve ConfigureHostBuilder. Which package I can find it in?

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
  <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
</ItemGroup>
public static class LoggingExtensions
{
    public static void ConfigureLogging(this ConfigureHostBuilder builder)
    {
        const string outputTemplate =
            "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}";

        builder.UseSerilog((_, cfg) =>
            cfg
                .MinimumLevel.Information()
                .MinimumLevel.Override("System", LogEventLevel.Error)
                .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
                .Enrich.FromLogContext()
                .WriteTo.Console(outputTemplate: outputTemplate));
    }
}

CodePudding user response:

ConfigureHostBuilder is a method, not a class. You probably meant to use IHostBuilder.

UseSerilog is an extension method on IHostBuilder. It's part of the Serilog.Extensions.Hosting package.

The package's landing page shows the method is used on an IHostBuilder instance:

public static IHost BuildHost(string[] args) =>
    new HostBuilder()
        .ConfigureServices(services => services.AddSingleton<IHostedService, PrintTimeService>())
        .UseSerilog() // <- Add this line
        .Build();
}

Your method should change to

public static class LoggingExtensions
{
    public static void ConfigureLogging(this IHostBuilder builder)
    {
        const string outputTemplate =
            "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}";

        builder.UseSerilog((_, cfg) =>
            cfg
                .MinimumLevel.Information()
                .MinimumLevel.Override("System", LogEventLevel.Error)
                .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
                .Enrich.FromLogContext()
                .WriteTo.Console(outputTemplate: outputTemplate));
    }
}
  • Related