2

I'm experimenting with CodeSite Express bundled into Delphi XE. I want to use the Category feature like this:

CodeSite.Category := 'SomeCategory';
CodeSite.EnterMethod ('SomeMethod');
try
  DoSomething;
finally
  CodeSite.ExitMethod ('SomeMethod');
end;

The problem is that if DoSomething contains logging code as well that sets the category, than the ExitMethod will end up in a different category and will blow up the whole hierarchy in the viewer.

Even worse if threading kicks in: Setting the Category and then invoking log commands is obviously not an atomic operation, so two threads that use CodeSite logging cannot really use the Category. At least that's how it looks like in my logs. I would have expected the Category to be thread-local but it seems not.

What's the right pattern to deal with categories in the context of nested logging and threading?

Thanks!

jpfollenius
  • 16,456
  • 10
  • 90
  • 156

1 Answers1

6

Create another Codesite Object, one for each thread perhaps, and set that objects category.

There are various ways you can do this. You could have a specific object like MyThreadCodesite : TCodeSite; which you write to, or you could define a property for the thread which is called "Codesite" and references that object. Thus your thread code will look exactly the same, in that it says "Codesite.Send('hello');" but references the thread's object.

This works well once you have it done. I have about ten TCodesite objects in one project, and the colouring allows you to see which part of the system is doing what easily.

mj2008
  • 6,647
  • 2
  • 38
  • 56
  • 1
    Interesting. We are using multiple codesite objects as well. Each is logging to a different file. Each also has to support multiple categories. We are loath to incur time penalties, so we don't lock and currently "suffer" the coloring sometimes going wrong. And as we can have an enormous number of (mostly dormant) threads around, I am not sure giving each its own codesite instance(s) is going to work for us. Especially as our current codesite descendant doesn't use the dispatcher but logs to file directly. But seeing your answer has given me a few ideas I am going to pursue... Thanks! – Marjan Venema Oct 07 '11 at 12:44