Using Log4net to write logs to the SQL Server database

Source: Internet
Author: User
Tags log4net

1, official website url:http://logging.apache.org/log4net/

2. configuration file Reference url:http://logging.apache.org/log4net/release/config-examples.html

3. Add the following code to the Web. config

3.1: Join in the configsections

<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>

  
<configsections>    <!--configuration of the log4net -    < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler,log4net "/>        < Sectionname= "EntityFramework"type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=6.0.0.0, culture= Neutral, publickeytoken=b77a5c561934e089 "requirepermission= "false" />    <!--for more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -  </configsections>
configsections

The 3.2:log4net node is configured as follows  

  
<!--configuration of the log4net -  <log4net>    <!--Local text file -    <Appendername= "Logfileappender"type= "log4net." Appender.fileappender " >      <paramname= "File"value= "C:\Log4\DBLog.txt" />      <paramname= "Appendtofile"value= "true" />      <Layouttype= "log4net." Layout.patternlayout ">        <paramname= "Conversionpattern"value= "%d [%t]%-5p%c [%x]-%m%n" />      </Layout>    </Appender>    <!--SQL Database -    <Appendername= "Adonetappender"type= "log4net." Appender.adonetappender ">      <buffersizevalue= "1" />      <ConnectionTypevalue= "System.Data.SqlClient.SqlConnection, System.Data, version=1.0.3300.0, Culture=neutral, publickeytoken= b77a5c561934e089 " />      <connectionStringvalue= "Data source=[database server];initial catalog=[database name];integrated security=false;persist Security info= True; User Id=[user]; Password=[password] " />    <CommandTextvalue= "INSERT into Log ([date],[thread],[level],[logger],[message],[exception]) VALUES (@log_date, @thread, @log_level , @logger, @message, @exception) " />      <parameter>        <parameternamevalue= "@log_date" />        <DbTypevalue= "DateTime" />        <Layouttype= "log4net." Layout.rawtimestamplayout " />      </parameter>      <parameter>        <parameternamevalue= "@thread" />        <DbTypevalue= "String" />        <sizevalue= "255" />        <Layouttype= "log4net." Layout.patternlayout ">          <Conversionpatternvalue= "%thread" />        </Layout>      </parameter>      <parameter>        <parameternamevalue= "@log_level" />        <DbTypevalue= "String" />        <sizevalue= " the" />        <Layouttype= "log4net." Layout.patternlayout ">          <Conversionpatternvalue= "%level" />        </Layout>      </parameter>      <parameter>        <parameternamevalue= "@logger" />        <DbTypevalue= "String" />        <sizevalue= "255" />        <Layouttype= "log4net." Layout.patternlayout ">          <Conversionpatternvalue= "%logger" />        </Layout>      </parameter>      <parameter>        <parameternamevalue= "@message" />        <DbTypevalue= "String" />        <sizevalue= "4000" />        <Layouttype= "log4net." Layout.patternlayout ">          <Conversionpatternvalue= "%message" />        </Layout>      </parameter>      <parameter>        <parameternamevalue= "@exception" />        <DbTypevalue= "String" />        <sizevalue= "$" />        <Layouttype= "log4net." Layout.exceptionlayout " />      </parameter>    </Appender>    <Root>      <!--Set this to "ERROR" before release -      <!--highest:off->fatal->rror->warn->info->debug->all:lowest -      < Levelvalue= "All" />      <Appender-refref= "Adonetappender" />      <!--<appender-ref ref= "Smtpappender"/> -      <!--<appender-ref ref= "Fileappender"/> -    </Root>  </log4net>
configuration of the LOG4 node

4, add the following two lines of code in AssemblyInfo.cs

Using Log4net;

[Assembly:log4net. Config.xmlconfiguratorattribute (Watch = True)]

5. The code call test is as follows:

private static readonly log4net. ILog log = log4net. Logmanager.getlogger (typeof (HomeController));        Public ActionResult Index ()        {            log. Info ("Information at the custom info level");            Log. Error ("Error", New Exception ("error message ... "));            Log. Fatal ("Fatal", New Exception ("Fatal info ... "));            Log. info ("info", new Exception ("info info ... "));            Log. Debug ("Debug", New Exception ("Debug info ... "));            Log. Warn ("Warn", New Exception ("Warn info ... "));            return View ();        }
log4net called in code

6, the effect is as follows:

7. Build the SQL statement of the table:

CREATE TABLE [dbo]. [Log] (    [Id] [int] IDENTITY (1, 1) not NULL,    [Date] [datetime] is not NULL,    [Thread] [varchar] (255) is not NULL,    [Lev EL] [varchar] () NOT NULL,    [Logger] [varchar] (255) is not NULL,    [Message] [varchar] (4000) is not NULL,    [exceptio n] [varchar] (+) NULL)

Using Log4net to write logs to the SQL Server database

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.