Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:14:16+00:00 2026-05-27T14:14:16+00:00

I have a logging framework that I’ve written that has the ability to track

  • 0

I have a logging framework that I’ve written that has the ability to track “logging context”. It has a pluggable strategy framework, however the one I use most often is a ThreadStatic variant that keeps track of context in a [ThreadStatic] variable. I’ve been trying to resolve a problem with logging context in a multi-threaded workflow. The goal is to have all the log entries for all calls across all methods and classes that share a common thread log the same context information. Since each thread should theoretically be getting their own ThreadStatic variable, the idea seemed easy.

public class ThreadStaticLoggingContextStrategy: ILoggingContextStrategy
{
    public ThreadStaticLoggingContextStrategy()
    {
        Debug.WriteLine("[INITIALIZE] A new instance of 'ThreadStaticLoggingContextStrategy' has been created.");
    }

    [ThreadStatic] private LoggingContext _context;

    public LoggingContext GetLoggingContext()
    {
        if (_context == null)
            _context = new LoggingContext();

        return _context;
    }
}

In reality, it seems that ThreadStatic data is actually being SHARED across threads. This goes against everything I understand about threading. I was having a hard time figuring out what the issue was until I threw in an extra log entry that tracked when each thread cleared out thread context (all the threads run on a main loop…at the beginning, if the necessary messages are received, context is initialized, and at the end in a finally clause, its reset.) The following logging is CONSISTENT:

[2011-12-15 16:27:21,233] [DEBUG]
[TPI.LTI.Eventing.GroupCreatedNotificationHandler:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.1_Thread:
324]: (ContextId=184e82dd-152b-4bb5-a2c6-3e05b2365c04;
TransactionId=1a11130e-e8dd-4fa1-9107-3b46dcb4ffd6;
HandlerName=GroupCreatedNotificationHandler;
HandlerId=WORKDEVELOPMENT.1)
Pushing event for tool
‘0967e031-398f-437d-8949-2a17fe844df0’ to
http://tpidev.pearsoncmg.com/tpi/lti/service/event…

[2011-12-15 16:27:21,259] [DEBUG] [TPI.LTI.Facades.LTIFacade:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.1_Thread:
299]: (ContextId=184e82dd-152b-4bb5-a2c6-3e05b2365c04;
TransactionId=1a11130e-e8dd-4fa1-9107-3b46dcb4ffd6;
HandlerName=GroupCreatedNotificationHandler;
HandlerId=WORKDEVELOPMENT.1)
Getting LTI Tool Instance for tool
instance guid 0967e031-398f-437d-8949-2a17fe844df0:

[2011-12-15 16:27:21,318] [DEBUG] [TPI.LTI.Facades.LTIFacade:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.1_Thread:
299]: (ContextId=184e82dd-152b-4bb5-a2c6-3e05b2365c04;
TransactionId=1a11130e-e8dd-4fa1-9107-3b46dcb4ffd6;
HandlerName=GroupCreatedNotificationHandler;
HandlerId=WORKDEVELOPMENT.1)
Found LTI Tool Instance for tool instance
guid 0967e031-398f-437d-8949-2a17fe844df0.

[2011-12-15 16:27:21,352] [DEBUG] [TPI.LTI.Facades.TPIFacade:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.1_Thread:
299]: (ContextId=184e82dd-152b-4bb5-a2c6-3e05b2365c04;
TransactionId=1a11130e-e8dd-4fa1-9107-3b46dcb4ffd6;
HandlerName=GroupCreatedNotificationHandler;
HandlerId=WORKDEVELOPMENT.1)
Publishing event to TPI at
‘http://tpidev.pearsoncmg.com/tpi/lti/service/event’…

[2011-12-15 16:27:21,428] [DEBUG]
[TPI.LTI.Eventing.GroupCreatedNotificationHandler:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.2_Thread:
301]:
[LOG] Resetting logging Context!!

[2011-12-15 16:27:21,442] [DEBUG]
[TPI.LTI.Eventing.GroupCreatedNotificationHandler:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.2_Thread:
299]: No message pending on queue.
GroupCreatedNotificationHandler.WORKDEVELOPMENT.2 handler is
waiting…

[2011-12-15 16:27:22,282] [DEBUG] [TPI.LTI.Facades.TPIFacade:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.1_Thread:
301]: Event published to TPI.

[2011-12-15 16:27:22,283] [DEBUG]
[TPI.LTI.Eventing.GroupCreatedNotificationHandler:
TPI.LTI.Provisioning.Handlers.GroupCreatedNotificationHandler.WORKDEVELOPMENT.1_Thread:
301]: Received a response from provider:

You can see that there are two threads in this particular case, 1_Thread and 2_Thread. I’ve italicized the contextual data that is supposed to be included at the beginning of EVERY log entry for 1_Thread. I’ve bolded the point in 2_Thread where logging context is reset. After that point, all of 1_Thread’s context information is missing. In dozens of tests so far, the context information for all threads is lost after the logging context is reset on another.

Am I misunderstanding ThreadStatic? I’ve been writing C# code since 2001, and I’ve never experienced this behavior before. It seems there is a new ThreadLocal<T> class in .NET 4, however I am not sure if that simply used ThreadStatic internally anyway, and would therefor have the same problem, or if it functions differently (and hopefully more reliably.) Any insight into this problem would be GREATLY APPRECIATED! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:14:16+00:00Added an answer on May 27, 2026 at 2:14 pm

    Because that field isn’t static. It only applies to static fields.

    If this is 4.0, maybe look at ThreadLocal<T>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any .NET logging framework that have ability to switch appender if something
I have a logging table which has three columns. One column is a unique
I have a little logging app (written in wxPython) that receives data from a
I have a framework that uses log4net for logging, I know that we can
I have a project right now that straddles the line on framework and pluggable
We have logging in place for any .Net Framework exceptions that occur in our
I have a small framework that is logging some info and debug messages using
I have just written my own logging framework (very lightweight, no need for a
I currently have a project that uses Entity Framework 4.1 for logging to a
I have spider that I have written using the Scrapy framework. I am having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.