Use of log4net

Source: Internet
Author: User
Tags log4net

1. Download Log4net.dll

2. Add a component reference to the project

3. Create a new profile in the project Log4net.config, delete the auto-generated code, and copy the following code to the file.

<?xml version= "1.0"?>

<configuration>

<startup>

<supportedruntime version= "v4.0" sku= ". netframework,version=v4.0 "/>

</startup>

<span style= "Color:rgb (102, 102, 204); ><appSettings>

<add key= "log4net. Internal.debug "value=" true "/>

</appSettings>

<log4net>

<!--A1 is set to be a consoleappender--

<appender name= "A1" type= "log4net. Appender.fileappender ">

<file value= "LogFile.txt"/>

<appendtofile value= "false"/>

<!--A1 uses Patternlayout--

<layout type= "log4net. Layout.patternlayout ">

<conversionpattern value= "%-4timestamp [%thread]%-5level%logger%ndc-%message%newline"/>

</layout>

</appender>

<!--Set Root logger level-to-DEBUG and its-only appender to A1-

<root>

<level value= "DEBUG"/>

<appender-ref ref= "A1"/>

</root>

</log4net>

<configSections>

<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>

</configSections></span>

</configuration>

4. Add in the program class (note that it is added on namespace)

[Assembly:log4net. Config.xmlconfigurator (Watch = True)]

private static log4net. ILog log = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);

5. And in the execution of the program, add

Log. Debug ("Hello");

After running, the LogFile.txt will be generated in the bin directory

You can also customize a static class

Namespace Attendance

{

STATUC class Log4net.ilog staticlog=log4net. Logmanger.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);

public static log4net. ILog Log

{

Get

{

return staticlog;

}

Set

{

}

}

}

Call Staticlog in the method.  Warn (""); Staticlog.  Error (""); Staticlog. Info ("");

Staticlog.debug (""); Staticlog.  Off (""); Staticlog. Fatal ("");

    1. <!--log level:
    2. Level allowed methods Boolean property precedence level
    3. OFF Highest
    4. FATAL void FATAL (...); BOOL isfatalenabled;
    5. rror void Error (...); BOOL iserrorenabled;
    6. WARN void WARN (...); BOOL iswarnenabled;
    7. info void info (...); BOOL isinfoenabled;
    8. debug void Debug (...); BOOL isdebugenabled;
    9. All Lowest

Ten.-->

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.