Reference address:
“C# log4net configuration”
“C# use log4net to record logs”
Project reference nuget package log4net
p>
Add a reference to the App.config file
Add the following content to the configuration node
"ALL" />
ref ref="SysAppender" />
"WebLogger">
"DEBUG" />
"SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
"File" value="log/" />
"AppendToFile" value="true" />
"RollingStyle" value="Date" />
"DatePattern" value=""Logs_"yyyyMMdd".txt"< /span>" />
"StaticLogFileName" value="false" />
"log4net.Layout.PatternLayout,log4net">
"ConversionPattern" value="%d [%t] %-5p% c-%m%n" />
"consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
"log4net.Layout.PatternLayout,log4net">
"ConversionPattern" value="%d [%t] %-5p% c-%m%n" />
View Code
Complex configuration file
p>
"logerror">
"ALL" />
ref ref="ErrorAppender" />
"loginfo">
"ALL" />
ref ref="InfoAppender" />
"ErrorAppender" type="log4net.Appender.RollingFileAppender< span style="color: #800000;">">
"AppendToFile" value="true" />
"MaxSizeRollBackups" value="100" />
"MaxFileSize" value="10240" />
"StaticLogFileName" value="false" />
"DatePattern" value="yyyyMMdd".htm"< span style="color: #800000;">" />
"RollingStyle" value="Date" />
"log4net.Layout.PatternLayout">
"ConversionPattern" value="<HR COLOR=red>%nException Time: %d [%t] <BR>%nException level: %-5p <BR>%nException type: %c [%x] <BR>%n%m <BR>%n < ;HR Size=1>" />
"InfoAppender" type="log4net.Appender.RollingFileAppender< span style="color: #800000;">">
"File" value="Log\LogInfo\ " />
"AppendToFile" value="true" />
"MaxFileSize" value="10240" />
"MaxSizeRollBackups" value="100" />
"StaticLogFileName" value="false" />
"DatePattern" value="yyyyMMdd".htm"< span style="color: #800000;">" />
"RollingStyle" value="Date" />
"log4net.Layout.PatternLayout">
"ConversionPattern" value="<HR COLOR=blue>%nlog Time: %d [%t] <BR>%nLog level:%-5p <BR>%nLog type:%c [%x] <BR>%n%m <BR>%n < ;HR Size=1>" />
View Code
Add content to the AssemblyInfo.cs file
Add a help class
p>
public class LogHelper
{
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo ");
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror ");
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);
}
}
}
Call the helper class
protected void Page_Load(object sender, EventArgs e)
{
try
{
string a = "FF";
int b = Convert.ToInt32(a);
}
catch (Exception ex){
LogHelper.WriteLog(ex.Message.ToString(), ex);
}
}
"ALL" />
ref ref="SysAppender" />
"WebLogger">
"DEBUG" />
"SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
"File" value="log/" />
"AppendToFile" value="true" />
"RollingStyle" value="Date" />
"DatePattern" value=""Logs_"yyyyMMdd".txt"< /span>" />
"StaticLogFileName" value="false" />
"log4net.Layout.PatternLayout,log4net">
"ConversionPattern" value="%d [%t] %-5p% c-%m%n" />
"consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
"log4net.Layout.PatternLayout,log4net">
"ConversionPattern" value="%d [%t] %-5p% c-%m%n" />
View Code
"ALL" />
ref ref="SysAppender" />
"WebLogger">
"DEBUG" />
"SysAppender" type="log4net.Appender.RollingFileAppender,log4net">
"File" value="log/" />
"AppendToFile" value="true" />
"RollingStyle" value="Date" />
"DatePattern" value=""Logs_"yyyyMMdd".txt"< /span>" />
"StaticLogFileName" value="false" />
"log4net.Layout.PatternLayout,log4net">
"ConversionPattern" value="%d [%t] %-5p% c-%m%n" />
"consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
"log4net.Layout.PatternLayout,log4net">
"ConversionPattern" value="%d [%t] %-5p% c-%m%n" />
"logerror">
"ALL" />
ref ref="ErrorAppender" />
"loginfo">
"ALL" />
ref ref="InfoAppender" />
"ErrorAppender" type="log4net.Appender.RollingFileAppender< span style="color: #800000;">">
"AppendToFile" value="true" />
"MaxSizeRollBackups" value="100" />
"MaxFileSize" value="10240" />
"StaticLogFileName" value="false" />
"DatePattern" value="yyyyMMdd".htm"< span style="color: #800000;">" />
"RollingStyle" value="Date" />
"log4net.Layout.PatternLayout">
"ConversionPattern" value="<HR COLOR=red>%nException Time: %d [%t] <BR>%nException level: %-5p <BR>%nException type: %c [%x] <BR>%n%m <BR>%n < ;HR Size=1>" />
"InfoAppender" type="log4net.Appender.RollingFileAppender< span style="color: #800000;">">
"File" value="Log\LogInfo\ " />
"AppendToFile" value="true" />
"MaxFileSize" value="10240" />
"MaxSizeRollBackups" value="100" />
"StaticLogFileName" value="false" />
"DatePattern" value="yyyyMMdd".htm"" />
"RollingStyle" value="Date" />
"log4net.Layout.PatternLayout">
"ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
View Code
"logerror">
"ALL" />
ref ref="ErrorAppender" />
"loginfo">
"ALL" />
ref ref="InfoAppender" />
"ErrorAppender" type="log4net.Appender.RollingFileAppender">
"File" value="Log\LogError\" />
"AppendToFile" value="true" />
"MaxSizeRollBackups" value="100" />
"MaxFileSize" value="10240" />
"StaticLogFileName" value="false" />
"DatePattern" value="yyyyMMdd".htm"" />
"RollingStyle" value="Date" />
"log4net.Layout.PatternLayout">
"ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p <BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
"InfoAppender" type="log4net.Appender.RollingFileAppender">
"File" value="Log\LogInfo\" />
"AppendToFile" value="true" />
"MaxFileSize" value="10240" />
"MaxSizeRollBackups" value="100" />
"StaticLogFileName" value="false" />
"DatePattern" value="yyyyMMdd".htm"" />
"RollingStyle" value="Date" />
"log4net.Layout.PatternLayout">
"ConversionPattern" value="<HR COLOR=blue>%n日志时间:%d [%t] <BR>%n日志级别:%-5p <BR>%n日 志 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
public class LogHelper
{
public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
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);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
string a = "FF";
int b = Convert.ToInt32(a);
}
catch (Exception ex){
LogHelper.WriteLog(ex.Message.ToString(), ex);
}
}