I’m using NLog and logging events to SQL server. One of the fields logged is the log level, this is logged as a string, ‘Info’, ‘Warn’ etc. I also want to log the level as an integer value to enable me to provide better sorting when viewing the log events in a GUI.
Is it possible to convert enums to integers for inserting into a database without writing a custom layout renderer?
The configuration line to log the level into a database as a string is
<parameter name="@Level" layout="${level}"/>
Ideally there might be a conversion to integer, like;
<parameter name="@LevelId" layout="${level:format=tointeger}"/>
much like there is a format=tostring converter.
Here is a custom LayoutRenderer that I wrote for just this purpose. I wrote it against NLog 1.0, so it might not fully adhere to NLog 2.0 conventions for LayoutRenderer (e.g. I think that GetEstimatedBufferSize is no longer used):