1. nuget 安装 Microsoft.Extensions.Logging.Log4Net.AspNetCore
2.修改
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>().ConfigureLogging((hostingContext, logging) => { //I assumed if clearing the providers, may clear the filter configuration, but it turned out it doesnt matter. logging.ClearProviders(); logging.SetMinimumLevel(LogLevel.Error); //This won't work, no matter that you properly have configured everything. //logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); logging.AddFilter("Default", LogLevel.Error); logging.AddFilter("Microsoft", LogLevel.Error); logging.AddFilter("System", LogLevel.Error); //I'm adding the debug provider, just so we can compare, in production you can exclude it. logging.AddDebug(); logging.AddConsole(); logging.AddLog4Net(); });
3.项目根目录添加log4net.config配置文件
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" > <file value="App_Data/Logs/Logs.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10000KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" /> </layout> </appender> <root> <appender-ref ref="RollingFileAppender" /> <level value="ERROR" /> </root> </log4net>