Factory method mode for multi-function Logging

Source: Internet
Author: User

Sometimes, we encounter some logging functions during development. In addition, they may be independent of the system's business logic, so we can make the log into a component form for convenient reuse.

The following uses the factory method to design the log component and ensure that the function expansion does not change the existing implementation.

Three logging types are available: error logging, click logging, and Operation logging.

First, we create an "Abstract Factory" [which is actually a log interface that must be implemented by each instance factory]. Since it is a log record, there must be a log entity. That is, logentity, which is an "abstract entity". Because different types of logs require different log entities, logentity is used as the abstract entity here, to generate specific types of log entities.

The implementation is as follows:

Public interface ilogfactory <br/>{< br/> void log (logentity); <br/>}
/// <Summary> <br/> // log entity (abstract base class) <br/> // </Summary> <br/> public abstract class logentity <br/> {<br/> Public override string tostring () <br/>{< br/> return "logentity (abstract class)"; <br/>}< br/>}
The following are the instance factories and log entities to implement their respective logging logic:

/// <Summary> <br/> // entity class of the Error Log <br/> /// </Summary> <br/> public class errorentity: logentity <br/> {<br/> /// <summary> <br/> // No. <br/> /// </Summary> <br/> Public string ID {Get; set ;} </P> <p> // <summary> <br/> // user id <br/> /// </Summary> <br/> Public String userid {Get; set ;} </P> <p> /// <summary> <br/> // page url <br/> /// </Summary> <br/> Public String pageurl {Get; set ;} </P> <p> /// <summary> <br/> // error message <br/> /// </Summary> <br/> Public String msg {Get; set ;} </P> <p> /// <summary> <br/> // occurrence time <br/> /// </Summary> <br/> Public datetime operatetime {Get; set ;}</P> <p>}
Public class errorfactory: ilogfactory <br/>{</P> <p> private const string fullclassname = "logcomponent. errorfactory "; </P> <p> Public void log (logentity) <br/>{< br/> # region check ARGs <br/> If (logentity = NULL) <br/>{< br/> throw new argumentnullexception (string. format ("the method in {0} occur argumentnullexception", fullclassname); <br/>}</P> <p> errorentity innerentity = logenti Ty as errorentity; </P> <p> If (innerentity = NULL) <br/>{< br/> throw new nullreferenceexception (string. format ("the method in {0} occur nullreferenceexception. because of converting type. ", fullclassname); <br/>}</P> <p> If (string. isnullorempty (innerentity. MSG) <br/>{< br/> throw new exception (string. format ("the method in {0} occur logic exception. because of the error message is not nullable", Fullclassname )); <br/>}< br/> # endregion </P> <p> // logs <br/> string SQL = @ "insert into systemerrorinfo (se_userid, se_pageurl, se_message, se_operatetime) values (: se_userid,: se_pageurl,: se_message,: se_operatetime )"; <br/> oracleparameter [] parameters = new oracleparameter [] <br/> {<br/> New oracleparameter (": se_userid", innerentity. userid = NULL? "": Innerentity. userid), <br/> New oracleparameter (": se_pageurl", innerentity. pageurl = NULL? "": Innerentity. pageurl), <br/> New oracleparameter (": se_message", innerentity. MSG = NULL? "": Innerentity. msg), <br/> New oracleparameter (": se_operatetime", innerentity. operatetime = datetime. minvalue? Datetime. now: innerentity. operatetime) <br/>}; </P> <p> oraclehelper. executenonquery (oraclehelper. connectionstringlocaltransaction, commandtype. text, SQL, parameters); </P> <p >}</P> <p>}
/// <Summary> <br/> // click the button for statistics. The buttonname field is required. <br/> /// </Summary> <br/> public class clickcountentity: logentity <br/> {<br/> /// <summary> <br/> // record number <br/> /// </Summary> <br/> public String ID {Get; set ;} </P> <p> /// <summary> <br/> // user id <br/> /// </Summary> <br/> Public String userid {get; set ;} </P> <p> /// <summary> <br/> // page url <br/> /// </Summary> <br/> Public String pageurl {Get; set ;} </P> <p> /// <summary> <br/> // operation time <br/> /// </Summary> <br/> Public datetime operatetime {Get; set ;} </P> <p> /// <summary> <br/> // path name <br/> /// </Summary> <br/> Public String pathname {Get; set ;} </P> <p> /// <summary> <br/> // button name <br/> /// </Summary> <br/> Public String buttonname {Get; set ;}< br/>}
Public class clickcountfactory: ilogfactory <br/>{</P> <p> private const string fullclassname = "logcomponent. clickcountfactory "; <br/> private errorentity = NULL; </P> <p> Public void log (logentity) <br/>{< br/> // check ARGs <br/> If (logentity = NULL) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = string. format ("the method in {0} occur argumentnullexcept Ion. because of the Arg: logentity is not nullable. ", fullclassname); <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw new argumentnullexception (string. format ("the method in {0} occur argumentnullexception. because of the Arg: logentity is not nullable. ", fullclassname); <br/>}</P> <p> clickcountentity innerentity = logentity as clickcountentity; <br /> If (innerentity = NULL) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = string. format ("the method in {0} occur nullreferenceexception. becaues of converting type ", fullclassname); <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw new nullreferenceexception (string. format ("the method in {0} occur nullreferenceexception .. becaue S of converting type ", fullclassname); <br/>}</P> <p> If (string. isnullorempty (innerentity. buttonname) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = string. format ("the method in {0} occur exception. because of the field buttonname is not nullable. ", fullclassname); <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw new excep Tion (string. format ("the method in {0} occur exception. because of the field buttonname is not nullable. ", fullclassname); <br/>}</P> <p> string SQL = @" insert into buttonclickstatistic <br/> (bs_userid, bs_pageurl, bs_buttonid, bs_operatetime, bs_pathname, bs_buttonname) <br/> values <br/> (: v_bs_userid,: v_bs_pageurl,: v_bs_buttonid,: Signature,: v_bs_pathname,: v_bs_buttonname )"; </P> <p> O Racleparameter [] parameters = new oracleparameter [] {<br/> New oracleparameter (": v_bs_userid", innerentity. userid = NULL? "": Innerentity. userid), <br/> New oracleparameter (": v_bs_pageurl", innerentity. pageurl = NULL? "": Innerentity. pageurl), <br/> New oracleparameter (": v_bs_buttonid", innerentity. ID = NULL? "": Innerentity. ID), <br/> New oracleparameter (": v_bs_operatetime", innerentity. operatetime), <br/> New oracleparameter (": v_bs_pathname", innerentity. pathname = NULL? "": Innerentity. pathname), <br/> New oracleparameter (": v_bs_buttonname", innerentity. buttonname) <br/>}; </P> <p> try <br/>{< br/> oraclehelper. executenonquery (oraclehelper. connectionstringlocaltransaction, commandtype. text, SQL, parameters); <br/>}< br/> catch (exception ex) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = ex. message + ex. stacktrace; <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw; <br/>}</P> <p>}
/// <Summary> <br/> // operation log entity class-where MSG is a required field <br/> /// </Summary> <br/> Public class operatingentity: logentity <br/> {<br/> /// <summary> <br/> // record number <br/> /// </Summary> <br/> public String ID {Get; set ;} </P> <p> /// <summary> <br/> // user id <br/> /// </Summary> <br/> Public String userid {get; set ;} </P> <p> /// <summary> <br/> // operation log <br/> /// </Summary> <br/> Public String msg {Get; set ;} </P> <p> /// <summary> <br/> // operation time <br/> /// </Summary> <br/> Public datetime operatingtime {Get; set ;} </P> <p> /// <summary> <br/> // logon time <br/> /// </Summary> <br/> Public datetime logintime {Get; set ;} </P> <p> /// <summary> <br/> // deprecation time <br/> /// </Summary> <br/> Public datetime logouttime {Get; set ;}</P> <p>}
Public class operatingfactory: ilogfactory <br/>{< br/> private const string fullclassname = "logcomponent. operatingfactory "; <br/> private errorentity = NULL; </P> <p> Public void log (logentity) <br/>{< br/> // check ARGs <br/> If (logentity = NULL) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = string. format ("the method in {0} occur argumentnullexception. Because of the Arg: {1} is not nullable ", fullclassname," logentity "); <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw new argumentnullexception (string. format ("the method in {0} occur argumentnullexception. because of the Arg: {1} is not nullable ", fullclassname," logentity ")); </P> <p >}</P> <p> operatingentity innerentity = logentity as operatingen Tity; <br/> If (innerentity = NULL) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = string. format ("the method in {0} occur nullreferenceexception. because of converting type. ", fullclassname); <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw new nullreferenceexception (string. format ("the method in {0} occur nullreference1_tio N. because of converting type. ", fullclassname); <br/>}</P> <p> If (string. isnullorempty (innerentity. MSG) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = string. format ("the method in {0} occur exception. because of the field: MSG is not nullable. ", fullclassname); <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw new exception (S Tring. format ("the method in {0} occur exception. because of the field: MSG is not nullable. ", fullclassname); <br/>}</P> <p> // logs <br/> string SQL = @" insert into operatinglog (ol_uf_id, ol_msg, ol_op_time, ol_li_time, ol_lo_time) <br/> values (: ol_uf_id,: ol_msg,: ol_op_time,: ol_li_time,: ol_lo_time )"; </P> <p> oracleparameter [] paras = new oracleparameter [] <br/> {<br/> New oracleparameter (": ol_uf_id", in Nerentity. userid = NULL? "": Innerentity. userid), <br/> New oracleparameter (": ol_msg", innerentity. MSG), <br/> New oracleparameter (": ol_op_time", innerentity. operatingtime), <br/> New oracleparameter (": ol_li_time", innerentity. logintime), <br/> New oracleparameter (": ol_lo_time", innerentity. logouttime), <br/>}; </P> <p> try <br/>{< br/> oraclehelper. executenonquery (oraclehelper. connectionstringlocaltransaction, commandtype. text, SQL, paras); <br/>}< br/> catch (exception ex) <br/>{< br/> errorentity = new errorentity (); <br/> errorentity. MSG = ex. message + ex. stacktrace; <br/> ilogfactory logfactory = new errorfactory (); <br/> logfactory. log (errorentity); </P> <p> throw; <br/>}</P> <p >}< br/>}
When a client calls a log object, you must first create a new log object and set its attributes, such:

Operatingentity entity = new operatingentity ();

// Set Property

Entity. operatingtime = datetime. now;

.....

Then provide your instance Factory:

Ilogfactory factory = new operatingfactory ();

Factory. Log (entity );

During expansion, you need to build your new log entity class and new instance factory to implement your record logic.

The original implementation does not need to be changed.

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.