Log4net Configuration Lessons

Source: Internet
Author: User

1th: Permission issues, be sure to give the directory loose permissions to write to the log file.
I just wasted a lot of time because of the privilege problem. (Because the permissions of IIS user are relatively low, log4net needs to create files, so permissions must be given, followed by application pool configuration)

I would like to use a separate profile, so the basic steps are as follows:
1: Download Net4log,
The specific address will not be affixed to an official website of the configuration Instructions page:

Http://logging.apache.org/log4net/release/manual/configuration.html

2: Using the corresponding version of the DLL,

       Be sure to use the corresponding version of, I just because the version does not correspond to waste time
       2.0 is to choose 2.0,4.0 4.0

3: Introducing DLLs into the project

4: The benefit of using a separate profile-modifying the log4net configuration does not affect the entire application reboot. My path is:
Configure directory, Log4netconfig.xml file
Configure\log4netconfig.xml
Specific content: I can see the corresponding description

<?xml version= "1.0" encoding= "Utf-8"?> <configuration> <configSections> <section name= "log4net "Type=" log4net. Config.log4netconfigurationsectionhandler, log4net "/> </configSections> <!--logging build configuration--> <log4net > <!--console section log output format set--> <appender name= "Consoleappender" type= "log4net". Appender.consoleappender "> <layout type=" log4net. Layout.patternlayout "> <conversionpattern value="%date [%thread]%-5level%logger%NDC-%message%newline "/  > </layout> </appender> <!--log file part log output format set--> <appender name= "Errorappender" Type= "Log4net. Appender.rollingfileappender "> <filter type=" log4net. Filter.levelmatchfilter "> <leveltomatch value=" ERROR "/> </filter> <filter type=" log 4net.  Filter.denyallfilter "/> <file value= logs\\err.log"/> <preservelogfilenameextension "true"
  />    <appendtofile value= "true"/> <rollingstyle value= "Date"/> <datepattern "value="/ > <layout type= "log4net.
      Layout.patternlayout "> <conversionpattern value="%date [%thread]%-5level%logger-%message%newline "/>" </layout> </appender> <appender name= "Infoappender" type= "log4net. Appender.rollingfileappender "> <filter type=" log4net. Filter.levelmatchfilter "> <leveltomatch value=" INFO "/> </filter> <filter type=" Log4 Net. Filter.denyallfilter "/> <file value=" Logs\\info.log "/> <preservelogfilenameextension value=" true "/> <appendtofile value=" true "/> <rollingstyle value=" Date "/> <datepattern value=" y Yyymmdd "/> <layout type=" log4net.
      Layout.patternlayout "> <conversionpattern value="%date [%thread]%-5level%logger-%message%newline "/>"
</layout>    </appender> <appender name= "Debugappender" type= "log4net". Appender.rollingfileappender "> <filter type=" log4net. Filter.levelmatchfilter "> <leveltomatch value=" DEBUG "/> </filter> <filter type=" log 4net. Filter.denyallfilter "/> <file value=" Logs\debug.log "/> <preservelogfilenameextension value=" true "/> <appendtofile value=" true "/> <rollingstyle value=" Date "/> <datepattern value=" y Yyymmdd "/> <layout type=" log4net.
      Layout.patternlayout "> <conversionpattern value="%date [%thread]%-5level%logger-%message%newline "/>" </layout> </appender> <appender name= "Perfappender" type= "log4net. Appender.rollingfileappender "> <filter type=" log4net. Filter.levelmatchfilter "> <leveltomatch value=" INFO "/> </filter> <filter type=" Log4 Net. Filter.denyallfilter "/> &LT File value= "Logs\perf.log"/> <preservelogfilenameextension value= "true"/> <appendtofile value= "t Rue "/> <rollingstyle value=" Date "/> <datepattern value=" yyyyMMdd "/> <layout type=" l Og4net. Layout.patternlayout "> <conversionpattern value="%date%logger-%message%newline "/> &LT;/LAYOUT&G
    T </appender> <appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender "> <file value=" logs\log_ "/> <appendtofile value=" true "/> & Lt;rollingstyle value= "Date"/> <datepattern value= "yyyyMMdd '. txt '"/> <staticlogfilename-value= "f Alse "/> <layout type=" log4net. 
        Layout.patternlayout ">  

5: Modify the AssemblyInfo.cs
For log4net to be able to read the configuration file. Specify the path to the configuration file

Defines the log4net configuration file path
//[assembly:log4net. Config.domconfigurator (configfile = @ "Configure\log4netconfig.xml", Watch = True)]
//can also be used on the above statement, above is the old way
[ Assembly:log4net. Config.xmlconfiguratorattribute (configfile = @ "Configure\log4netconfig.xml", Watch = True)]

6: New directory to store the configuration file, * * and authorized to write is very important

**

7: Using in code:
First you define in the class body:

DataHandler is the name of the class, used for reflection, more search is known, the use of time to replace, logger can also be changed, after the use of the time to remember the good.
 private static readonly ILog logger = Logmanager.getlogger (typeof (DataHandler));
You can use it in the location you want to use. The exact following is the corresponding to the configuration file, as in parentheses is the string to be output. Debug is the log level to indicate what level of logging the log is, and then configure the appropriate level within Appender in the configuration file to write the content to the appropriate file.
 logger. Debug ("Debug Test");

End to see if there are any files in the corresponding directory.

The

log4net architecture is divided into four elements: Logger;appender;layout;filter.
Logger is the logger responsible for the log
Appender the media that provides the record
layout is responsible for formatting the entries
filter is responsible for filtering the content
You can say that the whole process is a log line, Each member is responsible for one of the links.
Logger sends record information, Appender to the information, formats the record information according to the internal layout configuration, determines whether this information is filtered out according to the filter, and serializes it at last
Log4net Specific idea: logger (generate)->appender (receive)-> filter log (filter)->layout serialized output Appender record media through profile instantiation

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.