Log4net

Reference address:

“C# log4net configuration”

“C# use log4net to record logs”

Project reference nuget package log4net

share picture

p>

Add a reference to the App.config file

Add the following content to the configuration node

Share a picture

 




"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>

share picture




"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

Share Picture

Add content to the AssemblyInfo.cs file

share Picture

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);
}
}

share picture share picture

 




"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" />


Share a picture




"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);
}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3086 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.