What's the use of ContextItems in the Logging Application Block?

Topics: Logging Application Block
Jan 6, 2010 at 1:32 PM

Hi All,

    I'm studying the sourcecode of the logging application block. And I don't know what on earth is the use of ContextItems Class? Who can tell me the details about ContextItems and System.Runtime.Remoting.Messaging.CallContext(I know ContextItems Class uses CallContext, but I don't know why use them.) ?

      Thanks a lot.

                                  Ron

 

 

 

Jan 7, 2010 at 4:01 AM

Hi,

My understanding of the ContextItems class is that it holds the key value pair which will be merge to ExtendedProperties of the LogEntry Class, You can look at the unit test to see how to use ContextItems, see the ContextItemsFixture class.

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.

entlib.support@avanade.com

Jan 7, 2010 at 3:39 PM
Edited Jan 7, 2010 at 4:19 PM

Hi AvanadeSupport,

Thanks a lot. You are right.  The ContextItems class is that it holds the key value pair which will be merge to ExtendedProperites of the LogEntry Class with the ProcessContextItems Method.

Another problem, the value of The ContextItems must be inherited from ILogicalThreadAffinative?

Ron

Jan 8, 2010 at 2:41 AM

Hmm, what version of entlib source code are you using?  The ContextItems class in 4.1 doesn't inherit from that interface.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

entlib.support@avanade.com

 

Jan 8, 2010 at 3:38 AM
Edited Jan 8, 2010 at 7:22 AM

Hi Sarah Urmeneta

Maybe I made a mistake. Because the ContextItems class use the System.Runtime.Remoting.Messaging.CallContext to allow a remoting client and a remote object to share information, I thought the ContextItems class was used to share information between the clients and the server. But it's said "When a remote method call is made to an object in another AppDomain, the CallContext class generates a LogicalCallContext instance that travels along with the remote call. Only objects that expose the ILogicalThreadAffinative interface" on MSDN.

The Codes from the 4.1 version Entlib's ContextItems class:

[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  public void SetContextItem(object key, object value)
  {
   Hashtable contextItems = (Hashtable)CallContext.GetData(CallContextSlotName);
   if (contextItems == null)
   {
    contextItems = new Hashtable();
   }
   contextItems[key] = value;
   CallContext.SetData(CallContextSlotName, contextItems);
  }

So I think the contextItems should be inherited from ILogicalThreadAffinative.

Now If the client send a LogEntry to the server, the client's CallContext will be not sent ? Is the ContextItems Class used on the local thread or process?

Ron

 

 

 

Jan 12, 2010 at 5:45 AM

Hi,

We'll ask some input from the ent lib team about this.

Valiant Dudan
Global Technology & Solutions
Avanade, Inc.

entlib.support@avanade.com

Jan 12, 2010 at 6:07 AM

Hi AvanadeSupport,

I made a test with the ContextItems class. I used the SetContextItem method to set the values and a LogEntry object on the client , then called a remoting method to log it. The server could log the values and the LogEntry object.

I 'm confused. Why the values can be sent to the server.  I thought it shouldn't be sent because of the contextItems(it's a hashtable in the logging block.'s sourcecode) not inherited from ILogicalThreadAffinative .

Thanks your help. Waiting for your answers.

Ron