ASP.NET Core 2.0使用Log4net实现记录日志功能

一、安装Log4net

1、使用Nuget包进行安装

在依赖项上面右键,选择“管理NuGet程序包”,如下图所示:

ASP.NET Core 2.0使用Log4net实现记录日志功能_第1张图片

在浏览界面输入log4net,然后点击安装,如下图所示:

ASP.NET Core 2.0使用Log4net实现记录日志功能_第2张图片

2、使用程序包管理器控制台进行安装

使用Install-Package Log4net命令进行安装,如下图所示:

ASP.NET Core 2.0使用Log4net实现记录日志功能_第3张图片

二、配置log4net使用的配置文件

配置文件如下:

 1 xml version="1.0" encoding="utf-8" ?>
 2  <configuration>
 3    
 4    <log4net>
 5      <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
 6        <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
 7      appender>
 8  
 9      <appender name="FileAppender" type="log4net.Appender.FileAppender">
10      <file value="log-file.log" />
11      <appendToFile value="true" />
12      <layout type="log4net.Layout.PatternLayout">
13        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
14      layout>
15    appender>
16 
17    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
18      <file value="D:\study\logfile/" />   //指定日志文件保存的目录
19      <appendToFile value="true" />
20      <rollingStyle value="Composite" />
21      <staticLogFileName value="false" />
22      <datePattern value="yyyyMMdd'.log'" />
23      <maxSizeRollBackups value="10" />
24      <maximumFileSize value="1MB" />
25      <layout type="log4net.Layout.PatternLayout">
26        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
27      layout>
28    appender>
29 
30    
31    <root>
32      <level value="ALL" />
33      <appender-ref ref="ConsoleAppender" />
34      <appender-ref ref="FileAppender" />
35        <appender-ref ref="RollingLogFileAppender" />
36      root>
37  
38    log4net>
39  configuration>

 三、在Startup.cs类里面配置使用log4net

1 public static ILoggerRepository repository { get; set; }
2 public Startup(IConfiguration configuration)
3 {
4             Configuration = configuration;
5             repository = LogManager.CreateRepository("NETCoreRepository");
6             // 指定配置文件
7             XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
8 }

 四、在控制器里面注入log4net

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using log4net;
 7 using Microsoft.AspNetCore.Hosting;
 8 using Microsoft.AspNetCore.Mvc;
 9 using NetCoreLogDemo.Models;
10 
11 namespace NetCoreLogDemo.Controllers
12 {
13     public class HomeController : Controller
14     {
15         private ILog log;
16 
17         public HomeController(IHostingEnvironment hostingEnv)
18         {
19             this.log = LogManager.GetLogger(Startup.repository.Name, typeof(HomeController));
20         }
21 
22         public IActionResult Index()
23         {
24             log.Error("测试日志");
25             return View();
26         }
27 
28         public IActionResult About()
29         {
30             ViewData["Message"] = "Your application description page.";
31             
32             return View();
33         }
34 
35         public IActionResult Contact()
36         {
37             ViewData["Message"] = "Your contact page.";
38 
39             return View();
40         }
41 
42         public IActionResult Error()
43         {
44             return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
45         }
46     }
47 }

 测试结果:

 

你可能感兴趣的:(ASP.NET Core 2.0使用Log4net实现记录日志功能)