log4net in .NET core 6

Viewed 2495

I used to use .NET core 5, but now I'm trying to use .NET core 6, it seems the old Startup.cs is mixed now with Program.cs.

The thing is that I'm trying to add log4net to my project, and it gives me an error I'm not sure what is happening or I'm missing something. The error says:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1061  'ILoggingBuilder' does not contain a definition for 'AddLog4Net' and no accessible extension method 'AddLog4Net' accepting a first argument of type 'ILoggingBuilder' could be found

I have installed log4net via Nuget Managment and Package Manager Console Nothing seems to work, here is a part of my Program.cs file:

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddLog4Net(); // <-- here is where I got the error

// Add services to the container.
var services = builder.Services;
services.AddCors();
services.AddControllers();

services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddLogging();

2 Answers

You can use similar like below:

builder.Host.ConfigureLogging(logging =>
                              {
                                  logging.AddLog4Net(log4NetConfigFile: "log4net.config");
                                  logging.ClearProviders();
                                  logging.AddConsole();//for Logging on Console 
                                  logging.AddLog4Net();//for DB Query Logging
                              });
Related