这篇文章介绍了ASP.NETCore使用Log4net实现日志记录功能的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 一、安装Log4net1、使用Nuget包进行安装

右键单击依赖项并选择ldquo管理NuGet包rdquo,如下图所示:


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


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

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


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

配置文件如下所示:

lt;xml version="1.0" encoding="utf-8" gt; lt;configurationgt; lt;!-- This section contains the log4net configuration settings --gt; lt;log4netgt; lt;appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"gt; lt;layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" /gt; lt;/appendergt; lt;appender name="FileAppender" type="log4net.Appender.FileAppender"gt; lt;file value="log-file.log" /gt; lt;appendToFile value="true" /gt; lt;layout type="log4net.Layout.PatternLayout"gt; lt;conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /gt; lt;/layoutgt; lt;/appendergt; lt;appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"gt; lt;file value="D:studylogfile/" /gt; //指定日志文件保存的目录 lt;appendToFile value="true" /gt; lt;rollingStyle value="Composite" /gt; lt;staticLogFileName value="false" /gt; lt;datePattern value="yyyyMMdd'.log'" /gt; lt;maxSizeRollBackups value="10" /gt; lt;maximumFileSize value="1MB" /gt; lt;layout type="log4net.Layout.PatternLayout"gt; lt;conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /gt; lt;/layoutgt; lt;/appendergt; lt;!-- Setup the root category, add the appenders and set the default level --gt; lt;rootgt; lt;level value="ALL" /gt; lt;appender-ref ref="ConsoleAppender" /gt; lt;appender-ref ref="FileAppender" /gt; lt;appender-ref ref="RollingLogFileAppender" /gt; lt;/rootgt; lt;/log4netgt; lt;/configurationgt;三、在Startup.cs类里面配置使用log4netpublic static ILoggerRepository repository { get; set; }public Startup(IConfiguration configuration){ Configuration = configuration; repository = LogManager.CreateRepository("NETCoreRepository"); // 指定配置文件 XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));}四、在控制器里面注入log4netusing System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Threading.Tasks;using log4net;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc;using NetCoreLogDemo.Models;namespace NetCoreLogDemo.Controllers{ public class HomeController : Controller { private ILog log; public HomeController(IHostingEnvironment hostingEnv) { this.log = LogManager.GetLogger(Startup.repository.Name, typeof(HomeController)); } public IActionResult Index() { log.Error("测试日志"); return View(); } public IActionResult About() { ViewData["Message"] = "Your application description page."; return View(); } public IActionResult Contact() { ViewData["Message"] = "Your contact page."; return View(); } public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current.Id HttpContext.TraceIdentifier }); } }}

测试结果:


关于ASP.NET核心使用Log4net的日志功能的文章到此结束。希望对大家的学习有所帮助