MDCContext

MDC context element for CoroutineContext.

Example:

MDC.put("kotlin", "rocks") // Put a value into the MDC context

launch(MDCContext()) {
    logger.info { "..." }   // The MDC context contains the mapping here
}

Note that you cannot update MDC context from inside of the coroutine simply using MDC.put. These updates are going to be lost on the next suspension and reinstalled to the MDC context that was captured or explicitly specified in contextMap when this object was created on the next resumption. Use withContext(MDCContext()) { ... } to capture updated map of MDC keys and values for the specified block of code.

Parameters

contextMap - the value of MDC context map. Default value is the copy of the current thread’s context map that is acquired via MDC.getCopyOfContextMap.

Types

Key

companion object Key : Key<MDCContext>

Key of MDCContext in CoroutineContext.

Constructors

<init>

MDCContext(
    contextMap: MDCContextMap = MDC.getCopyOfContextMap())

MDC context element for CoroutineContext.

Properties

contextMap

val contextMap: MDCContextMap

The value of MDC context map.