Esframework Use Tips (1)-Automatic assembly of Esframework loggers

Source: Internet
Author: User
Tags foreach

Many of the important components in the Esframework are equipped with logger Iesblogger, which is used to record errors and exceptions when the frame runs. These important components expose a property called "Esblogger", which is used to set the value method to inject the logger.

When I built the application, I used spring.net to assemble the components, and in the spring XML configuration file, I needed to configure the "Esblogger" attribute for these important components, so that you had to be very careful, even myself, in order to not omit and configure the error. Because I don't remember which components are equipped with Iesblogger or not in hundreds of components. It would be nice if you could automatically assemble a logger for the components you need--this can be done. This is achieved because we already have two prerequisites:

(1) If a component in esframework requires a logger, it exposes a property named "Esblogger".

(2) The spring container provides an indirect way for us to traverse each object in the container.

If you can assemble the logger automatically, the spring configuration file can be used without regard to the Assembly of any component's logger, and the world will be simpler.

First, we get a list of all the objects in the spring container:

ArrayList objList = new ArrayList() ;
      string[] names = MainClass.SpringContext.GetObjectDefinitionNames() ;
      foreach(string name in names)
      {
        objList.Add(MainClass.SpringContext.GetObject(name)) ;
      }

Next, write a reusable method for setting the specified property for each object in the list, skipping if an object does not have a specified property:

public static void SetProperty(IList objs ,string propertyName ,object proValue)
    {
      object[] args = {proValue} ;
      foreach(object target in objs)
      {
        Type t = target.GetType() ;
        if(t.GetProperty(propertyName) == null)
        {
          continue ;
        }

        t.InvokeMember(propertyName ,BindingFlags.Default | BindingFlags.SetProperty ,null ,target ,args) ;
      }
    }

Here, it has come to a ripe:

//获取日志记录器组件
      object esbLogger = MainClass.SpringContext.GetObject("esbLogger") ;
      //装配
      SetProperty(objList ,"EsbLogger" ,esbLogger) ;

If a component in later Esframework needs to assemble a logger and add a new "Esblogger" attribute, then our spring configuration file will not have to make any changes, and the runtime will automatically assemble it.

This scenario for automatic assembly of loggers is not fully applicable because it relies on the spring container, what if we don't use a spring-like IOC container in an application built on Esframework?

The answer, I haven't found, because if there is no container, how can I get all the objects in the current application? Without getting all the objects, you cannot use the reflection above to dynamically assemble the logger.

Before, I thought of a solution that would slightly modify the implementation of each component in the esframework that needs to be equipped with a logger, so that their objects are registered to the public util (which can be registered in the component's constructor) when they are created, so that you can get a list of objects from Util. But this scheme is too intrusive and makes each component dependent on util, so it is abandoned.

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.