diff --git a/examples/ReReleaseNuGet/.gitignore b/examples/ReReleaseNuGet/.gitignore new file mode 100644 index 0000000..f4c13ee --- /dev/null +++ b/examples/ReReleaseNuGet/.gitignore @@ -0,0 +1 @@ +Pkgs \ No newline at end of file diff --git a/examples/ReReleaseNuGet/Readme.md b/examples/ReReleaseNuGet/Readme.md new file mode 100644 index 0000000..6e33350 --- /dev/null +++ b/examples/ReReleaseNuGet/Readme.md @@ -0,0 +1,81 @@ +# Properly reference Microsoft.ApplicationInsights.Profiler.AspNetCore in your NuGet package + +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. + +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`. + +```mermaid +stateDiagram-v2 +[*] --> WebAPI +[*] --> WebAPI2 +WebAPI --> ProfilerExample.SharedLib(NuGet) +WebAPI2 --> ProfilerExample.SharedLib(NuGet) +ProfilerExample.SharedLib(NuGet) --> Microsoft.ApplicationInsights.Profiler.AspNetCore(NuGet) + +note left of WebAPI2: This is another project that need to turn on Profiler. +note left of WebAPI: This is your project to reference shared NuGet package. +note right of ProfilerExample.SharedLib(NuGet): This is the shared NuGet package built by you. +note right of Microsoft.ApplicationInsights.Profiler.AspNetCore(NuGet): This is the Profiler package +``` + +Here's a recommendation: + +## Add reference to NuGet package + +1. Add reference to `Microsoft.ApplicationInsights.Profiler.AspNetCore` is in `SharedLib`: + + ```xml + + + + ``` + +1. [KEY STEP] Setup the package to exclude `contentFiles` from private assets: + + ```xml + + + + + analyzers;build + + + ``` + Refer to [SharedLib.csproj](./SharedLib/SharedLib.csproj) for details. The key is to setup `analyzers;build` to exclude the `contentFiles` from private assets. + + _Tips: You might want to reference `Microsoft.ApplicationInsights.AspNetCore` in your shared project as well._ + +## Verify it works + +1. Build the package + + ```shell + SharedLib> dotnet pack -o ../Pkgs + ``` + +1. Check the build output in [Pkgs](./Pkgs/) folder, expect to see `ProfilerExample.SharedLib.1.0.0.nupkg`. +1. Verify the NuGet package local source is configured correctly in [nuget.config](./nuget.config), specifically, the local source is added: + + ```xml + + ``` + +1. Reference the built package: + + ```xml + + ``` + +1. Now build the `WebAPI` project: + + ```shell + WebAPI> dotnet build + ``` + +1. Check that the file of `TraceUpload.zip` is included in the header project output at: `bin/Debug/net6.0/TraceUploader.zip` + +1. In any header project, without reference profiler package again, you can then follow the other instructions to enable profiler. + +## Feedback + +If you have suggestions or if there is any questions, problems, please [file an issue](https://github.com/microsoft/ApplicationInsights-Profiler-AspNetCore/issues). \ No newline at end of file diff --git a/examples/ReReleaseNuGet/SharedLib/SharedLib.csproj b/examples/ReReleaseNuGet/SharedLib/SharedLib.csproj new file mode 100644 index 0000000..7c046b6 --- /dev/null +++ b/examples/ReReleaseNuGet/SharedLib/SharedLib.csproj @@ -0,0 +1,21 @@ + + + + netstandard2.1 + enable + true + ProfilerExample.SharedLib + + + + + + + + analyzers;build + + + + + + \ No newline at end of file diff --git a/examples/ReReleaseNuGet/WebAPI/Controllers/WeatherForecastController.cs b/examples/ReReleaseNuGet/WebAPI/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..086e7cc --- /dev/null +++ b/examples/ReReleaseNuGet/WebAPI/Controllers/WeatherForecastController.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Mvc; + +namespace WebAPIOne.Controllers; + +[ApiController] +[Route("[controller]")] +public class WeatherForecastController : ControllerBase +{ + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } +} diff --git a/examples/ReReleaseNuGet/WebAPI/Program.cs b/examples/ReReleaseNuGet/WebAPI/Program.cs new file mode 100644 index 0000000..464aa18 --- /dev/null +++ b/examples/ReReleaseNuGet/WebAPI/Program.cs @@ -0,0 +1,27 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddApplicationInsightsTelemetry(); +builder.Services.AddServiceProfiler(); + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/examples/ReReleaseNuGet/WebAPI/WeatherForecast.cs b/examples/ReReleaseNuGet/WebAPI/WeatherForecast.cs new file mode 100644 index 0000000..392afea --- /dev/null +++ b/examples/ReReleaseNuGet/WebAPI/WeatherForecast.cs @@ -0,0 +1,12 @@ +namespace WebAPIOne; + +public class WeatherForecast +{ + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } +} diff --git a/examples/ReReleaseNuGet/WebAPI/WebAPI.csproj b/examples/ReReleaseNuGet/WebAPI/WebAPI.csproj new file mode 100644 index 0000000..6e39684 --- /dev/null +++ b/examples/ReReleaseNuGet/WebAPI/WebAPI.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/examples/ReReleaseNuGet/WebAPI/appsettings.Development.json b/examples/ReReleaseNuGet/WebAPI/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/examples/ReReleaseNuGet/WebAPI/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/examples/ReReleaseNuGet/WebAPI/appsettings.json b/examples/ReReleaseNuGet/WebAPI/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/examples/ReReleaseNuGet/WebAPI/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/examples/ReReleaseNuGet/nuget.config b/examples/ReReleaseNuGet/nuget.config new file mode 100644 index 0000000..1edc693 --- /dev/null +++ b/examples/ReReleaseNuGet/nuget.config @@ -0,0 +1,9 @@ + + + + + + + + +