configuring NLog's layout to show all the event-context entries

c#, nlog

Solution

There is a built-in layout renderer in NLog which serves that purpose - ${all-event-properties}. By default it emits all event context properties in key-value style, comma separated.

Checkout the docs for more details https://github.com/NLog/NLog/wiki/All-Event-Properties-Layout-Renderer

Problem

im currently in the process of developing a webapi2 application, and in the stages of conducting my logs using NLog. in my application i log in a key-value manner using the LogEventInfo.Properties dictionary in this way: ``` thisController.LogParams.Add("ControllerName",controllerName); thisController.LogParams.Add("ActionName", actionName); thisController.LogParams.Add("TotalTime", actionWatch.ElapsedMilliseconds); LogEventInfo logEvent = new LogEventInfo() { Message = string.IsNullOrEmpty(thisController.LogMessage)? "Finished Successfuly":thisController.LogMessage, Level = LogLevel.Info, TimeStamp = DateTime.Now }; logEvent.Properties.Merge(thisController.LogParams); logger.Log(logEvent); ``` everything works fine, however I cant seem to find the way to render the layout so it prints all the key-value entries that are in the LogEventInfo.Properties dictionary. lets assume my target is a file, then i have to explicitly mention the key name, is there a way to render it to show all the content of the dictionary ? this is how i do it today, where i can log only the entries i know of: ``` <target name="f1" xsi:type="File" fileName="${basedir}\logs\log.txt" maxArchiveFiles="60" archiveNumbering="Date" archiveDateFormat="yyyyMMdd" archiveEvery="Day" layout="${longdate} : ${callsite:className=true:methodName=true} : ${event-context:item=ControllerName} : ${event-context:item=ActionName} : ${event-context:item=TotalTime} : ${message}" /> ```

Original source