Text log help class of the SAS framework -- textloghelper

Source: Internet
Author: User

When talking about logs, everyone knows that it is nothing more than recording: who, when, what was done/what was wrong, isn't it easy. This time, I will also give you a step-by-step tutorial on how to write your own log help classes.

Before starting, we need to analyze the log help class (This mainly refers to text logs.) Which of the following points should be noted:

1) is the log help class static or used?

2) How does the log producer pass in the parameter or?

3) how to write, synchronize, or asynchronously write logs?

4). Log File storage location and file size control.

5) scaling issues.

 

For the time being, I have only found so many items. I hope you can add them.

Next I will discuss how to handle these issues.

I. Is the log help class static or used?

My choice is non-static class, that is, get the log help class object by instantiating, and then call the method to write logs.CodeIs:

Textloghelper loghelper = new textloghelper ();

Loghelper. writelog (......);

Why? In fact, many of the things I wrote are static, such as static classes, static objects, and static methods. They are easy to write and call. Why not. But in the constant lives of coders, I gradually feel that sometimes static things are not good, for example:

1) static public field/attribute: concurrency problem, because the caller may modify the value. This is the case;

2) public method: number of parameters. Because it is a static method, the information required inside the method can only be passed in through the parameter situation;

3) maintenance of static fields and attributes naturally increases the difficulty of maintenance because there are too many static fields and they are too complex.

4) Object-orientedProgramThe core of development is polymorphism, so if static classes are used, polymorphism cannot be realized.

I will not list the other items here. Another point that needs to be pointed out is that static classes have these problems, but they do not mean they are not suitable for static classes in any situation, such as tool classes and data conversion classes, the static class method can be used for implementation. Therefore, whether static classes are used or not depends on the actual situation.

 

Ii. How does the log producer pass in the parameter or?

Iii. How to Write logs, synchronous or asynchronous? 

4. Log File storage location and file size control.

5. Expansion problems.

I am going to talk about the second, third, fourth, and fifth points in one piece.

In fact, there are two types of logs: Fixed logs, such as system logs or internal framework logs, and user logs, such as in Web applications, different users may log on with different logs.

The following two constructor functions of the text log class in the SAS framework are provided:

Private textloghelper (string logsavepath, bool asynmechanic ismlog, int logfilesize ){...}

Public textloghelper (func <iloguser> getloguserhandler, string logsavepath, bool asynmechanic ismlog, int logfilesize ){...}

Public textloghelper (iloguser loguser, string logsavepath, string asynmechanic ismlog, string logfilesize ){...}

Through the above constructor, you may have seen the above clues:

1) control over the log file storage path, the log Content storage in synchronous/asynchronous mode, and the log file size by using the constructor parameters.

2) log users use iloguser to facilitate expansion. There is actually an iloghelper interface in terms of expansion, And the textloghelper to be mentioned in this article is inherited from the iloghelper interface.

3) logs are transmitted in two ways. The first is the delegate method, which allows the caller to determine who the log is. The second is to directly pass the log user object in.

The iloghelper interface code is provided below.

 

1 Namespace SAS. Interface
2 {
3 # Region Using
4 Using System;
5 # Endregion
6 ///   <Summary>
7 /// Namespace: SAS. Interface
8 /// Creater: juvy [david.telvent@gmail.com]
9 /// Create Time: 7:50:47, 2011-11-13
10 /// Function: log help class base class
11 ///   </Summary>
12 Public Interface Iloghelper
13 {
14 ///   <Summary>
15 /// Log User used by the current log help class
16 ///   </Summary>
17 Func <iloguser> getloguserhandler { Get ; Set ;}
18 ///   <Summary>
19 /// Write log information
20 ///   </Summary>
21 ///   <Param name = "message"> Log Information </Param>
22 Void Writelog ( String Message );
23 ///   <Summary>
24 /// Write log information
25 ///   </Summary>
26 ///   <Param name = "message"> Log Information </Param>
27 ///   <Param name = "methodname"> Name of the method with the current error </Param>
28 Void Writelog ( String Message, String Methodname );
29 ///   <Summary>
30 /// Write log information
31 ///   </Summary>
32 ///   <Param name = "ex"> Exception </Param>
33 Void Writelog (exception ex );
34 ///   <Summary>
35 /// Write log information
36 ///   </Summary>
37 ///   <Param name = "ex"> Exception </Param>
38 ///   <Param name = "message"> Log Information </Param>
39 Void Writelog (exception ex, String Message );
40 ///   <Summary>
41 /// Write log information
42 ///   </Summary>
43 ///   <Param name = "ex"> Exception </Param>
44 ///   <Param name = "message"> Log Information </Param>
45 ///   <Param name = "methodname"> Name of the method with the current error </Param>
46 Void Writelog (exception ex, String Message, String Methodname );
47 }
48 }

The log Interface contains five log writing methods, one for obtaining the attributes of log users. It should be easy to implement, so we will not talk about how to implement it here, so we will leave it to ourselves.

Let's talk about the two shortcomings of this log help class:

1) log level issues

2) Log Type Problems

 

Now, the text log class of the SAS framework is here. Thank you for your patience!

 

 

ASP. NET development technology exchange group: 67511751

In addition, I am looking for like-minded people who can communicate with me about technology or provide encouragement and support. If You Are the One, thank you!

QQ: 1054930154

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.