title: Intercept ThreadContext of log4j2 in Spring AOP Around Advice
body: <ul>
  <li><p>I have a spring web-app which receives some URI request from UI and calls business logic in Service classes to perform a certain operation.</p>
  
  <ul>
  <li>I'm using log4j2 JDBC logging to log in Database.</li>
  <li>Now I want to log session-Id in a different column in Database. And for this, I'm using ThreadContext Map to pass session-Id to be saved as mentioned here: <a href="https://logging.apache.org/log4j/2.x/manual/thread-context.html" rel="nofollow noreferrer">https://logging.apache.org/log4j/2.x/manual/thread-context.html</a></li>
  </ul></li>
  </ul>
  
  <p>Below is a snapshot of how I'm using:</p>
  
  <p>log4j configuration:</p>
  
  <pre><code>&lt;Appenders&gt;
          &lt;Jdbc ignoreExceptions="true" name="db-appender"
          bufferSize="${env:LOG_DB_BUFFER_SIZE}" tableName="test.&amp;quot;LOGS&amp;quot;"&gt;
          &lt;ConnectionFactory class="com.pritam.logging.ConnectionFactory" method="getDatabaseConnection" /&gt;
          &lt;Column name="dated" isUnicode="false" isEventTimestamp="true" /&gt;
          &lt;Column name="logger" isUnicode="false" pattern="%logger" /&gt;
          &lt;Column name="level" isUnicode="false" pattern="%level" /&gt;
          &lt;Column name="message" isUnicode="false" pattern="%message" /&gt;
          &lt;Column name="exception" isUnicode="false" pattern="%ex{full}" /&gt;
          &lt;Column name="session_id" isUnicode="false" pattern="%X{session_id}"/&gt;
      &lt;/Jdbc&gt;
  &lt;/Appenders&gt;
  </code></pre>
  
  <p>Sample method:</p>
  
  <pre><code>public void doSomething(String id, JobHeader jobHeader) {
      ThreadContext.put("session_id", jobHeader.getContext().getSessionId());
  
      //Business Logic
      logger.debug("Logging Message");
      //Business Logic
  
      ThreadContext.clearAll();        
  }
  </code></pre>
  
  <p>This works fine. And logs session id in a different column.</p>
  
  <p>Now, I have n numbers of <strong>doSomething</strong> methods which are going to be executed in independent threads having various session-ids. 
  And I don't want to write ThreadContext Statements over and over again, So I'm thinking of using Spring-AOP advice (@Around Advice) for this task.</p>
  
  <p>Can someone please explain to me how to inject the ThreadContext in @Around advice in Spring-AOP.</p>
  
  <p>Thank You</p>