Logging helps us quickly troubleshoot, and here's a simple procedure for logging, as an example of a simple MVC program that describes
1. Create a simple MVC program First
2. introduction of Log4net.dll in the project
3. Add TestLogNet4 class
Using System;
Using System.Collections.Generic;
Using System.Configuration;
Using System.Linq;
Using System.Web;
Using System.Web.Configuration;
[Assembly:log4net. Config.xmlconfigurator (configfile = "Logger/log4net.config", configfileextension = "config", Watch = True)]
Namespace WebApplication1
{
public class TestLogNet4
{
Information Log Storage folder
private static readonly log4net. ILog loginfo = log4net. Logmanager.getlogger ("Loginfo");
Error Log Store folder
private static readonly log4net. ILog logerror = log4net. Logmanager.getlogger ("Logerror");
<summary>
Record Log
</summary>
<param name= "message" > log Messages </param>
public static void Writelog (String message)
{
if (loginfo. isinfoenabled)
{
if (message! = NULL && message. Length > 0)
{
Loginfo. Info (message);
}
}
}
}
}
4. Add Logger, subfolders (LogError ,loginfo), and configuration files log4net.config
The 5.log4net.config configuration is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<log4net>
<!-- error log class --
<logger name= "Logerror" >
<level value= "All"/>
<appender-ref ref= "Errorappender"/>
</logger>
<!-- Information Log class --
<logger name= "Loginfo" >
<level value= "All"/>
<appender-ref ref= "Infoappender"/>
</logger>
<!-- error log Attach media --
<appender name= "Errorappender" type= "log4net. Appender.rollingfileappender ">
<param name= "File" value= "logger\\logerror\\"/>
<param name= "Appendtofile" value= "true"/>
<param name= "maxsizerollbackups" value= "/>"
<param name= "MaxFileSize" value= "10240"/>
<param name= "Staticlogfilename" value= "false"/>
<param name= "Datepattern" value= "yyyyMMdd". txt ""/>
<param name= "Rollingstyle" value= "Date"/>
<!-- layout --
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "
</layout>
</appender>
<!-- Information log Attach media --
<appender name= "Infoappender" type= "log4net. Appender.rollingfileappender ">
<param name= "File" value= "logger\\loginfo\\"/>
<param name= "Appendtofile" value= "true"/>
<param name= "MaxFileSize" value= "1"/>
<param name= "maxsizerollbackups" value= "/>"
<param name= "Staticlogfilename" value= "false"/>
<param name= "Datepattern" value= "yyyyMMdd". txt ""/>
<param name= "Rollingstyle" value= "Date"/>
<!-- Information log layout --
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "
</layout>
</appender>
</log4net>
The 6.webtest.aspx call is as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace WebApplication1
{
public partial class WebTest:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Testlognet4.writelog ("Test test test");
}
}
}
7. After running the WEBTEST.ASHX page in the program, check to see if the log files are generated in Loginfo under the Logger folder under the project, in the same text file as the same day.
Log text record