log4net Quick Start

Keywords: ASP.NET xml encoding

Original address: https://www.cnblogs.com/lsgsanxiao/p/5845300.html

Slight deletion


1. configuration files, you can create a log4net.config file separately, then specify the directory manually, or insert the following code under the app.config or web.config node of the project

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<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">
<!--Setting Log Storage Path-->
<param name="File" value="Data//WebLog//LogError//" />
<!--Does it add to the file?-->
<param name="AppendToFile" value="true" />
<!--The maximum number of log files generated, if exceeded, only the latest n One. Set value value="-1"For unlimited number of files-->
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="1024" />
<!--Is it written to only one file?-->
<param name="StaticLogFileName" value="false" />
<!--This is a folder generated by date, with a date before the file name.-->
<param name="DatePattern" value="yyyyMM/dd&quot;.log&quot;" />
<!--How to generate multiple log files(date[Date],file size[Size],blend[Composite])-->
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n Abnormal time:%d [%t] %n Exception level:%-5p %n Abnormal location:[%thread] (%file:%line) %n Message Description:%message%n Exception:%exception%n%n " />
</layout>
</appender>
<appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
<!--Setting Log Storage Path-->
<param name="File" value="Data//WebLog//LogInfo//" />
<!--Does it add to the file?-->
<param name="AppendToFile" value="true" />
<!--The maximum number of log files generated, if exceeded, only the latest n One. Set value value="-1"For unlimited number of files-->
<param name="MaxSizeRollBackups" value="100" />
<param name="MaxFileSize" value="1024" />
<!--Is it written to only one file?-->
<param name="StaticLogFileName" value="false" />
<!--This is a folder generated by date, with a date before the file name.-->
<param name="DatePattern" value="yyyyMM/dd&quot;.log&quot;" />
<!--How to generate multiple log files(date[Date],file size[Size],blend[Composite])-->
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%n Log time:%d [%t] %n Log level:%-5p %n Message Description:%c [%x] %n%m %n " />
</layout>
</appender>
</log4net>
</configuration>

 

2. Read configuration
Add read configuration code to the project startup file, say Program.cs or Global.cs, etc.
If you uninstall the project configuration file directly, read it in this way

log4net.Config.XmlConfigurator.Configure();

If the log4net.config file is written separately, you need to pass the file path to the Configure() method

var fi = new System.IO.FileInfo(path);
log4net.Config.XmlConfigurator.Configure(fi);

3. help class

/// <summary> 
/// LogHelper A summary description. 
/// </summary> 
public class LogHelper {
/// <summary>
/// Static Read-Only Entity Objects info information
/// </summary>
public static readonly log4net.ILog Loginfo = log4net.LogManager.GetLogger("loginfo");
/// <summary>
/// Static Read-Only Entity Objects error information
/// </summary>
public static readonly log4net.ILog Logerror = log4net.LogManager.GetLogger("logerror");

/// <summary>
/// Add to info information
/// </summary>
/// <param name="info">Custom Log Content Description</param>
public static void WriteLog(string info) {
try {
if (Loginfo.IsInfoEnabled) {
Loginfo.Info(info);
}
} catch { }
}


/// <summary>
/// Adding exception information
/// </summary>
/// <param name="info">Custom Log Content Description</param>
/// <param name="ex">Abnormal information</param>
public static void WriteLog(string info, Exception ex) {
try {
if (Logerror.IsErrorEnabled) {
Logerror.Error(info, ex);
}
} catch { }
}
}

Posted by bznutz on Sun, 06 Oct 2019 20:08:13 -0700