.NET 核心 6 中的 log4net
log4net in .NET core 6
我以前用的是.NET core 5,但现在我想用.NET core 6,看来旧的Startup.cs
现在和Program.cs
混合了。
问题是我正在尝试将 log4net 添加到我的项目中,它给了我一个错误,我不确定发生了什么或者我遗漏了什么。错误说:
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
我已经通过 Nuget Managment 和包管理器控制台安装了 log4net 似乎没有任何效果,这是我的 Program.cs
文件的一部分:
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();
您需要一个单独的扩展包才能使其适用于大多数用例。
您需要的包裹是:
https://www.nuget.org/packages/Microsoft.Extensions.Logging.Log4Net.AspNetCore/
文档和来源:
https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore
您可以像下面这样使用:
builder.Host.ConfigureLogging(logging =>
{
logging.AddLog4Net(log4NetConfigFile: "log4net.config");
logging.ClearProviders();
logging.AddConsole();//for Logging on Console
logging.AddLog4Net();//for DB Query Logging
});
我以前用的是.NET core 5,但现在我想用.NET core 6,看来旧的Startup.cs
现在和Program.cs
混合了。
问题是我正在尝试将 log4net 添加到我的项目中,它给了我一个错误,我不确定发生了什么或者我遗漏了什么。错误说:
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
我已经通过 Nuget Managment 和包管理器控制台安装了 log4net 似乎没有任何效果,这是我的 Program.cs
文件的一部分:
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();
您需要一个单独的扩展包才能使其适用于大多数用例。
您需要的包裹是: https://www.nuget.org/packages/Microsoft.Extensions.Logging.Log4Net.AspNetCore/
文档和来源: https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore
您可以像下面这样使用:
builder.Host.ConfigureLogging(logging =>
{
logging.AddLog4Net(log4NetConfigFile: "log4net.config");
logging.ClearProviders();
logging.AddConsole();//for Logging on Console
logging.AddLog4Net();//for DB Query Logging
});