Log4Net, ThreadContext и Global.asax

Таким образом, мы не должны блокировать очередь только, чтобы узнать, что это было пусто.

object item;
if (queue.Count > 0)
{
  lock (queue)
  {
    if (queue.Count > 0)
    {
       item = queue.Dequeue();
    }
  }
}
5
задан mai 8 March 2017 в 15:00
поделиться

1 ответ

It is not safe to load request-specific values into ThreadContext like that. The reason is that ASP.NET shared threads to service requests. It does this quite often, in fact.

You could instead use LogicalThreadContext, however that simply stores the values in Call Context, which is used for Remoting.

AFAIK there is no HttpContext specific context storage, so what you can do is instead assign a "value provider" instance as your thread context, and at runtime it will call .ToString() on this class to get the value.

public class HttpContextUserProvider
{
   public override string ToString()
   {
      return HttpContext.Current.User.Identity.Name;
   }
}

It's less than ideal, but it works.

11
ответ дан 18 December 2019 в 10:48
поделиться
Другие вопросы по тегам:

Похожие вопросы: