A logging component that behaves as HTML

Source: Internet
Author: User
Tags cdata log4net

About logging, if you do not want to use with the write, and do not want to use log4net words, perhaps you can understand this!

I am also a very lazy person, the program of things, as long as others have written, and their own use of cool, do not repeat the coding. The origin of this component is because feeling log4net too strong too complex (for me really), when I want to use log4net, I see the configuration I have to scare the urine n times, and this time heart and whim, with HTML to do the performance of the log! There are two reasons, one is HTML and CSS Everyone, the second is to achieve a good readable visibility.

I. Description of the configuration file

<?XML version= "1.0" encoding= "Utf-8"?><Loggerconfig>  <style>. fatal{background-color:red}. error{background-color:purple;    }. warn{Background-color:yellow;    }. info{Background-color:green;    }. debug{Background-color:gray;    }. myinfo{font-size:24px; }  </style>  <Tpls>    <TPLname= "Fatal">      <! [Cdata[<p> <span class= "Fatal" >{type}</span>:{message}<br/> {date} & Lt;/p>]]>    </TPL>    <TPLname= "Error" >      <! [Cdata[<p> <span class= "error" >{type}</span>:{message}<br/> {date} & Lt;/p>]]>    </TPL>    <TPLname= "Warn">      <! [Cdata[<p> <span class= "warn" >{type}</span>:{message}<br/> {date} &L T;/p>]]>    </TPL>    <TPLname= "Info">      <! [Cdata[<p> <span class= "info" >{type}</span>:{message}<br/> {date} &L T;/p>]]>    </TPL>    <TPLname= "Debug">      <! [Cdata[<p> <span class= "Debug" >{type}</span>:{message}<br/> {date} & Lt;/p>]]>    </TPL>  </Tpls>  <logs>    <Logname= "Default"Path= "D:\ABC"Types= "All"datefmt= "Yyyy-mm-dd HH:mm:ss">      <TPLname= "Info">        <! [Cdata[<p> <span class= "info MyInfo" >{type}</span>:{message}<br/> {date} </p>]]>      </TPL>    </Log>    <Logname= "Log2"Path= "D:\abc\a.html"Types= "Fatal,error,warn,info,debug">    </Log>  </logs></Loggerconfig>
Loggerconfig.xml

Although the above configuration is not short, Sasa Yangyang 80 big line, but it really only 3 (or 2 and a half) very simple node composition.

1, style node, this does not say, and HTML page style node is a kind of, used to define the following template in the TPL style.

2, Tpls node, here is defined as the default template of the log type , template content Everyone knows, is the HTML tag, which has three placeholder {type}, {message}, {date}, log type and {type} refers to a kind of thing , respectively, is fatal,error,warn,info,debug five types, which is referred to Log4net, where it is called the log level.

3, logs nodes, each sub-node is a log node defines the properties and behavior of a logger object.

First look at the properties of the log node:

1) name is a unique table of knowledge, used to obtain a unique logger object (internal cache mechanism, with Tiaogan, no need to worry), name= "default" means that logger

2) path is a directory or a specific file, the log file will be generated according to the date, as the "D:\ABC" will generate multiple this named file with the date (D:\abc\yyyy\MM\ dd.html), when the file is specific, it should be a. html or. htm file, and the log will append the file.

3) types is the above mentioned 5 types, empty means that nothing is recorded, more than one type separated by commas, such as only the fatal error and warning, that is types= "Fatal,warn", if you want to record all, can be types= "all" to simply represent types= "Fatal,error,warn,info,debug".

4) Datefmt This is the format of the {date} in the template, and the default is "Yyyy-mm-dd HH:mm:ss", which is called as a parameter of datetime.tostring () when logging.

Sub-nodes of the log node:

And the above Tpls sub-nodes, are defined template used, the above is the default, here is a special, optional, we should be able to think of the definition here will override the default, but actually it is indeed so ~_~! Because it is duplicated content, it is said that the configuration file is composed of 2 half nodes.

Second, call the API

After the component DLLs are introduced into the project, set the path to the logger configuration file in app. Config and Web. config, such as:

  < appSettings >    <  key= "Loggerconfig"  value= "Loggerconfig.xml"/>  </appSettings>

1. Get Logger Object

            // get the default logger, which is the one            name= "Default" var logger = logger.getinstance ();             // get the object            of name= "log2" var log2 = logger.getinstance ("log2");

2, simple record log

            Logger. Debug ("debugdebugdebug");            Logger. Info ("infoinfoinfoinfoinfoinfo");

3, over-the-record log, because the content of the log is likely to be dynamic, so the component provides all the logging method can be passed to a delegate to get rid of those things they do not care about

            // This is not going to be recorded, starting to be filtered            out for the numbers Logger. Error ("2323ErrorErrorErrorError", (s) =>regex.ismatch (S,@ "^\d+ " ));             // This will record, the contents of the log is 10, will not be filtered            Logger. Fatal ("fatalfatalfatalfatal");

4, the HTML display, began to say with HTML, the effect is what? (Here is not beautiful, does not mean that does not support the graceful layout, the programmer's interface, you understand!) )

For programmers who prefer simple programming! Download

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.