title: Log4j2: unable to set MDC keys using Servlet Context Listener
body: <p>I'm trying to set the hostname as a MDC variable using a Servlet Context Listener. The MDC variable gets available only in the listener itself, but it disappears if i try to log from other servlets.</p>
  
  <p>Here my listener code:</p>
  
  <pre><code>public class MdcServletListener implements ServletContextListener {
  
      /** logger. */
      private static Logger logger = LoggerFactory
              .getLogger(MdcServletListener.class);
  
      /*
       * (non-Javadoc)
       * 
       * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
       * ServletContextEvent)
       */
      @Override
      public void contextDestroyed(ServletContextEvent sContext) {
  
      }
  
      /*
       * (non-Javadoc)
       * 
       * @see
       * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
       * .ServletContextEvent)
       */
      @Override
      public void contextInitialized(ServletContextEvent sContext) {
          try {
              ThreadContext.put("LISTENER", "TEST");
              logger.info("Inizializzazione variabili mdc completata");
          } catch (Exception e) {
              logger.error("Inizializzazione variabili mdc fallita", e);
          }
  
      }
  
  }
  </code></pre>
  
  <p>Here my web.xml:</p>
  
  <pre><code>&lt;listener&gt;
          &lt;listener-class&gt;org.apache.logging.log4j.web.Log4jServletContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;MdcServletListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;log4jServletFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.apache.logging.log4j.web.Log4jServletFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;log4jServletFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
          &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
          &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
          &lt;dispatcher&gt;INCLUDE&lt;/dispatcher&gt;
          &lt;dispatcher&gt;ERROR&lt;/dispatcher&gt;
      &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>Any suggestion?
  Thanks.</p>