asp.net core 5.0 使用log4net
之前我在.net core 2.1的时候使用的是Huanent.Logging。考虑未来升级尝试使用Log4net.下面是我的配置步骤:
1、网上下载一个log4net配置文件.因为这种没有技术含量需要时间的工作就直接copy就好了.在web目录下建立一个config文件夹
<?xml version="1.0"?> <log4net> <root> <level value="DEBUG"/> <appender-ref ref="SysAppender"/> <appender-ref ref="consoleApp"/> </root> <!--信息日志配置--> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Logs\\" /> <param name="AppendToFile" value="true" /> <param name="MaximumFileSize" value="20MB" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="\\yyyyMM\\yyyyMMdd".log"" /> <param name="RollingStyle" value="Composite" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n -------------------------------------------------------------------------------------- %n日志时间:%d [%t] %n日志级别:%-5p %n日 志 类:%c [%x] %n%m %n" /> <param name="Header" value=" ====================================================================================== " /> </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> <logger name="WebLogger"> <level value="DEBUG"/> </logger> </log4net>
2、使用nuget 引入log4net 。我是2.0.8
3、编写Log4netHelper
using log4net; using log4net.Repository; using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Text; namespace NF.Common.Utility { /// <summary> /// 日志等级 /// </summary> public enum LogLevel { Error, Debug, Warning, Info } /// <summary> /// 单例模式初始化 /// </summary> public class Singleton { private ILog Log; private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } /// <summary> /// 获取日志初始化器 /// </summary> /// <param name="type">类名 方法名</param> /// <returns></returns> public ILog Init(string type) { Log = LogManager.GetLogger(Log4netHelper.Repository.Name, type); return Log; } } /// <summary> /// 日志操作类 /// </summary> public class Log4netHelper { /// <summary> /// log4net 仓储 /// </summary> public static ILoggerRepository Repository { get; set; } /// <summary> /// 输出Erro日志 /// </summary> /// <param name="message">日志内容</param> public static void Error(string message) { StackTrace trace = new StackTrace(); //获取是哪个类来调用的 var className = trace.GetFrame(1).GetMethod().DeclaringType; //获取方法名称 MethodBase method = trace.GetFrame(1).GetMethod(); var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name; WriteLog(LogLevel.Error, message, type); } /// <summary> /// 输出Warning日志 /// </summary> /// <param name="message">日志内容</param> public static void Warning(string message) { StackTrace trace = new StackTrace(); //获取是哪个类来调用的 var className = trace.GetFrame(1).GetMethod().DeclaringType; //获取方法名称 MethodBase method = trace.GetFrame(1).GetMethod(); var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name; //记录日志 WriteLog(LogLevel.Warning, message, type); } /// <summary> /// 输出Info日志 /// </summary> /// <param name="message">日志内容</param> public static void Info(string message) { StackTrace trace = new StackTrace(); //获取是哪个类来调用的 var className = trace.GetFrame(1).GetMethod().DeclaringType; //获取方法名称 MethodBase method = trace.GetFrame(1).GetMethod(); var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name; //记录日志 WriteLog(LogLevel.Info, message, type); } /// <summary> /// 输出Debug日志 /// </summary> /// <param name="message">日志内容</param> public static void Debug(string message) { StackTrace trace = new StackTrace(); //获取是哪个类来调用的 var className = trace.GetFrame(1).GetMethod().DeclaringType; //获取方法名称 MethodBase method = trace.GetFrame(1).GetMethod(); var type = "类名:" + className.Namespace + "\r\n\r\t\r\r方法名:" + method.Name; //记录日志 WriteLog(LogLevel.Debug, message, type); } /// <summary> /// 写日志 /// </summary> /// <param name="logLevel">日志等级</param> /// <param name="message">日志信息</param> /// <param name="type">类名 方法名</param> private static void WriteLog(LogLevel logLevel, string message, string type) { ILog Log = Singleton.getInstance().Init(type); switch (logLevel) { case LogLevel.Debug: Log.Debug(message); break; case LogLevel.Error: Log.Error(message); break; case LogLevel.Info: Log.Info(message); break; case LogLevel.Warning: Log.Warn(message); break; } } } }
4、修改Startup 在ConfigureServices(IServiceCollection services)方法里加入如下代码:
Log4netHelper.Repository = LogManager.CreateRepository("NETCoreRepository"); XmlConfigurator.Configure(Log4netHelper.Repository, new FileInfo(Environment.CurrentDirectory + "/Config/log4net.config"));
5、测试完成。
本文来自博客园,作者:jevan,转载请注明原文链接:https://www.cnblogs.com/DoNetCShap/p/14006710.html