Configure the log4net to log to MS SQL under ASP.net

Source: Internet
Author: User
Tags date config datetime log thread log4net
ASP.net log4net is an excellent open source log project, which I use to write a demo, as follows

0: Preparation
Go to http://logging.apache.org/, download log4net, latest version 1.2,
Create a ASP.net Web project and add Log4net.dll to the reference

1: Set up a data table
CREATE TABLE [dbo]. [Log] (
[ID] [int] IDENTITY (1, 1) not NULL,
[Date] [DateTime] Not NULL,
[Thread] [varchar] (255) Not NULL,
[Level] [varchar] () not NULL,
[Logger] [varchar] (255) Not NULL,
[Message] [varchar] (4000) Not NULL,
[Exception] [varchar] (Watts) NULL
)


2: Write the configuration file
<configuration>
<configSections>
<section name= "log4net" type= "log4net". Config.log4netconfigurationsectionhandler, log4net "/>
</configSections>

<system.web>
<!--
......
-->
</system.web>

<log4net>

<root>
<level value= "All"/>
<appender-ref ref= "Adonetappender"/>
</root>

<!--
<logger name= "Testapp.logging" >
<level value= "All"/>
<appender-ref ref= "Adonetappender"/>

</logger>
-->

<appender name= "Adonetappender" type= "log4net". Appender.adonetappender ">
<buffersize value= "Ten"/>
<connectiontype value= "System.Data.SqlClient.SqlConnection, System.Data, version=1.0.3300.0, Culture=neutral, publickeytoken=b77a5c561934e089 "/>
<connectionstring value= "Server=localhost;database=demo;user id=sa;password=123456"/>
<commandtext value= "INSERT into Log ([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>

Special note <buffersize value= "Ten"/&gt, this is the log information record of the buffer size (for example, only log information records up to 10,
Log information log will be written to the database)

3: In the Global.asax.cs
protected void Application_Start (Object sender, EventArgs e)
{
Read configuration information
Log4net. Config.DOMConfigurator.Configure ();
}
4: A demo class that uses log
Using System;

Namespace Log
{
<summary>
Summary description of the mylog.
</summary>
public class MyLog
{
private static readonly log4net. ILog log = log4net. Logmanager.getlogger (typeof (MyLog));
public static void Loginfo ()
{
Log. Info ("log Logs");
}

public static void Logerror ()
{
Log. Info ("Log Error");
}
}
}
5: Resources:
My blog:http://tomch.donews.net/tomch/posts/22457.aspx
Master Station logging.apache.org/
Learn to use log4net articles www.ondotnet.com/pub/a/dotnet/2003/06/16/log4net.html

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.