Detect if you're running in a debug or release executable at runtime

守給你的承諾、 提交于 2021-01-29 05:38:32

问题


I have a library that will be built and published as a nuget package in release mode.

However for debugging purposes it is useful to capture stacktraces at various points.

Capturing a stack trace is relatively expensive, and I don't want to do it in release. However neither do I want to force everyone to replace my nuget package with the debug version when they want to debug their code.

Is there a way to check if the executable that a dll is running in was compiled with debug or release? In other words even though my nuget package was compiled with release, I want to run different code depending on whether or not the executable that my library is used by was built in release or debug?


回答1:


The lines below appear to be in conflict with each other:

Is there a way to check if the executable that a dll is running in was compiled with debug or release? In other words even though my nuget package was compiled with release, I want to run different code

Usually it is possible to do something similar to what you are after by using pre-processor directives. This would allow your program to execute different paths according to for instance, the name of the configuration used to build the project:

#if debug
    //Log
#endif

However, you seem to be after something different. It seems that you want to change behaviour but keeping the same build (release in both cases).

To cater for this, it might be easier to have a flag, say verbose which is false by default and when enabled, it logs the extra information. This would allow you to keep the same build mechanism but be able to log more information accordingly.

Edit: As per your comments, what I mean is something like this:

In your code that calls the nuget, you would have something like so:

#if DEBUG
    var x = new NugetInstance(verbose:true...);
#else 
    var x = new NugetInstance(verbose:false...);
#endif


来源:https://stackoverflow.com/questions/56752377/detect-if-youre-running-in-a-debug-or-release-executable-at-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!