其他配置详情在网上都可以找到,但是很多看着都晕,本人就记录一下如何使用:
1、按不同级别(官方说明)可记录的日志级别有:
Info、Warn、Error、Debug
2、可以按着四个配置四个输出日志路径和配置文件的配置节点
可以在filter节点中配置value来区别记录日志的级别
例如:
<appender name="Errorlog" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="Errorlog\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value=""/>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明
输出格式
样例:-- ::, [] INFO Log4NetDemo.MainClass [(null)] - info
<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->
<conversionPattern value="%date 描述:%message%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="ERROR"/>
</filter>
</appender>
然后可以根据自己需要配置不同的输出路径,注意filter里的param节点的value必须大写并只能是四种节点的一种value
最终可以在logger节点中配置指向appender
<logger name="myLogger">
<level value="ALL"/>
<appender-ref ref="Errorlog"/>
<appender-ref ref="Runlog"/>
<appender-ref ref="Warnlog"/>
<appender-ref ref="Debuglog"/>
<!--<appender-ref ref="ErrorLoging" />-->
</logger>
最后附上配置文件以及LogHelper类:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<!--定义输出到文件中-->
<appender name="Errorlog" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="Errorlog\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value=""/>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明
输出格式
样例:-- ::, [] INFO Log4NetDemo.MainClass [(null)] - info
<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->
<conversionPattern value="%date 描述:%message%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="ERROR"/>
</filter>
</appender>
<appender name="Runlog" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="Runlog\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value=""/>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明
输出格式
样例:-- ::, [] INFO Log4NetDemo.MainClass [(null)] - info
<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->
<conversionPattern value="%date 描述:%message%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<appender name="Warnlog" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="Warnlog\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value=""/>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明
输出格式
样例:-- ::, [] INFO Log4NetDemo.MainClass [(null)] - info
<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->
<conversionPattern value="%date 描述:%message%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN"/>
<param name="LevelMax" value="WARN"/>
</filter>
</appender>
<appender name="Debuglog" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="Debuglog\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value=""/>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明
输出格式
样例:-- ::, [] INFO Log4NetDemo.MainClass [(null)] - info
<conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>-->
<conversionPattern value="%date 描述:%message%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="DEBUG"/>
</filter>
</appender>
<logger name="myLogger">
<level value="ALL"/>
<appender-ref ref="Errorlog"/>
<appender-ref ref="Runlog"/>
<appender-ref ref="Warnlog"/>
<appender-ref ref="Debuglog"/>
<!--<appender-ref ref="ErrorLoging" />-->
</logger>
<!--<root>
<level value="ALL"/>
-->
<!--文件形式记录日志-->
<!--
<appender-ref ref="SuoerServerLog"/>
<appender-ref ref="PDFWatcherLog"/>
</root>-->
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; [assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
namespace DotNet.Utilities.Log
{
public class LogHelper
{
//配置loger指向
static log4net.ILog log = log4net.LogManager.GetLogger("myLogger"); /// <summary>
/// 输出日志到Log4Net
/// </summary>
/// <param name="t"></param>
/// <param name="msg"></param>
#region Error节点 public static void WriteErrorLog(string msg)
{
//log4net.ILog log = log4net.LogManager.GetLogger(t);
log.Error(msg);
} #endregion #region Info节点 public static void WriteInfoLog(string msg)
{
//log4net.ILog log = log4net.LogManager.GetLogger(t);
log.Info(msg);
} #endregion #region Debug节点 public static void WriteDebugLog(string msg)
{
//log4net.ILog log = log4net.LogManager.GetLogger(t);
log.Debug(msg);
} #endregion #region Warn节点 public static void WriteWarnLog(string msg)
{
//log4net.ILog log = log4net.LogManager.GetLogger(t);
log.Warn(msg);
} #endregion }
}