.net 5.0 注入中间件方式
引用
- log4net
- Microsoft.Extensions.Logging.Log4net.AspNetCore
log4.config【始终复制】
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<!--指定日记记录方式,以滚动文件的方式(文件记录)-->
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<!--日志路径-->
<file value="Log\message" />
<!--是否是向文件中追加日志-->
<appendToFile value="true" />
<!--log保留天数-->
<param name= "MaxSizeRollBackups" value= "10"/>
<!--每个文件最大1M-->
<param name="maximumFileSize" value="1MB" />
<!--日志根据日期滚动-->
<param name="RollingStyle" value="Date" />
<!--日志文件名格式为:logs_20080831.log-->
<param name="DatePattern" value=""logs_"yyyyMMdd".log"" />
<!--日志文件名是否是固定不变的-->
<param name="StaticLogFileName" value="false" />
<!--布局-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFile" />
</root>
</log4net>
Program.cs
使用方式
控制台应用程序
引用包
log4net.config【始终复制】
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Level的级别,由高到低 -->
<!-- None > Fatal > ERROR > WARN > DEBUG > INFO > ALL-->
<!-- 解释:如果level是ERROR,则在cs文件里面调用log4net的info()方法,则不会写入到日志文件中-->
<log4net>
<!--错误日志类-->
<logger name="error">
<!--定义记录的日志级别-->
<level value="ALL" />
<!--输出到文件-->
<appender-ref ref="ErrorAppender" />
<!--输出到控制台-->
<appender-ref ref="ConsoleAppender"/>
</logger>
<!--信息日志类-->
<logger name="info">
<level value="ALL" />
<appender-ref ref="InfoAppender" />
<appender-ref ref="ConsoleAppender"/>
</logger>
<!--调试日志类-->
<logger name="debug">
<level value="ALL" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="ConsoleAppender"/>
</logger>
<!--错误日志附加介质:输出到文件-->
<!-- name属性指定其名称,type则是log4net.Appender命名空间的一个类的名称,意思是,指定使用哪种介质-->
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<!--日志输出到exe程序这个相对目录下-->
<file value="Log\\Error\\" />
<!--输出的日志不会覆盖以前的信息-->
<appendToFile value="true" />
<!--备份文件的个数-->
<maxSizeRollBackups value="100" />
<!--单个日志文件的最大大小-->
<maxFileSize value="10240" />
<!--是否使用静态文件名-->
<staticLogFileName value="false" />
<!--日志文件名-->
<datePattern value="yyyyMMdd'.txt'" />
<!--文件创建的方式,这里是以Date方式创建-->
<rollingStyle value="Date" />
<!--错误日志布局-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [Thread: %thread] %newline%message %newline"/>
</layout>
</appender>
<!--信息日志附加介质:输出到文件-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<!--日志目录-->
<file value="Log\\Info\\" />
<appendToFile value="true" />
<maxFileSize value="10240" />
<maxSizeRollBackups value="100" />
<!--日志文件名是否为静态-->
<staticLogFileName value="false" />
<datePattern value="yyyyMMdd'.txt'" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [Thread: %thread] %message %newline"/>
</layout>
</appender>
<!--调试日志附加介质:输出到文件-->
<appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
<file value="Log\\Debug\\" />
<appendToFile value="true" />
<maxFileSize value="10240" />
<maxSizeRollBackups value="100" />
<staticLogFileName value="false" />
<datePattern value="yyyyMMdd'.txt'" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [Thread: %thread] %message %newline"/>
</layout>
</appender>
<!--日志附加介质:输出到控制台-->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<!--<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="White" />
<backColor value="Green" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="White" />
--><!--<backColor value="Yellow" />--><!--
</mapping>-->
<layout type="log4net.Layout.PatternLayout">
<!--%data格式为yyyy-MM-dd HH:mm:ss,sss-->
<conversionPattern value="%date{yyyy'-'MM'-'dd HH':'mm':'ss} [%-5level] %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="Debug"/>
<param name="LevelMax" value="Fatal"/>
</filter>
</appender>
</log4net>
</configuration>
控制台:LogHelper.cs
using log4net;
using log4net.Config;
using log4net.Repository;
using System.IO;
public class LogHelper
{
private static ILoggerRepository repository { get; set; }
private static ILog _logInfo;
private static ILog _logError;
private static ILog _logDebug;
private static ILog logInfo
{
get
{
if (_logInfo == null)
{
Configure();
}
return _logInfo;
}
}
private static ILog logError
{
get
{
if (_logError == null)
{
Configure();
}
return _logError;
}
}
private static ILog logDebug
{
get
{
if (_logDebug == null)
{
Configure();
}
return _logDebug;
}
}
public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "log4net.config")
{
repository = LogManager.CreateRepository(repositoryName);
XmlConfigurator.Configure(repository, new FileInfo(configFile));
_logInfo = LogManager.GetLogger(repositoryName, "info");
_logError = LogManager.GetLogger(repositoryName, "error");
_logDebug = LogManager.GetLogger(repositoryName, "debug");
}
public static void Info(string msg)
{
logInfo.Info(msg);
}
public static void Debug(string msg)
{
logError.Debug(msg);
}
public static void Error(string msg)
{
logError.Error(msg);
}
}