C # Aspect-oriented programming--monitoring logging scheme

Source: Internet
Author: User

Background: Now the company as a whole in the monitoring platform, the requirements of the various parts of the details are recorded, in the front page display, so now need to do is a monitoring log record work, today is the example of rendering monitoring logs.

Status: The current renderer does not reserve code access for monitoring logging, so it is bound to add new code to the new monitoring log. But the code that simply adds logging to the existing logic just doesn't seem reasonable: because many of the code for logging is the same, there are a lot of duplicate code in the code, the original code is online and running well, the new code destroys the original package, and even increases the likelihood of error, and from a design standpoint, Logging does not belong to any one of the features, the new log and the execution intent of each method is incompatible, so a new way of logging is required.

Solution:

Data structure: Design a common method log record class, including ID, method name, execution time length, log result (normal record normal information, Exception Log error message), log result is exception flag bit and so on, and list<string> contains the parameter name used in the method, The value of the parameter variable in the List<object> method, which implements the serialization interface or attribute, so that the method context information can be logged and the error is helped. All method execution logs use this class to form a uniform common method for performing state information logging classes.

Logging: For all methods that require logging, a dynamic proxy is used to handle the class in which the method resides. In the proxy method before and after the original method of the new log records, dynamic agent using the adorner pattern implementation is commonly referred to as AOP,AOP aspects of the content see: https://msdn.microsoft.com/zh-cn/library/dn574804.aspx. The above is the method of the existing class to log the use of dynamic proxy class, for the method inside the log records, the use of dynamic proxy stopwatch to achieve. Because the internal log of the method is generally to count the length of time, dynamic proxy stopwatch the Start method and the Stop method, the execution time of the method and the value of the intermediate parameter variables are recorded after the start and stop calls.


Get parametric: For the parameters of the method is easy to get, the dynamic agent will pass the parameters to the proxy method, you can use the object pool to get and save the handle of the parameter, in order to prevent the object pool holding parametric so that it can not be reclaimed by the garbage collector when exiting the method, holding all the parameter variables in the way of weak reference. For local variables inside methods, the method of saving to the object pool is instantiated in the proxy method. The object pool is implemented in a dictionary, the object pool key is represented by a string, and the method name + variable name + thread context hash value is recorded, implementing the suffix of the different parameter key (thread context hash value) and the different parameter values of the same parameter in multi-thread recording. This allows you to get the value of the local variable ahead of the method and output it to the log.

Object Pool Implementation: The above mentioned object pools are stored in a dictionary, but when does the object pool garbage collection, clean up garbage data? At this point we have to define a method of adding objects to the object pool, the first part of this method is to clean up the garbage data, so that each new data to delete the old data, so as to ensure the object pool data validity. The local variables should be set in the proxy method before the method is added, in the original code to add the time to get from the object pool, if there is already used before the original code to add the value, no or has been garbage collected to regenerate new data.

Source:

https://download.csdn.net/download/hirisw/10486250

C # Aspect-oriented programming--monitoring logging scheme

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.