MDCContext

class MDCContext(val contextMap: MDCContextMap = MDC.getCopyOfContextMap()) : AbstractCoroutineContextElement, ThreadContextElement<MDCContextMap>

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.

Constructors

Link copied to clipboard
fun MDCContext(contextMap: MDCContextMap = MDC.getCopyOfContextMap())

Types

Link copied to clipboard
object Key : CoroutineContext.Key<MDCContext>

Properties

Link copied to clipboard
val contextMap: MDCContextMap

The value of MDC context map.