Almost all large applications have their own APIs for tracking and debugging. Once a program is deployed, it is unlikely that special debugging tools will be used. However, an administrator may need a powerful log system to diagnose and fix configuration problems. Log4net is a well-known open-source component for logging. Its basic framework is derived from log4j, another famous sister component in the Java field. Log4net logs are very powerful. It can output logs to different media according to different levels and different styles. Use an instance to describe how to use Log4net: 1. Download The Log4net component:
Http://logging.apache.org/log4net/download.html
Http://archive.apache.org/dist/incubator/log4net/1.2.10/incubating-log4net-1.2.10.zip 2, decompress the Log4net component dll: incubating-log4net-1.2.10.zip \ log4net-1.2.10 \ bin \ net \ 2.0 \ release:
Log4net. xml
Log4net. dll 3, set log4net. dll copy. under the. Net project directory: bin/debug; 4, set log4net. add dll to project reference. Add method: Right-click the reference under the VS tool solution project and select log4net. dll OK; 5. Create the log4net configuration file log4net under bin/debug. xml. The file content is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net-net-1.0"/>
</ConfigSections>
<Log4net>
<! -- Log Level:
Method Boolean attribute priority 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
-->
<Root>
<Level value = "WARN"/>
<Appender-ref = "LogFileAppender"/>
<Appender-ref = "leleappender"/>
</Root>
<Logger name = "com. suntom">
<Level value = "DEBUG"/>
</Logger>
<! -- Log output formatting:
% M (message): the output Log message, such as ILog. Debug (...) Output Message
% N (new line): line feed
% D (datetime): time when the current statement is output
% R (run time): number of milliseconds consumed by the output program from running to executing to the current statement
% T (thread id): ID of the thread where the current statement is located
% P (priority): Current Log priority level, namely DEBUG, INFO, WARN... And so on
% C (class): name of the current log object, for example:
Mode string: %-10c-% m % n
Code:
ILog log = LogManager. GetLogger ("Exam. Log ");
Log. Debug ("Hello ");
The output is in the following format:
Exam. Log-Hello
% L: the row number of the output statement
% F: File Name of the output statement
%-Number: indicates the minimum length of the item. If not, fill it with spaces.
-->
<Appender name = "LogFileAppender" type = "log4net. Appender. FileAppender">
<Param name = "File" value = "./log.txt"/>
<Param name = "AppendToFile" value = "true"/>
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "Header" value = "[Header] \ r \ n"/>
<Param name = "Footer" value = "[Footer] \ r \ n"/>
<Param name = "ConversionPattern" value = "% d [% t] %-5 p % c-% m % n"/>
</Layout>
<Filter type = "log4net. Filter. LevelRangeFilter">
<Param name = "LevelMin" value = "DEBUG"/>
<Param name = "LevelMax" value = "FATAL"/>
</Filter>
</Appender>
<Appender name = "leleappender" type = "log4net. Appender. ConsoleAppender">
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "ConversionPattern" value = "% d [% t] %-5 p % c [% x]-% m % n"/>
</Layout>
</Appender>
</Log4net>
</Configuration>
6. Configure Log4net in the project. The configuration method is as follows: In AssemblyInfo. cs (the Web Project is web. config) Add at the bottom: [assembly: log4net. config. xmlConfigurator (ConfigFile = ". /log4net. xml ", ConfigFileExtension =" xml ", Watch = true)] 7. Use Log4net in the project, such as com. suntom. testClass:
Log4net. ILog log = log4net. logManager. getLogger (this. getType (); log. debug ("Enter initMenus ()"); 8. Start the program and run it to the log output. After exiting the program, you can view the log file log.txt in bin/debug: [Header] \ r \ n2011-07-04 22:38:58, 226 [10] DEBUG com. suntom. testClass-Enter initMenus ()
[Footer] 9. Here, Log4net can be used.
This article is from the "little he Beibei's technical space" blog, please be sure to keep this http://babyhe.blog.51cto.com/1104064/602748