log4net writing data to SQL database

Source: Internet
Author: User
Tags sql client log4net

Recently need to add some user behavior to the database, so think of using log4net, if there are other good solutions, you can give me correct.

Look at the configuration file first. This is the console file, app. Config

   <layout type= "logtest.mylayout, logtest" >          <param name= "Conversionpattern" value= "%property{Log_Type}" />        
This is the custom parameter
<?xml version="1.0"encoding="Utf-8"?><configuration> <configSections> <section name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler, Log4net"/> </configSections> <log4net> <root> <level value=" All"></level> <appender-ref ref="Adonetappender"/> </root> <!--SQL database--<appender name="Adonetappender"Type="log4net. Appender.adonetappender"> <buffersize value="1"/> <!--SQL data source, locally installed SQL client--<connectiontype value="System.data.sqlclient.sqlconnection,system.data, version=1.0.3300.0, culture=neutral,publickeytoken= b77a5c561934e089"/> <connectionstring value="database=asm_usermanger;server=xxxxxx; User id=xxx; Password=xxxx"/> <commandtext value="INSERT into Asm_log ([log_object],[log_type],[log_content],[log_url],[au_id]) VALUES (@Log_Object, @Log_Type, @ Log_content, @Log_Url, @au_ID)"/> <parameter> <parametername value="@Log_Object"/> <dbtype value="String"/> <size value=" $"/> <layout type="Logtest.mylayout, Logtest"> <param name="Conversionpattern"Value="%property{log_object}"/> </layout> </parameter> <parameter> <parametername value="@Log_Type"/> <dbtype value="String"/> <size value=" $"/> <layout type="Logtest.mylayout, Logtest"> <param name="Conversionpattern"Value="%property{log_type}"/> </layout> </parameter> <parameter> <parametername value="@Log_Content"/> <dbtype value="String"/> <size value=" -"/> <layout type="Logtest.mylayout, Logtest"> <param name="Conversionpattern"Value="%property{log_content}"/> </layout> </parameter> <parameter> <parametername value="@Log_Url"/> <dbtype value="String"/> <size value=" -"/> <layout type="Logtest.mylayout, Logtest"> <param name="Conversionpattern"Value="%property{log_url}"/> </layout> </parameter> <!--Custom Members--<parameter> <parameter Name value="@au_ID"/> <dbtype value="Int32"/> <layout type="Logtest.mylayout, Logtest"> <param name="Conversionpattern"Value="%property{au_id}"/> </layout> </parameter> </appender> </log4net> <startup> &lt ; Supportedruntime version="v4.0"sku=". netframework,version=v4.5"/> </startup></configuration>

Mylayout is a mapping class

  Public classMylayout:patternlayout { Publicmylayout () { This. Addconverter (" Property",typeof(Mymessagepatternconverter)); }    }     Public classMymessagepatternconverter:patternlayoutconverter {protected Override voidConvert (System.IO.TextWriter writer, log4net. Core.loggingevent loggingevent) {if(Option! =NULL) {                //Write The value for the specified keyWriteObject (writer, loggingevent.repository, Lookupproperty (Option, loggingevent)); }            Else {                //Write all the key value pairsWritedictionary (writer, loggingevent.repository, Loggingevent.getproperties ()); }        }        /// <summary>          ///To get the value of an attribute of an incoming log object by reflection/// </summary>          /// <param name= "property" ></param>          /// <returns></returns>          Private ObjectLookupproperty (stringProperty , Log4net. Core.loggingevent loggingevent) {ObjectPropertyValue =string.            Empty; PropertyInfo PropertyInfo=LoggingEvent.MessageObject.GetType ().            GetProperty (property); if(PropertyInfo! =NULL) PropertyValue= Propertyinfo.getvalue (Loggingevent.messageobject,NULL); returnPropertyValue; }    }

Console text

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceLogtest {classProgram {Static voidMain (string[] args) {log4net.            Config.XmlConfigurator.Configure (); Log4net. ILog Log= Log4net. Logmanager.getlogger (typeof(program)); Log. Info (NewAsm_log {au_id =1, log_content =" One", Log_type ="s", Log_object ="3", Log_url ="SSS" });        Console.readkey (); }    }     Public classAsm_log { Public intau_id {Set;Get; }  Public stringLog_object {Set;Get; }  Public stringLog_type {Set;Get; }  Public stringlog_content {Set;Get; }  Public stringLog_url {Set;Get; } }}

Because projects are connected to other projects, it is necessary to refer to existing projects to

Download Link: Http://pan.baidu.com/s/1jHJgLnS

log4net writing data to SQL database

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.