Skip to content

AspNet Response Cookie Layout Renderer

Rolf Kristensen edited this page Aug 28, 2023 · 11 revisions

Renders the Cookie Name and Values stored in the ASP.NET Response Cookies collection.

Platforms Supported: All

Introduced in NLog.Web.AspNetCore v5.1.0 and NLog.Web v5.1.0

Configuration Syntax

${aspnet-response-cookie:items=String[]:outputFormat=Enum
    :itemSeparator=String:valueSeparator=String
    :valuesOnly=Boolean}

Parameters

Rendering Options

  • items - Cookie key name(s). A list of keys can be passed as comma separated values, e.g.: Key1,Key2 (Not specified means all)

    Introduced with NLog.Web / NLog.Web.AspNetCore v5.2.1, and replaces the cookieNames-option.

  • exclude - Cookie key names to be excluded. A list of keys can be passed as comma separated values, e.g.: Key1,Key2

Formatting options

  • outputFormat - Renders as flat string or as JSON. Default: Flat.

    • Flat - Key-value-pair as standard string
    • JsonArray - Key-value-pair in JSON-array
    • JsonDictionary - Key-value-pair as JSON-dictionary.
  • itemSeparator - Separator between items. Default: ,. Only applies when OutputFormat is Flat.

  • valueSeparator - Separator between key and value. Default: =. Only applies when OutputFormat is Flat.

  • objectSeparator - Separator between object-collections of key-value-pair-items. Default: ;. Only applies when OutputFormat is Flat and Verbose is True.

  • ValuesOnly - Only render the values of the key/value pairs. Default: false.

  • LowerCaseKeys - Convert Keys to lowercase. Default: false.

    Introduced in NLog.Web / NLog.Web.AspNetCore 5.1.1

  • Verbose - Render additional properties of the HttpCookie-object (Domain, Path, Expires, Secure, HttpOnly).

Examples

In C# code:

Request.Cookies["key1"] = "value1";
Request.Cookies["id"] = "d4b20a34-6231-4201-83a6-c72599e41164";

Log single cookie in default Flat output format

${aspnet-response-cookie:CookieNames=key1}

Will print:

"key1=value1"

Log multiple cookies in default Flat output format

${aspnet-response-cookie:items=key1,id}

Will print:

"key1=value1,id=d4b20a34-6231-4201-83a6-c72599e41164"

Log single cookie in JSON output format

${aspnet-response-cookie:items=key1:OutputFormat=JSON}

Will print:

[{"key1":"value1"}]

Log multiple cookies in JSON output format

${aspnet-response-cookie:items=key1,id:OutputFormat=JSON}

Will print:

[{"key1":"value1","id":"d4b20a34-6231-4201-83a6-c72599e41164"}]

Log single cookie as JSON dictionary output format

${aspnet-response-cookie:items=key1:OutputFormat=JsonDictionary}

Will print:

{"key1":"value1"}

Log single cookie in Flat output format as value only

${aspnet-response-cookie:items=key1:ValuesOnly=true}

Will print:

"value1"

Log single cookie in JSON output format as value only

${aspnet-response-cookie:items=key1:OutputFormat=JSON:ValuesOnly=true}

Will print:

["value1"]

Log multiple cookies in JSON output format as value only

${aspnet-response-cookie:items=key1,id:OutputFormat=JSON:ValuesOnly=true}

Will print:

["value1","d4b20a34-6231-4201-83a6-c72599e41164"]
Clone this wiki locally