Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug code in NuGet package created by me

I have a NuGet package I created and installed in another solution but now I need to debug the code of the package when called from my new solution.

I tried referencing the solution of the package but it's not working.

I am using Visual Studio 2013.

like image 218
Yatiac Avatar asked Jun 09 '17 12:06

Yatiac


People also ask

How do I debug a custom NuGet package?

Enable Symbol Server Support In order to debug into NuGet package libraries, Visual Studio must be configured to use ProGet as a symbol server. To do this select Debug > Options, from the menu bar, then browse to Debugging > Symbols in the tree menu.

How do I debug NuGet source code?

In order to use Source Code to debug the code form a NuGet package, we need to enable it in Visual Studio. Under the Debug menu, click on Options. First, under General, tick the option to Enable Source Link support. Next, we need to add the NuGet symbol server to the locations where Visual Studio look symbol files.


2 Answers

To debug any dll you need the symbol file of it (.pdb). If you build your project in the debug configuration you will see that those files are generated and put in the build output folder.

Visual studio loads those symbol files from different places as described here. The easiest way to debug your nuget packages is to put the .pdb files of the packages in the build output folder of the project you want to debug.


If the code you are trying to debug is classified as non-user code you need to uncheck Just My Code in the debugging options.

enter image description here

The following quote from the Microsoft - Visual Studio Docs shows what counts as user and what as non-user code.

User and non-user code

To distinguish user code from non-user code, Just My Code looks at symbol (.pdb) files and program optimizations. The debugger considers code to be non-user code when the binary is optimized or when the .pdb file is not available.

Three attributes also affect what the debugger considers to be My Code:

  • DebuggerNonUserCodeAttribute tells the debugger that the code it is applied to is not My Code.
  • DebuggerHiddenAttribute hides the code from the debugger, even if Just My Code is turned off.
  • DebuggerStepThroughAttribute tells the debugger to step through the code it is applied to, rather than step into the code.

All other code is considered to be user code.

A more detailed answer can be found on my blog.

like image 88
NtFreX Avatar answered Sep 21 '22 07:09

NtFreX


For Visual Studio 2017 and your nuget package source code hosted on GitHub or BitBucket:

  1. Enable full debug information in *.csproj file:

    <PropertyGroup Condition="'$(Configuration)'=='Debug'">   <DebugType>full</DebugType>   <DebugSymbols>true</DebugSymbols> </PropertyGroup> 

    or right-click project properties, build, advanced, output debugging information - set to full.

  2. To enable automatic source download and stepping for your nuget package dll, add nuget package SourceLink.Create.CommandLine to your project, or add it manually into *.csproj file:

    <ItemGroup>   <PackageReference Include="SourceLink.Create.CommandLine" Version="2.8.2" PrivateAssets="All" />  </ItemGroup> 

    More info here.

  3. In tools - options - debugging, disable "Enable Just My Code", and enable "Suppress JIT optimization on module load (Managed Only)".

    After this, you should be able to step inside methods from your nuget package dll.

like image 30
xhafan Avatar answered Sep 18 '22 07:09

xhafan