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>

1
  • You can either write them manually, or preprocess your code before compilation so that a tool writes them out for you, or you can do that at runtime and incur a significant cost on every function call. Your choice. Commented Jul 12, 2013 at 15:05

1 Answer 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; 

    } 
} 
Sign up to request clarification or add additional context in comments.

1 Comment

just tryed it out. I like it very much, unfortunately logging feature is only available in paid version. anyway thank you so much for advice! Now its clear for me what way I should move

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.