dotnet core 6.0 配置使用log4net

Posted 圆滚滚的胖狸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dotnet core 6.0 配置使用log4net相关的知识,希望对你有一定的参考价值。

开发工具 vs2022社区版,.net 6.0

一、NuGet添加 Microsoft.Extensions.Logging.Log4Net.AspNetCore

二、在项目根目录添加log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
	<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
		<param name="File" value="Log//" />
		<param name="AppendToFile" value="true" />
		<param name="MaximumFileSize" value="102KB" />
		<param name="MaxSizeRollBackups" value="10" />
		<param name="StaticLogFileName" value="false" />
		<param name="DatePattern" value="yyyyMMdd&quot;.log&quot;" />
		<param name="RollingStyle" value="Composite" />
		<layout type="log4net.Layout.PatternLayout">
			<param name="ConversionPattern" value="%date %5level  %logger.%method [%line] - %newlineMESSAGE: %message -  EXCEPTION: %exception%newline"  />
		</layout>
	</appender>
	<root>
		<level value="ALL"/>		
		<appender-ref ref="InfoAppender" />
	</root>
</log4net>

 

三、添加LogHelper类

public class LogHelp
    
        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
       
        private static object thisLock = new object();
        /// <summary>
        /// 将内容记录到info
        /// 客户端调用写法如下:LogHelp.WriteLog("123");  
        /// </summary>
        /// <param name="info"></param>
        /// <param name="realWriteLog"></param>
        public static void WriteLog(string info, bool realWriteLog = true)
        
            lock (thisLock)
            
                if (realWriteLog)
                
                    if (loginfo.IsInfoEnabled)
                    
                        loginfo.Info(info);
                    
                
            
        


        /// <summary>
        /// 将内容记录到error
        /// 客户端调用写法如下:LogHelp.WriteLog("456",new Exception ("错误"));
        /// </summary>
        /// <param name="info"></param>
        /// <param name="se"></param>
        /// <param name="realWriteLog"></param>
        public static void WriteLog(string info, Exception se, bool realWriteLog = true)
        
            lock (thisLock)
            
                if (realWriteLog)
                
                    if (loginfo.IsInfoEnabled)
                    
                        loginfo.Info(info,se);
                    
                
            
        
        /// <summary>
        /// 日志保留多少天
        /// </summary>
        /// <param name="logFilePath"></param>
        /// <param name="saveDays"></param>
        public static void SaveLogDays(string logFilePath, int saveDays)
        
            lock (thisLock)
            
                string[] logFileName = Directory.GetFiles(logFilePath);
                if (logFileName.Count() > saveDays)
                
                    File.Delete(logFileName[0]);
                
            
        
    

四、在appsetting.json中添加

"Log4NetCore": 
    "Name": "MyLog",
    "LoggerRepository": "Fantastic",
    "OverrideCriticalLevelWith": "Critical",
    "Watch": false,
    "UseWebOrAppConfig": false    
  

五、在Program.cs文件中添加

builder.Logging.AddLog4Net("log4net.config");

 

六、调用loghelp

LogHelp.WriteLog("msg");
LogHelp.WriteLog("msg",new Exception("testLog"));

输出结果展示:

 

 

 

 

以上是关于dotnet core 6.0 配置使用log4net的主要内容,如果未能解决你的问题,请参考以下文章

asp.ner core 6.0路由找不到

如何在 Entity Framework Core cli 工具中使用来自 dotnet 6 最小 API 的配置

使用appsettings.json配置Kestrel监听端口Dotnet core 2 preview 2

DotNet Core,没有为此 DbContext 配置数据库提供程序

log4N配置方式

DOCKER上运行DOTNET CORE