title: Thread safety of the ambient LogContext in Serilog
body: <p>According to <a href="https://nblumhardt.com/2016/08/context-and-correlation-structured-logging-concepts-in-net-5/" rel="nofollow noreferrer">this</a> article, you can attach a request ID to an ambient context (the <code>LogContext</code>) in Serilog like this:</p>
  <pre><code>using (LogContext.PushProperty(&quot;RequestId&quot;, Request.Id))
  {
      // Process request; all logged events will carry `RequestId`
      Log.Information(&quot;Adding {Item} to cart {CartId}&quot;, item, cart.Id);
  }
  </code></pre>
  <p>And that is fine, and seems to be working. I have two scenarios where I need this:</p>
  <ul>
  <li>Add a correlation ID to logs per request to an API.</li>
  <li>Add a correlation ID per message read of a queue.</li>
  </ul>
  <p>What has me confused is what happens if multiple requests hits the API at once, or we process multiple messages concurrently. Since <code>LogContext</code> is a static class, can I accidentally overwrite the <code>RequestId</code> property in the <code>LogContext</code> with values from another thread than the one who originally set the property?</p>
  <p>Lets say thread 1 sets the <code>RequestId</code> to <code>1</code>. Before it is finished, thread 2 sets the <code>RequestId</code> to <code>2</code> because we received another request. Will thread 1 now use <code>2</code> as the value for <code>RequestId</code> when logging? I'm guessing it wont, but can someone explain why? According this <a href="https://github.com/serilog/serilog/wiki/Enrichment" rel="nofollow noreferrer">this</a>, pushing a property with the same name as an existing property onto the stack overwrites it:</p>
  <blockquote>
  <p>Pushing property onto the context will override any existing properties with the same name, until the object returned from PushProperty() is disposed, as the property A in the example demonstrates.</p>
  </blockquote>
  <p>Does Serilog have one stack per handle returned from the <code>LogContext.PushProperty(...)</code> method, or something? Is there any way I can override <code>RequestId</code>, perhaps short of calling <code>LogContext.PushProperty(&quot;RequestId&quot;, Request.Id)</code> again?</p>