Write log4net configurations to the SQL Server database (sqlserver-sqlclient) and input custom Business Objects

Source: Internet
Author: User
Tags log4net

Http://www.cnblogs.com/Arlen/archive/2008/11/22/1338908.html

This post is based on some modifications. After debugging and running, the code in this post can be directly copied and used.

Business logs need to be recorded in the project (that is, what operations are performed by the user, what content is operated, and when the operation content is stored in a structured manner to facilitate data mining in the future ).

The system uses log4net to record business logs to the database. You can add an appender to log4net anyway. Because the business needs to record not simple system time % date, level % level, information % message and other fields, but custom business fields. You can pass in the object parameter log.info (Object message) for methods such as info, error, and debug that record logs ). So I went to the Internet to find out if it was as expected, and sent a custom Business Log object to the info method. It automatically helped me get the field value of this business object. After searching for half a day, the answer is:No.

After thinking about it, isn't it used by others? How do others pass in custom business objects?

Although it seems that there is no need, I have made a solution myself.

The following describes the configuration method and the solution for passing in custom business objects.

1. How to configure log4net for sqlserver:

(Note: If the configuration is written to the database, copy system. Data. DLL to the bin directory .)

Before writing data to the database, first check the structure of the database table to be written.


 

The table name is test_log.

Let's look at our custom message class.

Public class logcontent <br/>{< br/> Public String reason {Get; Set ;}< br/> Public string name {Get; Set ;}< br/>}

Everything is ready. I am in arrears. The following is the configuration file.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <configuration> <br/> <configsections> <br/> <section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net "/> <br/> </configsections> <br/> <log4net> <br/> <root> <br/> <level value =" all "/> <br/> <appender-ref = "adonetappender"/> <br/> </root> <br/> <appender name = "adonetappender" type = "log4net. appender. adonetappender "> <br/> <buffersize value =" 1 "/> <br/> <connectiontype value =" system. data. sqlclient. sqlconnection, system. data, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089 "/> <br/> <connectionstring value =" database = xxx; server = xxx; user id = sa; password = xxx "/> <br/> <commandtext value =" insert into test_log (record time, message level, message content, user name) values (@ logtime, @ level, @ reason, @ user) "/> </P> <p> <parameter> <br/> <parametername value =" @ logtime "/> <br/> <dbtype value =" datetime "/> <br/> <layout type = "log4net. layout. rawtimestamplayout "/> <br/> </parameter> </P> <p> <parameter> <br/> <parametername value =" @ level "/> <br/> <dbtype value = "string"/> <br/> <size value = "50"/> <br/> <layout type = "log4net. layout. patternlayout "value =" % level "/> <br/> </parameter> </P> <p> <parameter> <br/> <parametername value =" @ reason" /> <br/> <dbtype value = "string"/> <br/> <size value = "100"/> <br/> <layout type = "testlogniu. mylayout, testlogniu "> <br/> <Param name =" conversionpattern "value =" % property {reason} "/> <br/> </layout> <br/> </Parameter> </P> <p> <parameter> <br/> <parametername value = "@ user"/> <br/> <dbtype value = "string"/> <br /> <size value = "20"/> <br/> <layout type = "testlogniu. mylayout, testlogniu "> <br/> <Param name =" conversionpattern "value =" % property {name} "/> <br/> </layout> <br/> </Parameter> </P> <p> </appender> <br/> </log4net> <br/> </configuration>

 

At this point, the program cannot run. By the way, there is only one thing missing, that is, the custom mylayout. The code below is provided.

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. reflection; <br/> using system. text; <br/> using log4net. layout; <br/> using log4net. layout. pattern; </P> <p> namespace testlogniu <br/> {<br/> public class mylayout: patternlayout <br/>{< br/> Public mylayout () <br/>{< br/> This. addconverter ("property", typeof (mymessagepatternconverter); <br/>}</P> <P> public class mymessagepatternconverter: patternlayoutconverter <br/> {<br/> protected override void convert (system. io. textwriter writer, log4net. core. loggingevent) <br/>{< br/> If (option! = NULL) <br/>{< br/> // write the value for the specified key <br/> writeobject (writer, loggingevent. repository, lookupproperty (option, loggingevent )); <br/>}< br/> else <br/> {<br/> // write all the key value pairs <br/> writedictionary (writer, loggingevent. repository, loggingevent. getproperties ()); <br/>}< br/> // <summary> <br/> // obtain the value of a property of the input log object through reflection <br/>}< br/> br/> // </Summary> <br/> // /<Param name = "property"> </param> <br/> // <returns> </returns> <br/> private object lookupproperty (string property, log4net. core. loggingevent) <br/>{< br/> Object propertyvalue = string. empty; <br/> propertyinfo = loggingevent. messageobject. getType (). getproperty (property); <br/> If (propertyinfo! = NULL) <br/> propertyvalue = propertyinfo. getvalue (loggingevent. messageobject, null); <br/> return propertyvalue; <br/>}< br/>

Code call

Class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> log4net. config. xmlconfigurator. configure (); </P> <p> log4net. ilog log = log4net. logmanager. getlogger (typeof (Program); <br/> log. info (New logcontent {reason = "this is a test", name = "James"}); <br/> console. readkey (); </P> <p >}< br/>}

Running result.

 

 

You are done.

 

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.