log4net的配置详解

log4net是Apache的C#日志系统,下面是详细配置:

一,用Nuget安装log4net:

1

2

二,修改App.config文件,添加配置项,下面是完整的配置文件内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
	</configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
	<log4net>
		<logger name="logerror">
			<level value="ERROR" />
			<appender-ref ref="ErrorAppender" />
		</logger>
		<logger name="loginfo">
			<level value="INFO" />
			<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" />
			<datePattern value="yyyyMM\\yyyyMMdd'_Demo.txt'" />
			<param name="RollingStyle" value="Date" />
			<layout type="log4net.Layout.PatternLayout">
				<param name="ConversionPattern" value="%n异常时间:%d %n异常级别:%-5p%n异常内容:%m%n" />
			</layout>
			<!--< > = <> %n = 回车-->
		</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" />
			<datePattern value="yyyyMM\\yyyyMMdd'_Demo.txt'" />
			<param name="RollingStyle" value="Date" />
			<layout type="log4net.Layout.PatternLayout">
				<param name="ConversionPattern" value="日志时间:%d %n日志级别:%-5p  %n日志内容:%m%n%n" />
			</layout>
		</appender>
	</log4net>
</configuration>

注意:这个配置项必须加入,而且必须放到第一个位置,否则会报错!

	<configSections>
		<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
	</configSections>

 三,在AssemblyInfo.cs文件中注册,在文件末尾加入:

//为项目注册Log4Net.config配置文件
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]

四,工程里面创建日志类,就可以使用了:

public class LogHelper
{
    private LogHelper()
    {

    }

    public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");

    public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");

    public static void SetConfig()
    {
        log4net.Config.XmlConfigurator.Configure();
    }

    public static void SetConfig(FileInfo configFile)
    {
        log4net.Config.XmlConfigurator.Configure(configFile);
    }

    public static void WriteLog(string info)
    {
        if (loginfo.IsInfoEnabled)
        {
            loginfo.Info(info);
        }
    }

    public static void WriteLog(string info, Exception se)
    {
        if (logerror.IsErrorEnabled)
        {
            logerror.Error(info, se);
        }
    }
}

参考:

https://www.cnblogs.com/Liyuting/p/8417757.html

https://blog.csdn.net/m0_46378049/article/details/115000379

 

posted on 2024-06-02 11:50  sunylat  阅读(53)  评论(0编辑  收藏  举报