1

I am logging method name and its parameters values as shown below (pseudo code):

class TestClass
{
    void Method(int Param1, int Param2, int Param3)
    {
       _logger->AddLogEntry("Method",Param1, Param2, Param3);
      ....
      <some work here>
      ....
    }

    void AnotherMethod(int Param)
   {
       _logger->AddLogEntry("AnotherMethod",Param);

      ....
      <some work here>
      ....
   }
  .....
}

I am a bit confused about need to write all parameters manually as input parameters in AddLogEntry method. Its probably to forget one or more parameters when signature of loggable method changed. Is there any way to automate this step. I mean take all values of all parameters of current method. It would be great if I could take also name of the current method and pair <param name> - <value>

4

1 回答 1

1

You can use PostSharp for logging operations, it's work by the Aspect-oriented programming model.

It's based on attributes and reflection.

To be more specifically, take a look at their LogAttribue documentation.

Here is a simple example from their website for logging:

[assembly:  
Log(AttributeTargetTypes="Contoso.Crm.Orders.*", 
    AttributeTargetMemberAttributes = MulticastAttributes.Public)]  

public class OrderFulfillmentService  
{ 
    public bool IsValid( Order order )  
    { 
        if ( order.Lines.Count == 0 ) 
          return false; 

        if ( order.Amount < 0 ) 
          return false; 

        return true; 

    } 
} 
于 2013-07-12T15:02:22.107 回答