Log4net writes data to the SQL database, log4netsql

Source: Internet
Author: User
Tags sql client log4net

Log4net writes data to the SQL database, log4netsql

Recently, some user behaviors need to be added to the database, so I thought of using log4net. If there are other good solutions, you can correct me.

Let's take a look at the configuration file. This is the console file app. config.

<Layout type = "LogTest. MyLayout, LogTest"> <param name = "ConversionPattern" value = "% property {Log_Type}"/> </layout>
This is a 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 =" ADONetAppender "/> </root> <! -- Sqldatabase --> <appender name = "ADONetAppender" type = "log4net. Appender. ADONetAppender"> <bufferSize value = "1"/> <! -- SQL data source, local installation of 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_Conte Nt, @ Log_Url, @ au_ID) "/> <parameter> <parameterName value =" @ Log_Object "/> <dbType value =" String "/> <size value =" 200 "/> <layout type =" LogTest. myLayout, logTest "> <param name =" ConversionPattern "value =" % property {Log_Object} "/> </layout> </parameter> <parameterName value =" @ Log_Type" /> <dbType value = "String" type = "codeph" text = "/codeph"/> <size value = "200"/> <layout type = "LogTest. myLayout, LogTest "> <param name =" Con VersionPattern "value =" % property {Log_Type} "/> </layout> </parameter> <parameterName value =" @ Log_Content "/> <dbType value =" String & quot;/> <size value = "2000"/> <layout type = "LogTest. myLayout, logTest "> <param name =" ConversionPattern "value =" % property {Log_Content} "/> </layout> </parameter> <parameterName value =" @ Log_Url" /> <dbType value = "String" type = "codeph" text = "/codeph"/> <size value = "100"/> <layout type = "LogTest. MyLayout, LogTest"> <param name = "ConversionPattern" value = "% property {Log_Url}"/> </layout> </parameter> <! -- Custom member --> <parameter> <parameterName value = "@ au_ID"/> <dbType value = "Int32"/> <layout type = "LogTest. myLayout, logTest "> <param name =" ConversionPattern "value =" % property {au_ID} "/> </layout> </parameter> </appender> </log4net> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.5 "/> </startup> </configuration>

MyLayout is a ing Class.

Public class MyLayout: PatternLayout {public MyLayout () {this. addConverter ("property", typeof (MyMessagePatternConverter);} public class MyMessagePatternConverter: PatternLayoutConverter {protected override void Convert (System. IO. textWriter writer, log4net. core. loggingEvent loggingEvent) {if (Option! = Null) {// Write the value for the specified key WriteObject (writer, loggingEvent. repository, LookupProperty (Option, loggingEvent);} else {// Write all the key value pairs WriteDictionary (writer, loggingEvent. repository, loggingEvent. getProperties ());}} /// <summary> /// obtain the value of an attribute of the input log object through reflection /// </summary> /// <param name = "property"> </param> // <returns> </returns> private object LookupPrope Rty (string property, log4net. Core. LoggingEvent loggingEvent) {object propertyValue = string. Empty; PropertyInfo propertyInfo = loggingEvent. MessageObject. GetType (). GetProperty (property); if (propertyInfo! = Null) propertyValue = propertyInfo. GetValue (loggingEvent. MessageObject, null); return propertyValue ;}}

 

Console

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LogTest {    class Program {        static void Main(string[] args) {            log4net.Config.XmlConfigurator.Configure();            log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));            log.Info(new ASM_Log { au_ID = 1, Log_Content = "11", Log_Type = "s", Log_Object = "3", Log_Url = "sss" });            Console.ReadKey();        }    }    public class ASM_Log {        public int au_ID { set; get; }        public string Log_Object { set; get; }        public string Log_Type { set; get; }        public string Log_Content { set; get; }        public string Log_Url { set; get; }    }}

 

 

Because projects are connected to other projects, you can reference existing projects as needed.

Download link: http://pan.baidu.com/s/1jHJgLnS

 

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.