Use a standalone Log4net.config file to configure the log4net, logging to the MySQL database "original"

Source: Internet
Author: User
Tags throw exception log4net

Development environment:

VS2013,

ASP. NET MVC 4.0,

. Net Framework 4.0,

Log4net 1.2.13.0,

mysql.data.dll,6.8.3.0

Setup steps:

1. At the site root, create a log4net.config XML file. (The file name can be arbitrary, we are called log4net.config)

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<log4net>
<root>
<level value= "All"/>
<appender-ref ref= "Adonetappender"/>
<!--<appender-ref ref= "Rollingfile"/>-->
<!--<appender-ref ref= "Rollinglogfileappender"/>-->
</root>
<appender name= "Console" type= "log4net. Appender.consoleappender ">
<layout type= "log4net. Layout.patternlayout ">
<!--Pattern to output the caller's file name and line number--
<conversionpattern value= "%5level [%thread] (%file:%line)-%message%newline"/>
</layout>
</appender>
<!--log file part of the output format setting--
<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 ">
<conversionpattern value= "%date [%thread]%-5level%logger [%NDC]-%message%newline"/>
</layout>
</appender>

<appender name= "Rollingfile" type= "log4net. Appender.rollingfileappender ">
<file value= "Example.log"/>
<appendtofile value= "true"/>
<maximumfilesize value= "100KB"/>
<maxsizerollbackups value= "2"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%level%thread%logger-%message%newline"/>
</layout>
</appender>
<appender name= "Adonetappender" type= "log4net. Appender.adonetappender ">
<buffersize value= "1"/>
<connectiontype value= "MySql.Data.MySqlClient.MySqlConnection, Mysql.data,version=6.8.3.0,culture=neutral, Publickeytoken=c5687fc88969c44d "/>
<connectionstring value= "server=127.0.0.1;database=log4net_db; Uid=root; pwd=123; "/>
<commandtext value= "INSERT into log4net (' Date ', ' Thread ', ' Level ', ' Logger ', ' Message ', ' Exception ') VALUES (@log_ Date, @thread, @log_level, @logger, @message, @exception) "/>
<parameter>
<parametername value= "@log_date"/>
<dbtype value= "DateTime"/>
<layout type= "log4net. Layout.rawtimestamplayout "/>
</parameter>
<parameter>
<parametername value= "@thread"/>
<dbtype value= "String"/>
<size value= "255"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%thread"/>
</layout>
</parameter>
<parameter>
<parametername value= "@log_level"/>
<dbtype value= "String"/>
<size value= "/>"
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%level"/>
</layout>
</parameter>
<parameter>
<parametername value= "@logger"/>
<dbtype value= "String"/>
<size value= "255"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%logger"/>
</layout>
</parameter>
<parameter>
<parametername value= "@message"/>
<dbtype value= "String"/>
<size value= "4000"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%message"/>
</layout>
</parameter>
<parameter>
<parametername value= "@exception"/>
<dbtype value= "String"/>
<size value= "/>"
<layout type= "log4net. Layout.exceptionlayout "/>
</parameter>
</appender>
</log4net>
</configuration>

2. Under Project visualization, add the Properties/assemblyinfo.cs:

[Assembly:log4net. Config.xmlconfigurator (configfile = "Log4net.config", Watch = True)]

The 3.global.asax.cs file executes:

protected void Application_Start ()
{

The Independent Log4net.config
log4net. Config.XmlConfigurator.Configure (New FileInfo ("Log4net.config"));

Arearegistration.registerallareas ();

Webapiconfig.register (globalconfiguration.configuration);
Filterconfig.registerglobalfilters (globalfilters.filters);
Routeconfig.registerroutes (routetable.routes);
Bundleconfig.registerbundles (Bundletable.bundles);
}

4. Call the Write log method:

ILog logger = Logmanager.getlogger (typeof (Albumservcie));

Logger. Errorformat ("Getalbummodel isiiscache={0} Throw Exception={1}", Isiiscache, ex. Message);

Attention:

In Log4net. Config, delete:

<configSections>      <section name= "log4net" type= "log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>In Web. config, delete the <configSections>      <section name= "log4net" type= "log4net.Config.Log4NetConfigurationSectionHandler, log4net" />    </configSections>

Reference:

1.http://www.cnblogs.com/mahope/archive/2006/04/14/375221.html

2.http://logging.apache.org/log4net/release/config-examples.html

3.http://blog.csdn.net/zhoufoxcn/article/details/2220533

Use a standalone Log4net.config file to configure the log4net, logging to the MySQL database "original"

Related Article

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.