'log4net in .NET core 6
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();
Solution 1:[1]
You need a separate extension package to make it work in most use cases.
The package you need is: https://www.nuget.org/packages/Microsoft.Extensions.Logging.Log4Net.AspNetCore/
Documentation and source: https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore
Solution 2:[2]
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
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ExplodatedFaces |
Solution 2 | Dutt93 |