Log4net custom field to database (ii)

Source: Internet
Author: User
Tags log4net

This method is more troublesome than the first method Log4net.config<?xml version="1.0"encoding="Utf-8"?><configuration> <!--author:gaobingbing--> <configSections> <section name="log4net"Type="log4net. Config.log4netconfigurationsectionhandler,log4net"/> </configSections> <log4net> <!--write to database-<appender name="Adonetappender_dbserver"Type="log4net. Appender.adonetappender"> <!--error queue data reached 5 to persist to database-<buffersize value="1"/> <param name="ConnectionType"Value="MySql.Data.MySqlClient.MySqlConnection, Mysql.data"/> <param name="ConnectionString"Value="Data source=ip;initial catalog=test;user id=test;password=****;p ooling=true;min pool size=1; max Pool size=5; Connection timeout=100;"/> <commandtext value="INSERT into Xiao_log (date,thread,level,logger,message,userid,username) VALUES (@log_date, @thread, @log_level, @ Logger, @message, @userid, @username)"/> <parameter> <parametername value="@log_date"/> <dbtype value="DateTime"/> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%date{yyyy '-' mm '-' dd HH ': ' mm ': ' SS '. ' FFF}"/> </layout> <!--<layout type="log4net. Layout.rawtimestamplayout"/>--> </parameter> <parameter> <parametername value="@thread"/> <dbtype value="String"/> <size value="255"/> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%thread"/> </layout> </parameter> <parameter> <parametername value="@log_level"/> <dbtype value="String"/> <size value=" -"/> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%level"/> </layout> </parameter> <parameter> <parametername value="@logger"/> <dbtype value="String"/> <size value="255"/> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%logger"/> </layout> </parameter> <parameter> <parametername value="@message"/> <dbtype value="String"/> <size value="4000"/> <layout type="log4net. Layout.patternlayout"> <conversionpattern value="%message"/> </layout> </parameter> <!--custom Fields--<!--method Two--and <parameter> <parametername value="@userid"/> <dbtype value="String"/> <size value="255"/> <layout type="Log4net.mylayout">[Note: The log4net here is the namespace, Mylayout is the class name] <conversionpattern value="%userid"/> </layout> </parameter> <parameter> <parametername value="@username"/> <dbtype value="String"/> <size value="255"/> <layout type="Log4net.mylayout"> <conversionpattern value="%username"/> </layout> </parameter> </appender> <root> <level value=" All"/> <appender-ref ref="Adonetappender_dbserver"/> </root> <!--Specify the level forSome specific categories-<logger name="iNotes"> <!--<appender-ref ref="B"/>--<level value=" All"/> <appender-ref ref="Adonetappender_dbserver"/> </logger> </log4net></configuration>
Create LogMessage.cs, which is all custom field properties inside the classnamespacelog4net{ Public classLogMessage { Public string_userid;  Public string_username;  Publiclogmessage () {} PublicLogMessage (stringUseridstringuserName) {_userid=userId; _username=UserName; }         Public stringUserId {Get{return_userid;} Set{_userid =value;} }         Public stringUserName {Get{return_username;} Set{_username =value;} }    }}
Create ParemterContent.cs Add Parameter converter class, each field a schema conversion classnamespacelog4net{ Public classparemtercontent {Internal Sealed classUseridparam:patternlayoutconverter {//Methods            protected Override voidConvert (TextWriter writer, loggingevent loggingevent) {logmessage content= Loggingevent.messageobject asLogMessage; if(Content! =NULL) {writer. Write (content.                USERID); }            }        }        Internal Sealed classUsernameparam:patternlayoutconverter {protected Override voidConvert (TextWriter writer, loggingevent loggingevent) {logmessage content= Loggingevent.messageobject asLogMessage; if(Content! =NULL) {writer. Write (content.                UserName); }            }        }     }} 
Create MyLayout.cs does this look familiar?<layout type= "Log4net.mylayout" >namespacelog4net{Internal classMylayout:patternlayout { Publicmylayout () { This. Addconverter ("username",typeof(Paremtercontent.usernameparam));  This. Addconverter ("userid",typeof(Paremtercontent.useridparam)); }    }}
    Log4net. ILog Loger = log4net. Logmanager.getlogger ("iNotes");                 New LogMessage ("dajiahao! ") " );                Loger. Info (msg);
If the insertion is unsuccessful,  you need to check " Log4net.config " true )]global.asax is not joined by            Logmanager.getlogger (Methodbase.getcurrentmethod (). DECLARINGTYPE); If missing, it needs to be added. Check the Log4net.config configuration file if it is not successful, and make sure that there is a problem with the custom attribute in XML

Log4net custom field to database (ii)

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.