error CS1929: 'ConfigureHostBuilder' does not contain a definition for 'UseSerilog'

Veeraraghavan SEKAR 0 Reputation points
2023-07-19T15:05:45.7333333+00:00

Hi Team,

I am trying to use Serilog for capturing logs for my Opentelemetry. When I am using Web App console example into my code I am getting following error

error CS1929: 'ConfigureHostBuilder' does not contain a definition for 'UseSerilog' and the best extension method overload 'SerilogWebHostBuilderExtensions.UseSerilog(IWebHostBuilder, ILogger?, bool, LoggerProviderCollection?)' requires a receiver of type 'IWebHostBuilder'

Background of the work,

I am using dotnet7 framework and using Bazel to build the packages. The Aspcore and .NET core are at version 7. The Serilog version is 3.0.1 and Serilog.AspNetCore is 7.0.0.

Code which getting error is

 Log.Information("Starting web application");
 var builder = WebApplication.CreateBuilder(args);
 builder.Host.UseSerilog(); // <-- Add this line //Error here

I would like to know is their any dependency issue or I am missing something? It would be helpful if you can hint me some directions what all could lead to such a error. Thanks again

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,648 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2023-07-19T16:49:42.17+00:00

    you add to the Logging, not host. you also may want to clear other logger:

    // optional: remove default logging providers
    builder.Logging.ClearProviders();
    
    // Serilog configuration		
    var logger = new LoggerConfiguration()
        .WriteTo.Console()
        .CreateLogger();
    
    // Register Serilog
    builder.Logging.AddSerilog(logger);