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 ("");
- <!--log level:
- Level allowed methods Boolean property precedence level
- OFF Highest
- FATAL void FATAL (...); BOOL isfatalenabled;
- rror void Error (...); BOOL iserrorenabled;
- WARN void WARN (...); BOOL iswarnenabled;
- info void info (...); BOOL isinfoenabled;
- debug void Debug (...); BOOL isdebugenabled;
- All Lowest
Ten.-->