Visual Studio - How to remove a reference in Relea

2019-03-23 06:38发布

I'm developing a library for use in other apps and this library has lots of debugging and logging statements thanks to NLog.

Is it possible to exclude the reference to NLog.dll when I switch to release mode?

Cheers,

5条回答
一纸荒年 Trace。
2楼-- · 2019-03-23 07:02

You can manually edit the csproj file, and do something like this:

<Reference Include="NLog" Condition="'$(Configuration)' == 'Debug'" />

This only makes it reference that assembly in Debug. I wouldn't recommend doing this often though, because this behavior isn't reflected in the references list in Visual Studio when you change the configuration. It does work when compiling though

查看更多
聊天终结者
3楼-- · 2019-03-23 07:10

The only way I know is to take the reference out completely and call the assembly via reflection. Then, you should only log if the assembly loads.

查看更多
不美不萌又怎样
4楼-- · 2019-03-23 07:11

There is no reason for removing a reference in case you are sure that no code will be in use from that DLL. In that case you can simply remove DLL.

查看更多
混吃等死
5楼-- · 2019-03-23 07:12

Why would you want to do that?

If you want to stop logging, you can programatically turn off the logging. The performance hit will be minimal. (I have had great success with NLog even when logging cross process.)

Otherwise, you need to wrap it as described above.

-Scott

查看更多
啃猪蹄的小仙女
6楼-- · 2019-03-23 07:14

I can't think of a good way to do this. Unless maybe you wrote a stub reference for NLog.dll. Since you are using the reference in your code I don't see how you could just remove it in your release.

Probably too late now but in the future you could write a class to wrap NLog.dll and then just change it in one place so it wouldn't actually log in the release version. Or have some sort of flag.

查看更多
登录 后发表回答