ViewArticleIndex Via http://www.cnblogs.com/seesea125/archive/2012/04/17/2453256.html
Ibatis. NET provides convenient log processing. You can reference ibatisnet. Common. Logging. log4net. DLL to write logs. Of course, ibatisnet. Common. dll must be referenced.
Ibatis. Net does not need to be written to process logs.CodeTo achieve this, you only need to configure the log4net configuration file log4net. config. (This file is usually stored in the root directory of the site. Of course, it can be stored in other directories.ProgramAdd monitoring to the application_start method of Global. Asa at runtime)
Step 1: Add monitoring to the application_start method when the program is running:
// Configureandwatch: Add log4net. config monitoring and modify the configuration in time when you modify log4net. config.
Log4net. config. xmlconfigurator. configureandwatch (New fileinfo (appdomain. currentdomain. setupinformation. applicationbase + "log4net. config "));
Step 2: Configure log4net. config with the following annotations added:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net"
Type = "log4net. config. log4netconfigurationsectionhandler, log4net, version = 1.2.10.0, culture = neutral, publickeytoken = bf100aa01a5c2784"/>
</Configsections>
<Deleetask>
<Add key = "log4net. Internal. debug" value = "true"/>
</Appsettings>
<! -- Each <appender> subnode represents a log recording method -->
<Log4net>
<! -- This log is used to write logs elsewhere in the system -->
<Appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender">
<File value = "./logs \ log _"/>
<Appendtofile value = "true"/>
<Rollingstyle value = "date"/>
<Datepattern value = "yyyymmdd'.txt '"/>
<Staticlogfilename value = "false"/>
<Layout type = "log4net. layout. patternlayout">
<Header value = "------------------------------------------------------------ & # XD; & # XA;"/>
<Conversionpattern value = "% date [% thread] %-5 level % logger [% NDC]-% message % newline"/>
</Layout>
</Appender>
<Root>
<Level value = "all"/>
<Appender-ref = "rollinglogfileappender"/>
</Root>
<! -- Ibatislogfile log -->
<Appender name = "ibatislogfile" type = "log4net. appender. rollingfileappender">
<File value = "./logs \ ibatis _"/>
<Appendtofile value = "true"/>
<Datepattern value = "yyyymmdd'.txt '"/>
<Rollingstyle value = "date"/>
<Staticlogfilename value = "false"/>
<Layout type = "log4net. layout. patternlayout">
<Header value = "[header] & amp; #13; & amp; #10;"/>
<Footer value = "[footer] & amp; #13; & amp; #10;"/>
<Conversionpattern value = "% Date {dd/mm/yyyy-hh: mm: SS} % m % newline % exception"/>
</Layout>
</Appender>
<! -- Ibatismapperlogfile log -->
<Appender name = "ibatismapperlogfile" type = "log4net. appender. rollingfileappender">
<File value = "./logs \ ibatismapper _"/>
<Appendtofile value = "true"/>
<Datepattern value = "yyyymmdd'.txt '"/>
<Rollingstyle value = "date"/>
<Staticlogfilename value = "false"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% Date {dd/mm/yyyy-hh: mm: SS} % m % newline % exception"/>
</Layout>
</Appender>
<! -- Ibatiscachelogfile log -->
<Appender name = "ibatiscachelogfile" type = "log4net. appender. rollingfileappender">
<File value = "./logs \ ibatischache _. log"/>
<Appendtofile value = "true"/>
<Datepattern value = "yyyy-mm-dd"/>
<Rollingstyle value = "date"/>
<Staticlogfilename value = "false"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% Date {dd/mm/yyyy-hh: mm: SS} % m % newline % exception"/>
</Layout>
</Appender>
<! -- Note that additivity = "false" must be added, because the exception log is passed up. If this attribute is not added, ibatisnet logs will also appear in the system's total logs, in this way, the record is repeated -->
<Logger name = "ibatisnet" additi.pdf = "false">
<Level value = "all"/>
<! -- If you want to add a log output destination, add the <appender-ref> node. Note that the ref is the appender name configured in config. -->
<Appender-ref = "ibatislogfile"/>
</Logger>
<! -- Note that additivity = "false" must be added because the exception log is passed up. If this attribute is not added, it will be included in the system's total logs and the logs in ibatisnet, ibatisnet. the datamapper log is recorded again -->
<Logger name = "ibatisnet. datamapper" additi.pdf = "false">
<Level value = "all"/>
<Appender-ref = "ibatismapperlogfile"/>
</Logger>
</Log4net>
</Configuration>
Finally, Let's explain the name of logger. This name represents the namespace in the code, that is, when we write in the code, ilog log = log4net. logmanager. getlogger (typeof (Program); this getlogger (typeof (Program) also obtains the namespace. When the obtained value is the same as the name value of the configuration file, it is written to different media according to appender-Ref.
In addition, the logger name has an inheritance relationship, for example
// A indicates the namespace, indicating that the logs in the namespace are the responsibility of the logger.
Name =
// A. B indicates a class in the namespace and logs in the namespace. Therefore, logger is responsible
Name = A. B
A. B inherits a. If additivity = "false" is not set on a. B, the log of A. B is written to.
Step 3: configure the logs written by ibatis. NET through ibatisnet. Common. Logging in Web. config.
<Configsections>
<Sectiongroup name = "ibatis">
<Section name = "logging" type = "ibatisnet. Common. Logging. configurationsectionhandler, ibatisnet. Common"/>
</Sectiongroup>
</Configsections>
<Ibatis>
<Logging>
<Logfactoryadapter type = "ibatisnet. Common. Logging. impl. log4netloggerfa, ibatisnet. Common. Logging. log4net">
<! --
Inline: log4net node configuration in APP. config/Web. config file
File: use an external configuration file (the configfile parameter must be used together, <Arg key = "configfile" value = "external configuration file ")
File-Watch: Like "file", it only has the ability to monitor changes to external configuration files. If there is any change, reload the configuration.
External: ibatis will not try to configure log4net.
-->
<Arg key = "configtype" value = "external"/>
</Logfactoryadapter>
</Logging>
</Ibatis>
Run the site and find that the logs folder already exists under the root directory of the site and start to write logs.
Download demo