Using Log4net ABC in Web projects

Source: Internet
Author: User
Tags log4net
Read the n log4net tutorial, although a lot of it is introduced in the Web project using Log4net, but none of the experiment was successful.
Dizzy death. Studied for half an hour, finally finished.
1. Modify AssemblyInfo.cs
[Assembly:log4net. Config.xmlconfigurator (configfile= "Log4net.config", Watch=true)]
2. Modify Web.config
<configuration>
<configSections>
<section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler, log4net "/>
</configSections>
<system.web>
... System Auto-generated configuration
</system.web>
</configuration>
3. Create a new class Logger.cs
public class Logger
{
private static log4net. ILog Log=null;
private static Object Lockhelper = new Object ();
public static log4net. ILog Log
{
Get
{
if (log = = null)
{
Lock (Lockhelper)
{
if (log==null)
Log = Log4net. Logmanager.getlogger ("logger");
}
}
return log;
}
}
}
4. New Log4net.config
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<log4net debug= "false" >
<appender name= "Logfileappender" type= "log4net". Appender.fileappender ">
<file value= "Log.txt"/>
<appendtofile value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%NDC]-%message%newline"/>
</layout>
</appender>
<root>
<level value= "All"/>
<appender-ref ref= "Logfileappender"/>
</root>
</log4net>
</configuration>
5. Calling methods
Logger.Log.Debug ("Hello");
6. Make sure that the Web folder has write permissions.
7.OK

When you have problems using log4net, set up in Log4net.config
<log4net debug= "true" >
This will enable you to see the error message.
At first I did not set the Write permission for the file, so the use did not succeed. Then I turned on debug and I saw the error message, and it was done. So when you have problems using log4net, be sure to turn on debug and look at the error message, which is very useful.

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.