[c#]log4net Write SQL Server database log configuration method

Source: Internet
Author: User
Tags config datetime log thread log4net
server|sqlserver| Data | database

Preparation of database log tables

Prepare one of these tables in the database, Mail_web_log:

Build script:

CREATE TABLE [dbo]. [Mail_web_log] (

[NId] [INT] IDENTITY (1, 1) not NULL,

[Dtdate] [DateTime] Not NULL,

[Sthread] [varchar] (m) COLLATE chinese_prc_ci_as not NULL,

[Slevel] [varchar] (m) COLLATE chinese_prc_ci_as not NULL,

[Slogger] [varchar] (m) COLLATE chinese_prc_ci_as not NULL,

[Smessage] [varchar] (COLLATE) chinese_prc_ci_as not NULL,

[Sexception] [varchar] (4000) COLLATE chinese_prc_ci_as NULL

) on [PRIMARY]

Go

ALTER TABLE [dbo]. [Mail_web_log] With NOCHECK ADD

CONSTRAINT [Pk_mail_web_log] PRIMARY KEY CLUSTERED

(

[NId]

) on [PRIMARY]

Go

Where the Slevel field represents the level of the log, and the Slogger field represents which class or file the log is generated, such as

"INotes.Default.Page_Load (c:\mail\mailsource\default.aspx.cs:269)"; The Smessage field represents the contents of the log.

Configuration of log4net in Web.config

Note how the "Adonetappender" node is written, and how each field is printed.

In general, we define a "<layout type=" log4net for each log field. Layout.patternlayout ">" node to tell log4net how to output, under this node, we need to configure

<conversionpattern value= "%t"/>

%t in log4net specifically refers to the thread name.

Below we explain to "%d%t%p%l%m%n" one by one:

L%d output log point date or time, the default format is ISO8601, you can specify the format after, such as:%d{yyyy-mm-dd HH:mm:ss}, output similar: 2005-7-19 17:49:27, just fit to insert SQL Server;

L%T the thread name that generated the log event;

L%p Log log_level, such as debug, warn, or info;

The class of the output of the L-%c, usually the full name of the class in which it is located, such as "Inotes.default";

L%M the contents of the log;

L%l where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code. such as Write2database.main (WRITE2DATABASE.JAVA:33);

%n output a carriage return line feed, Windows platform "\ r \ n", UNIX platform "\ n"

For the Web site's Web.config file, you need to configure the following, this part is to refer to the CGQ214 blog to the writing, at the same time according to the actual effect of the point of correction:

Configuration:

<log4net>

...

<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=192.168.1.112;database=domino;uid=sa;pwd=;timeout=300;"/>

<commandtext value= INSERT into Mail_web_log ([dtdate],[sthread],[slevel],[slogger],[smessage],[sexception]) 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= "/>"

<layout type= "log4net. Layout.patternlayout ">

<conversionpattern value= "%t"/>

</layout>

</parameter>

<parameter>

<parametername value= "@log_level"/>

<dbtype value= "String"/>

<size value= "/>"

<layout type= "log4net. Layout.patternlayout ">

<conversionpattern value= "%p"/>

</layout>

</parameter>

<parameter>

<parametername value= "@logger"/>

<dbtype value= "String"/>

<size value= "/>"

<layout type= "log4net. Layout.patternlayout ">

<conversionpattern value= "%logger"/>

</layout>

</parameter>

<parameter>

<parametername value= "@message"/>

<dbtype value= "String"/>

<size value= "/>"

<layout type= "log4net. Layout.patternlayout ">

<conversionpattern value= "%m"/>

</layout>

</parameter>

<parameter>

<parametername value= "@exception"/>

<dbtype value= "String"/>

<size value= "/>"

<layout type= "log4net. Layout.exceptionlayout "/>

</parameter>

</appender>

<!--Setup The root category, add the Appenders and set the default level-->

<root>

<level value= "WARN"/>

<level value= "INFO"/>

<level value= "DEBUG"/>

<level value= "FINE"/>

</root>

<!--specify for some specific categories-->

<logger name= "INotes" >

<level value= "WARN"/>

<level value= "INFO"/>

<level value= "DEBUG"/>

<level value= "FINE"/>

<appender-ref ref= "Rollinglogfileappender"/>

<appender-ref ref= "Adonetappender"/>

</logger> </log4net>



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.