Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#if(DEBUG) and log4net line number source/runtime mismatch

When I use preprocessor directives like

1 #if(DEBUG)
2 // 1
3 // 2
4 #else
5 // 1
6 // 2
7 #endif
8
9 logger.Debug("Log exception, etc."); 

it causes that at runtime the line numbers in the log (eg. stack trace) are incorrect - in the above example line 9 would become 4 because the rest would be parsed by the preprocessor.

It makes log analysis quite difficult.

Is there a way to solve this issue without creating methods with ConditionalAttribute?

I'm aware of Debugger.IsAttached (and I'm using this solution now) but I would prefer to run code based on the build mode (debug/release) not on whether the debugger is attached.

like image 998
surfen Avatar asked Nov 04 '22 10:11

surfen


1 Answers

I checked again in a completely separate project and as Ross Patterson suggests, the exception trace is correct.

Unfortunately I already refactored my (large) project with Debugger.IsAttached and the problem went away so I (groundlessly as it turns out) suspected that preprocessor directives were the issue. Maybe the .pdb were not in sync with the binaries.

Since I'm unable to reproduce the issue now I consider it solved. It's good to know that this scenario is supported. Thanks Ross Patterson!

like image 170
surfen Avatar answered Nov 09 '22 10:11

surfen