1、NuGet添加2个包:
Microsoft.Extensions.Logging.Log4Net.AspNetCore
log4net.Kafka.Core
2、Program里修改CreateWebHostBuilder:
public class Program { public static void Main(string[] args) { System.Threading.ThreadPool.SetMinThreads(200, 200); // NLogBuilder.ConfigureNLog("Config/nlog.config"); // NLogBuilder.ConfigureNLog("Config/nlog.config").GetCurrentClassLogger(); CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args). ConfigureLogging((context, loggingbuilder) => { //该方法需要引入Microsoft.Extensions.Logging名称空间 loggingbuilder.AddFilter("System", LogLevel.Warning); //过滤掉系统默认的一些日志 loggingbuilder.AddFilter("Microsoft", LogLevel.Warning);//过滤掉系统默认的一些日志 //var path = Directory.GetCurrentDirectory() + "\\log4net.config"; //不带参数:表示log4net.config的配置文件就在应用程序根目录下,也可以指定配置文件的路径 loggingbuilder.AddLog4Net("Config/log4net.config"); }) .UseStartup<Startup>(); }
添加配置文件:在Config目录下创建log4net.config,内容如下:
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="KafkaAppender" type="log4net.Kafka.Core.KafkaAppender, log4net.Kafka.Core"> <KafkaSettings> <broker value="地址1:端口,地址2:端口,地址3:端口" /> <topic value="kafka的topic" /> </KafkaSettings> <layout type="log4net.Kafka.Core.KafkaLogLayout,log4net.Kafka.Core" > <appid value="sysName" /> </layout> </appender> <root> <level value="ALL"/> <appender-ref ref="KafkaAppender" /> </root> </log4net>
原文地址:https://www.cnblogs.com/puzi0315/p/11429817.html
时间: 2024-10-18 13:41:09