'Can Serilog capture method parameters

I am playing with Serilog and capturing all sorts of great information when an exception is thrown. This new test however is not producing quite as much information as I would like.

On this line of code I am passing a null parameter:

SetPaginationMetrics(ref actualApiReturn, pageNumber, actualApiReturn.data.totalPages);

I do get a "null ref exception" and it does log the stack but I was hoping it might be able to pinpoint the offending parameter. I tried a StackTrace (https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stackframe.getfilecolumnnumber?view=netcore-3.1#System_Diagnostics_StackFrame_GetFileColumnNumber) to get the column but it reports the column where the line of code begins NOT the column of the null parameter.

I was wondering if there is an enricher or some way of capturing my parameters to the log file.

I could litter my code with _logger.LogInfo() before every method call but that somewhat defeats why I have logging enrichers, logging and exception handling middleware.

The same scenario exist is you have a parameter that is of the wrong type passed.

So can I somehow identify an offending parameter in the logs without LogInfo() statement all over or are my expectations unrealistic?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source