Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog destructuring operator - does it destructure ahead of log level check

Tags:

c#

.net

serilog

I am trying to understand the performance impact of serilog destructuring operator, specifically to verify that destructure is not performed before the logger checks if the intended message can even be logged. For example, the following code:

var sensorInput = new { Latitude = 25, Longitude = 134 };
Log.Debug("Processing {@SensorInput}", sensorInput);

should not result in something like:

var tmp = destructure(sensorInput);

readonly bool _isDebug = Log.IsEnabled(LogEventLevel.Debug);
if (_isDebug) Log.Debug("Processing {var1}", tmp);

I did check the Serilog source code but I haven't found anything which would confirm the above is not happening - perhaps someone knows the relevant code?

like image 357
aleksander_si Avatar asked Nov 26 '25 02:11

aleksander_si


1 Answers

Serilog will only apply destructuring if the event is enabled. Debug() calls Write(), which does a level check before any more work is performed.

like image 158
Nicholas Blumhardt Avatar answered Nov 28 '25 17:11

Nicholas Blumhardt