Skip to content

Commit 51111d7

Browse files
authored
Merge pull request #178 from xiaomi7732/dev/saars/doc-re-release-nuget
Add an example about re-pack Microsoft.ApplicationInsights.Profiler.AspNetCore
2 parents 68b3313 + d12afc2 commit 51111d7

10 files changed

Lines changed: 214 additions & 0 deletions

File tree

examples/ReReleaseNuGet/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pkgs

examples/ReReleaseNuGet/Readme.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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).
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
7+
<PackageId>ProfilerExample.SharedLib</PackageId>
8+
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<!-- Default is contentfiles;analyzers;build as per: -->
13+
<!-- https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets -->
14+
<PackageReference Include="Microsoft.ApplicationInsights.Profiler.AspNetCore" Version="2.*">
15+
<PrivateAssets>analyzers;build</PrivateAssets>
16+
</PackageReference>
17+
18+
<!-- This is not required. Decide by your scenario whether you want to carry the latest application insights for ASP.NET Core or not -->
19+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.20.0" />
20+
</ItemGroup>
21+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace WebAPIOne.Controllers;
4+
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
8+
{
9+
private static readonly string[] Summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
private readonly ILogger<WeatherForecastController> _logger;
15+
16+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
[HttpGet(Name = "GetWeatherForecast")]
22+
public IEnumerable<WeatherForecast> Get()
23+
{
24+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
25+
{
26+
Date = DateTime.Now.AddDays(index),
27+
TemperatureC = Random.Shared.Next(-20, 55),
28+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
29+
})
30+
.ToArray();
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddApplicationInsightsTelemetry();
5+
builder.Services.AddServiceProfiler();
6+
7+
builder.Services.AddControllers();
8+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
9+
builder.Services.AddEndpointsApiExplorer();
10+
builder.Services.AddSwaggerGen();
11+
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (app.Environment.IsDevelopment())
16+
{
17+
app.UseSwagger();
18+
app.UseSwaggerUI();
19+
}
20+
21+
app.UseHttpsRedirection();
22+
23+
app.UseAuthorization();
24+
25+
app.MapControllers();
26+
27+
app.Run();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace WebAPIOne;
2+
3+
public class WeatherForecast
4+
{
5+
public DateTime Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
11+
<PackageReference Include="ProfilerExample.SharedLib" Version="1.*" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
5+
<clear />
6+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
7+
<add key="local" value="Pkgs" />
8+
</packageSources>
9+
</configuration>

0 commit comments

Comments
 (0)