->Log4Net is a .Net class library that can help programmers output log information to various targets
First add the NuGet package to the project:
then search for Log4Net in the NuGet form, then click Install <The installation process may take a few minutes, please be patient>
Configuration file App.Config or Web.Config
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!--Log4Net块配置-->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<!--log4net配置-->
<log4net>
<!--定义日志载入器-->
<logger name="logerror">
<level value="DEBUG" />
<!--关联日志追加者:ErrorAppender-->
<appender-ref ref="ErrorAppender" />
</logger>
<logger name="loginfo">
<level value="INFO" />
<!--关联日志追加者:InfoAppender-->
<appender-ref ref="InfoAppender" />
</logger>
<!--//OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL-->
<!--追加者ErrorAppender(针对ERROR级别错误日志)-->
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<!--指定日志文件名路径-->
<param name="File" value="ErrorFile.txt" />
<!--是否追加文件-->
<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".txt"" />
<!--回滚格式(方式)-->
<param name="RollingStyle" value="Date" />
<!--日志输出格式-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="异常时间:%d %n异常级别:%-5p%n异常内容:%m%n" />
</layout>
<!--< > = <> %n = 回车-->
</appender>
<!--追加者InfoAppender(针对Info级别错误日志)-->
<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".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>
Finally add the log category:"
public class LogHelper
{
public class LogHelper
{
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");//这里的 loginfo 和 log4net.config 里的名字要一样
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");//这里的 logerror 和 log4net.config 里的名字要一样
public static void WriteLog(string info)
{
if (loginfo.IsInfoEnabled)
{
loginfo.Info(info);
}
}
public static void WriteLog(string info, Exception ex)
{
if (logerror.IsErrorEnabled)
{
logerror.Error(info, ex);
}
}
}
}