Log text record

Source: Internet
Author: User
Tags log4net

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.