log4net用法
1、新建配置文件
在项目的bin文件下新建config文件:\logUtil\bin\Debug\net6.0\log4net.config文件,
2、config配置文件参考:
配置一:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!--定义输出到文件中-->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="log\\" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />
<staticLogFileName value="false" />
<param name="MaxSizeRollBackups" value="100" />
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property] %n错误描述:%message%newline %n" />
</layout>
</appender>
<root>
<level value="ALL" />
<!--文件形式记录日志-->
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
配置二:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<logger name="logerror">
<appender-ref ref="ErrorAppender" />
</logger>
<!-- 信息日志类 -->
<logger name="loginfo">
<appender-ref ref="InfoAppender" />
</logger>
<!--定义输出到文件中-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="log\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.log'"/>
<staticLogFileName value="false"/>
<!--定义文件最大个数,超过按顺序删除-->
<param name="MaxSizeRollBackups" value="100"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="50MB" />
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<conversionPattern value="%date: %message%newline %n"/>
</layout>
</appender>
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="log\\Error\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.log'"/>
<staticLogFileName value="false"/>
<!--定义文件最大个数,超过按顺序删除-->
<param name="MaxSizeRollBackups" value="100"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maximumFileSize value="50MB" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="Error" />
<levelMax value="Error" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<conversionPattern value="%date: %message%newline %n"/>
</layout>
</appender>
</log4net>
</configuration>
配置三:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<!-- 错误日志类-->
<logger name="logerror">
<level value="ALL" />
<appender-ref ref="ErrorAppender" />
</logger>
<!-- 信息日志类 -->
<logger name="loginfo">
<level value="ALL" />
<appender-ref ref="InfoAppender" />
</logger>
<!-- 错误日志附加介质-->
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\LogError\\" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".htm"" />
<param name="RollingStyle" value="Date" />
<!--布局-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
</layout>
</appender>
<!-- 信息日志附加介质-->
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log\\LogInfo\\" />
<param name="AppendToFile" value="true" />
<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd".htm"" />
<param name="RollingStyle" value="Date" />
<!-- 信息日志布局-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
</layout>
</appender>
</log4net>
</configuration>
如何使用
1、新建项目后,引用log4net的nuget包到项目中
2、将congig文件包括在项目中
3、新建log4net工具l类来写 log4net逻辑代码,注意[assembly: log4net.Config.XmlConfigurator(ConfigFile = “log4net.config”, ConfigFileExtension = “config”, Watch = true)]这个可以写在assembly.cs文件中,如果没有这串代码,文件夹中不会出现log记录文件夹
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]
namespace logUtil.Tools
{
public class Logger
{
private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
public static void WriteInfo(string info)
{
Console.WriteLine(info);
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
public static void WriteError(string error)
{
Console.WriteLine(error);
if (logerror.IsErrorEnabled)
{
logerror.Error(error);
}
}
public static void WriteError(string info, Exception ex)
{
Console.WriteLine(info);
if (logerror.IsErrorEnabled)
{
logerror.Error(info, ex);
}
}
}
}
4、调用日志工具类文章来源:https://uudwc.com/A/59AER
private static void Main(string[] args)
{
try
{
Console.WriteLine("Hello, World!");
Logger.WriteError("测试");
int num =int.Parse("测试") ;
}
catch (Exception e)
{
Logger.WriteError(e.Message);
}
}
结果图:
文章来源地址https://uudwc.com/A/59AER