1新建NetCore項目,我這裡NetCoreSDK版本是2.2.0。 2.進入NuGet程式包官網 : https://www.nuget.org,搜索以下兩個包並安裝到項目中。 Microsoft.Extensions.Logging.Log4Net.AspNetCore Log4Net 打開項 ...
1新建NetCore項目,我這裡NetCoreSDK版本是2.2.0。
2.進入NuGet程式包官網 : https://www.nuget.org,搜索以下兩個包並安裝到項目中。
Microsoft.Extensions.Logging.Log4Net.AspNetCore
Log4Net
打開項目的程式包管理控制台,運行以下兩條命令,安裝Log4Net到項目中
a、Install-Package Microsoft.Extensions.Logging.Log4Net.AspNetCore -Version 2.2.10 b、Install-Package log4net -Version 2.0.8 3、項目新建一個目錄,新增一個log4net.config的配置文件。
config內容如下。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- This section contains the log4net configuration settings --> <log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ALL"/> <appender-ref ref="SysAppender"/> </root> <!-- Print only messages of level DEBUG or above in the packages --> <logger name="WebLogger"> <level value="DEBUG"/> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Content/Logs/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%n日誌時間:%d [%t] %n日誌級別:%-5p %n日 志 類:%c [%L] %n%m %n" /> <param name="Header" value=" ----------------------Header-------------------------- " /> <param name="Footer" value=" ----------------------Footer-------------------------- " /> </layout> </appender> <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> </layout> </appender> </log4net> </configuration>
4、在Startup.cs中的配置如下
...
public static log4net.Repository.ILoggerRepository LogRepository { get; set; } public Startup(IConfiguration configuration) { Configuration = configuration; LogRepository = log4net.LogManager.CreateRepository("NetCoreRepository"); log4net.Config.XmlConfigurator.Configure(LogRepository, new FileInfo(@"Config\log4net.config")); }
...
5、新建一個Core文件夾,增加一個Log4Net.cs的日誌幫助類
using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace CMSCore.Web.Core { public class Log4Net { private static ILog log; static Log4Net() { log = LogManager.GetLogger(Startup.LogRepository.Name,typeof(Log4Net)); } public static void Info(string message) { log.Info(message); } public static void Warn(string message) { log.Warn(message); } public static void Warn(string message,Exception ex) { log.Warn(message,ex); } public static void Error(string message) { log.Error(message); } public static void Error(string message,Exception ex) { log.Error(message,ex); } public static void Debug(string message) { log.Debug(message); } public static void Debug(string message, Exception ex) { log.Debug(message, ex); } } }
6、在項目中需要記錄日誌的類引用該命名空間,即可在項目中使用log4net的類的方法記錄日誌了。