|
| 1 | +# Properly reference Microsoft.ApplicationInsights.Profiler.AspNetCore in your NuGet package |
| 2 | + |
| 3 | +It is common to have shared libraries in a solution, some times, your own NuGet packages. This example describes how to properly build a NuGet package of your own that carries profiler and could be reused by other projects. |
| 4 | + |
| 5 | +In this example, there are 2 projects, `WebAPI` is client project, that's the project we want to turn profiler on. `SharedLib` will be built into a NuGet package to simulate whatever the common package your want to build, with Profiler ready to go. As per the `PackageId` property in [SharedLib.csproj](./SharedLib/SharedLib.csproj), it will be built into `ProfilerExample.SharedLib.1.0.0.nupkg`. |
| 6 | + |
| 7 | +```mermaid |
| 8 | +stateDiagram-v2 |
| 9 | +[*] --> WebAPI |
| 10 | +[*] --> WebAPI2 |
| 11 | +WebAPI --> ProfilerExample.SharedLib(NuGet) |
| 12 | +WebAPI2 --> ProfilerExample.SharedLib(NuGet) |
| 13 | +ProfilerExample.SharedLib(NuGet) --> Microsoft.ApplicationInsights.Profiler.AspNetCore(NuGet) |
| 14 | +
|
| 15 | +note left of WebAPI2: This is another project that need to turn on Profiler. |
| 16 | +note left of WebAPI: This is your project to reference shared NuGet package. |
| 17 | +note right of ProfilerExample.SharedLib(NuGet): This is the shared NuGet package built by you. |
| 18 | +note right of Microsoft.ApplicationInsights.Profiler.AspNetCore(NuGet): This is the Profiler package |
| 19 | +``` |
| 20 | + |
| 21 | +Here's a recommendation: |
| 22 | + |
| 23 | +## Add reference to NuGet package |
| 24 | + |
| 25 | +1. Add reference to `Microsoft.ApplicationInsights.Profiler.AspNetCore` is in `SharedLib`: |
| 26 | + |
| 27 | + ```xml |
| 28 | + <ItemGroup> |
| 29 | + <PackageReference Include="Microsoft.ApplicationInsights.Profiler.AspNetCore" Version="2.*" /> |
| 30 | + </ItemGroup> |
| 31 | + ``` |
| 32 | + |
| 33 | +1. [KEY STEP] Setup the package to exclude `contentFiles` from private assets: |
| 34 | + |
| 35 | + ```xml |
| 36 | + <ItemGroup> |
| 37 | + <PackageReference Include="Microsoft.ApplicationInsights.Profiler.AspNetCore" Version="2.*"> |
| 38 | + <!-- Default is contentfiles;analyzers;build as per: --> |
| 39 | + <!-- https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets --> |
| 40 | + <PrivateAssets>analyzers;build</PrivateAssets> |
| 41 | + </PackageReference> |
| 42 | + </ItemGroup> |
| 43 | + ``` |
| 44 | + Refer to [SharedLib.csproj](./SharedLib/SharedLib.csproj) for details. The key is to setup `<PrivateAssets>analyzers;build</PrivateAssets>` to exclude the `contentFiles` from private assets. |
| 45 | + |
| 46 | + _Tips: You might want to reference `Microsoft.ApplicationInsights.AspNetCore` in your shared project as well._ |
| 47 | + |
| 48 | +## Verify it works |
| 49 | + |
| 50 | +1. Build the package |
| 51 | + |
| 52 | + ```shell |
| 53 | + SharedLib> dotnet pack -o ../Pkgs |
| 54 | + ``` |
| 55 | + |
| 56 | +1. Check the build output in [Pkgs](./Pkgs/) folder, expect to see `ProfilerExample.SharedLib.1.0.0.nupkg`. |
| 57 | +1. Verify the NuGet package local source is configured correctly in [nuget.config](./nuget.config), specifically, the local source is added: |
| 58 | + |
| 59 | + ```xml |
| 60 | + <add key="local" value="Pkgs" /> |
| 61 | + ``` |
| 62 | + |
| 63 | +1. Reference the built package: |
| 64 | + |
| 65 | + ```xml |
| 66 | + <PackageReference Include="ProfilerExample.SharedLib" Version="1.*" /> |
| 67 | + ``` |
| 68 | + |
| 69 | +1. Now build the `WebAPI` project: |
| 70 | + |
| 71 | + ```shell |
| 72 | + WebAPI> dotnet build |
| 73 | + ``` |
| 74 | + |
| 75 | +1. Check that the file of `TraceUpload.zip` is included in the header project output at: `bin/Debug/net6.0/TraceUploader.zip` |
| 76 | + |
| 77 | +1. In any header project, without reference profiler package again, you can then follow the other instructions to enable profiler. |
| 78 | + |
| 79 | +## Feedback |
| 80 | + |
| 81 | +If you have suggestions or if there is any questions, problems, please [file an issue](https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore/issues). |
0 commit comments