Log4net is the .net version of Apache log4j. It is a set of XML configuration log engines. Recently, there happened to be a program that required a log system. For the convenience of integration, I decided to study it. Thanks to Hao Wei for the recommendation.
The log4net system is divided into 7 log levels. If set to "WARN", DEBUG and INFO will not be recorded. As follows:
1. ALL
2. DEBUG
3. INFO
4. WARN
5. ERROR
6. FATAL
7. OFF
Creating the Sample Application
Step 1: Download log4net
and go to logging.apache.org/log4net to get their latest version. In the example I used incubating-log4net-1.2.9-beta.zip, I look forward to the official version.
Step 2: Create an ASP.NET Web application.
I won’t go into details about creating this, as shown in log4net-1.2.0-beta8binnet. Find the corresponding dll and add it to the reference. Some versions do not have a compiled dll, so compile it yourself and use the .NET V1.0 version to convert it to 1.1 or 2.0. I think this is not a work error, but intentional. Many people who are used to playing Java are like this.
Configuring the Sample Application
Step 3: Add Assembly information.
Open the AssemblyInfo.cs file and add:
[assembly: log4net.Config.DOMConfigurator()]
This sentence means that the log4net system will automatically look for the configuration file Web.config to obtain and load the configuration information. If you want log4net to monitor the configuration file at any time for reloading, it will be a little more troublesome. You need to write like this:
[assembly:log4net.Config.DOMConfigurator(ConfigFile="filename",ConfigFileExtension="log4net",Watch=true)]
Step 4: To add configuration information
, you need to edit the Web.config file. I am really surprised every time I see it. Find the configuration tag, and write configSections immediately below it (without considering others at all, what if someone is as domineering as him? Then just write its log configuration file! I guess APACHE is like this think). Take a look at an example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net-net-1.0"
/>
</configSections >
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type=" log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
< /layout>
</appender>
<!-- RollingFileAppender looks after rolling over files by size or date -->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value= "C:\log\RollingLogHelloWorld.log" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value ="1000" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name= "ConversionPattern" value="%d [%t] %-5p %-45c [%x] - %m%n" />
</layout>
</appender>
<!-- FileAppender appends to a log and it is manually managed or size -->
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="LogHelloWorld.log" />
<!-- Example using environment variables in params -->
<!-- <param name="File" value="${TMP}\ApplicationKit.log" /> -->
<param name="AppendToFile" value="true" />
<layout type= "log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
< !-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
<!-- ApplicationKit category - the presentation UI -->
<logger name="WebForm1">
<level value="INFO" />
<appender-ref ref="FileAppender" />
</logger>
</log4net>
</configuration>
Note that the NAME attribute of the Logger node is set to WebForm1, which refers to the class name. Add such a node to each class that needs to add LOG. I think of the OSWORKFLOW workflow. Flexibility and convenience and practicality often cannot be achieved at the same time. The level here is INFO, that is to say, anything below INFO will not be recorded.
Running the Sample Application
Step 5: To add a Log class,
first add a reference
using log4net; using log4net.Config; //Then instantiate. If you study the configuration file carefully, then this declaration method is easy to understand: private static readonly ILog log = LogManager. GetLogger("WebForm1");
Step 6: Using the Log object
. Now, all the configuration work has been completed. The next step is to test it and connect it to the application layer. Just add a button in the response event Button1_Click event handler.
log.Info("Hello World, I am a logger");
Step 7: Run the Application
Now it can be run. It should look like this:
Every time you click the button,2006-02-13 10:12:30,671 [1228] INFO WebForm1 [] - Hello
will appear in the date file.
World, I am a logger is such a record.
Summary:
It’s simple and easy to use. Let’s study the SQL log after a while. DB is the best, haha.
For more information see http://logging.apache.org/log4net/