方法一:
1.通过Nuget日志包log4net.aspnetcore
2.在Program中的CreateHostBuilder方法中添加如下代码
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
//引用log4net.aspnetcore写日志,筛选部分日志
.ConfigureLogging((context, loggingBuilder) =>
{
loggingBuilder.AddFilter("Microsoft", LogLevel.Warning);
loggingBuilder.AddFilter("System", LogLevel.Warning);
//默认根目录路径
//loggingBuilder.AddLog4Net();
//指定路径
var path = System.IO.Directory.GetCurrentDirectory();
loggingBuilder.AddLog4Net($"{path}/log4netcore.config");
})
//未筛选,直接显示全部日志
//.ConfigureLogging(p =>
//{
// var path = System.IO.Directory.GetCurrentDirectory();
// p.AddLog4Net($"{path}/log4netnetcore.config");//配置文件
//})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
3.log4netcore.config文件
<?xml version="1.0" encoding="utf-8" ?>
<!-- This section contains the log4net configuration settings -->
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log/log.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile/" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value="yyyyMMdd'.log'" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FileAppender" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
方法二:通过类库写日志
1.通过Nuget日志包log4net
2.在Startup中添加如下代码
//配置log4net写日志
public static ILoggerRepository repository { get; set; }
//public Startup(IConfiguration configuration)
//{
// Configuration = configuration;
//}
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
//.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
repository = LogManager.CreateRepository("NETCoreRepository");
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
WriteLog.LogRepository = repository;//类库中定义的静态变量
}
3.在类库中添加如下代码
public class WriteLog
{
public static ILoggerRepository LogRepository { get; set; }
}
public class Base
{
public static readonly ILog log = LogManager.GetLogger(WriteLog.LogRepository.Name,typeof(Base));
public static void TestLog()
{
log.Info("Info Test");
}
}
4.log4net.config的内容比上面log4netcore.config多一个节点
<configuration>
.....
</configuration>