How could I pass data to child method's interceptor from parent method

Jul 3, 2014 at 10:29 AM
Let's say I have two methods as below - Parent and Child. When logging Child method, I want to know which parentId is now being processed. That mean in the LogHandler of Child method, I could get the parentId. Is doing so possible in Unity?
        [LogHandler] //Unity call handler
        public virtual void Parent(int parentId)
            //do something
        public virtual void Child()
Jul 6, 2014 at 6:22 PM
I don't think there is much available in Unity to help you.

Here are some options:

It looks as though you are using a VirtualMethodInterceptor. If that is the case and your object lifetime allows you could set an instance variable and access it within the CallHandler. The downside of this is that the CallHandler now has to know about your specific object which is bad code smell. Also, the call handler would have to know about what methods were called (could maybe use a StackFrame or build it in to your class as well).

Another approach is to set the context information you need into some shared state (singleton, ThreadStatic, etc.).

I'm curious why you need to know the parentId in the Child LogHandler? Are you trying to correlate requests?

Randy Levy
Enterprise Library support engineer
Support How-to
Jul 7, 2014 at 6:36 AM
Edited Jul 7, 2014 at 6:39 AM
thanks Randylevy,

the background needing parentId in the Child LogHandler is that: I persist the log asynchronously, so let's say there are 100 consumer calling Parent() method with pareentId from 1 to 100. The in the log, you may see:
Parent(int parentId) is called with parameters {parentId:2} 00:00AM
Parent(int parentId) is called with parameters {parentId:1} 00:01AM
Child(int childId) is called with parameters{childId: 10} 00:02AM
Child(int childId) is called with parameters{childId: 11} 00:03AM  __(as the log is persisted asynchronously, so the log are not shown in sequence.)__
then I don't know that is childId:10 generated by parentId:1 or parentId:2, as the log are not in sequence. I could put the attribute as [LogHandler("parentId")] so in the Parent LogHandler I could extract the parentId from IMethodInvocation.Arguments and then log. but the problem is that how could I pass the parentId from Parent LogHandler to Child LogHandler.

Jul 7, 2014 at 6:55 AM
Edited Jul 10, 2014 at 3:48 AM
Unfortunately, I don't think there is a way to get the information using Unity.

It does sound like you are trying to correlate requests. If so, you might want to think about using an ActivityId or a logical operation like the one provided by CorrelationManager. Then instead of relying on the data (which in general might also be hard to distinguish in separate requests) you could use a GUID or some other value that is useful.

Randy Levy
Enterprise Library support engineer
Support How-to
Jul 8, 2014 at 5:20 AM
thanks Randy, I will check CorrelationManager out.