.NET Core下使用Log4Net记录日志的方法步骤

Log4Net 相信大家都很熟悉了,算是比较主流和著名的日志组件了。

官网: logging.apache.org

开源地址: https://github.com/apache/logging-log4net

最佳实践

在项目中添加组件包

Install-Package log4net

添加 log4net.config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 </configSections>
 <log4net debug="false">

 <appender name="info" type="log4net.Appender.RollingFileAppender,log4net">
  <param name="File" value="http://www.cppcns.com/wangluo/aspnet/log4net/info/" />
  <param name="AppendToFile" value="http://www.cppcns.com/wangluo/aspnet/true" />
  <param name="MaxSizeRollBackups" value="http://www.cppcns.com/wangluo/aspnet/-1"/>
  <param name="MaximumFileSize" value="http://www.cppcns.com/wangluo/aspnet/5MB"/>
  <param name="RollingStyle" value="http://www.cppcns.com/wangluo/aspnet/Composite" />
  <param name="DatePattern" value="http://www.cppcns.com/wangluo/aspnet/yyyyMMdd//HH".log"" />
  <param name="StaticLogFileName" value="http://www.cppcns.com/wangluo/aspnet/false" />
  <layout type="log4net.Layout.PatternLayout,log4net">
  <param name="ConversionPattern" value="http://www.cppcns.com/wangluo/aspnet/%n
{
 "system": "Meowv.Blog",
 "datetime": "%d",
 "description": "%m",
 "level": "%p",
 "info": "%exception"
}" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="http://www.cppcns.com/wangluo/aspnet/INFO" />
  <levelMax value="http://www.cppcns.com/wangluo/aspnet/INFO" />
  </filter>
 </appender>

 <appender name="error" type="log4net.Appender.RollingFileAppender,log4net">
  <param name="File" value="http://www.cppcns.com/wangluo/aspnet/log4net/error/" />
  <param name="AppendToFile" value="http://www.cppcns.com/wangluo/aspnet/true" />
  <param name="MaxSizeRollBackups" value="http://www.cppcns.com/wangluo/aspnet/-1"/>
  <param name="MaximumFileSize" value="http://www.cppcns.com/wangluo/aspnet/5MB"/>
  <param name="RollingStyle" value="http://www.cppcns.com/wangluo/aspnet/Composite" />
  <param name="DatePattern" value="http://www.cppcns.com/wangluo/aspnet/yyyyMMdd//HH".log"" />
  <param name="StaticLogFileName" value="http://www.cppcns.com/wangluo/aspnet/false" />
  <layout type="log4net.Layout.PatternLayout,log4net">
  <param name="ConversionPattern" value="http://www.cppcns.com/wangluo/aspnet/%n
{
 "system": "Meowv.Blog",
 "datetime": "%d",
 "description": "%m",
 "level": "%p",
 "info": "%exception"
}" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="http://www.cppcns.com/wangluo/aspnet/ERROR" />
  <levelMax value="http://www.cppcns.com/wangluo/aspnet/ERROR" />
  </filter>
 </appender>

 <root>
  <level value="http://www.cppcns.com/wangluo/aspnet/ALL"></level>
  <appender-ref ref="info"/>
  <appender-ref ref="error"/>
 </root>

 </log4net>

</configuration>

控制台项目

using log4net;
using log4net.Config;
using System;
using System.IO;
using System.Reflection;

namespace ConsoleAppDemo
{
 class Program
 {
  static void Main(string[] args)
  {
   var log4netRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
   XmlConfigurator.Configure(log4netRepository, new FileInfo("log4net.config"));

   ILog log = LogManager.GetLogger(log4netRepository.Name, "NETCorelog4net");

   log.Info("NETCorelog4net log");
   log.Info("test log");
   log.Error("error");
   log.Info("linezero");
   Console.ReadKey();
  }
 }
}

.NET Core下使用Log4Net记录日志的方法步骤 

AspNetCore项目

新建一个静态类,写一个扩展方法 UseLog4Net() ,用于 log4net 初始化。

public static class Log4NetExtensions
{
 public static IHostBuilder UseLog4Net(this IHostBuilder hostBuilder)
 {
  var log4netRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
  XmlConfigurator.Configure(log4netRepository, new FileInfo("log4net.config"));

  return hostBuilder;
 }
}

扩展方法返回 IHostBuilder 对象,这样可以链式调用,美化代码,然后在

.NET Core下使用Log4Net记录日志的方法步骤

扫一扫手机访问