- 2 minutes to read

Formula - Context

The context Formula function returns the value of one or more Message Context properties (key/value pairs) of the Log Event, such as a correlation ID or an HTTP header.

Syntax

context('Key')
context(regex('Key pattern'))

Parameters

Parameter Required Description
'Key' Yes (first form) The exact name of the context property, in single quotes.
regex('Key pattern') Yes (second form) A regex on the key names. Every context property whose key matches contributes its value.

Returns

  • context('Key') returns the value of that context property, or nothing when the Log Event has no such key.
  • context(regex('Key pattern')) returns the values of all matching context properties; duplicates are collapsed (see Formula language rules).

To get the key names instead of the values, use contextKey.

Examples

Example 1: Read the correlation ID

Input

Key Value
Correlation Id demo001
Header#Content-Type Application/XML

Formula

context('Correlation Id')

Result

demo001

Formula context expression returning the Correlation Id value from the Message Context tab
The Correlation Id key matches exactly, so only its value demo001 is returned. The screenshot writes Context; function names are case-insensitive.

Example 2: Read the status of every order key

Input

Key Value
Order#123 OK
Order#456 OK
Order#789 Fail

Formula

context(regex('Order[#].*'))

Result

OK
Fail

All three keys match the pattern; the duplicate OK is collapsed into one unique value, so the test shows 2 unique matches.

  • Write a literal # as [#] or \#. Formula regexes ignore unescaped whitespace and treat # as the start of a comment, so 'Order#.*' silently runs as 'Order' and returns nothing.
  • Match the whole key. context(regex(...)) reads the keys whose matched text equals a key name, so end the pattern with .*; '^Order[#]' matches only Order# and finds nothing.

See the Formula language rules for the details.

Formula context with a regex on the key returning the unique values OK and Fail from three Order keys
Three keys match Order[#].*, but the two OK values collapse into one, so the test reports 2 unique matches.

Example 3: Read XML stored in a context value

Input

Key Value
OrderNumber <Id>3</Id>

Formula

xpath('Id', context('OrderNumber'))

Result

3

Formula xpath on a context value returning the Id 3 from the XML stored in the OrderNumber key
context returns the XML text stored in OrderNumber, and xpath reads the Id element from it; the same pattern works with jsonPath for JSON values.

Notes

  • The context key in context('Key') must match the stored key name exactly, including spaces and prefixes such as Header#.
  • context reads the Message Context only; it never reads the message body. Use body for the payload.

Next Steps