Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Gordon committed Dec 19, 2019
2 parents 9d1d04e + 594d6f6 commit ae77ddc
Show file tree
Hide file tree
Showing 51 changed files with 1,286 additions and 160 deletions.
46 changes: 38 additions & 8 deletions CorrelationId.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2003
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CorrelationId", "src\CorrelationId\CorrelationId.csproj", "{865886FC-EBBE-49E1-8F49-FECD0408EF6E}"
EndProject
Expand All @@ -23,7 +23,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{5961B376
.travis.yml = .travis.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcCorrelationIdSample", "samples\MvcCorrelationIdSample\MvcCorrelationIdSample.csproj", "{CDD51CCC-0764-4C8C-8EA2-2DAAAF4CE0F9}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2.1", "2.1", "{0537FAD6-C2F9-48A9-A428-43430B079AC6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3.1", "3.1", "{E28C5481-A68F-44AF-983F-EA127E70621A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcSample", "samples\3.1\MvcSample\MvcSample.csproj", "{9393676B-BE08-44C9-B556-7BE31CFA5B86}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcSample", "samples\2.1\MvcSample\MvcSample.csproj", "{244E25C9-847A-49E0-B413-F4CA3E8F7CF4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcSampleTests", "samples\3.1\MvcSampleTests\MvcSampleTests.csproj", "{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2.2", "2.2", "{C7C340B3-FDDA-465A-B196-04B2EB8C8C58}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcSample", "samples\2.2\MvcSample\MvcSample.csproj", "{67E39614-131D-4C0E-9119-A1D7CF2DEEEA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,18 +51,36 @@ Global
{41035337-9829-4A6A-8EA9-42CC94734CE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41035337-9829-4A6A-8EA9-42CC94734CE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41035337-9829-4A6A-8EA9-42CC94734CE6}.Release|Any CPU.Build.0 = Release|Any CPU
{CDD51CCC-0764-4C8C-8EA2-2DAAAF4CE0F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDD51CCC-0764-4C8C-8EA2-2DAAAF4CE0F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDD51CCC-0764-4C8C-8EA2-2DAAAF4CE0F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDD51CCC-0764-4C8C-8EA2-2DAAAF4CE0F9}.Release|Any CPU.Build.0 = Release|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9393676B-BE08-44C9-B556-7BE31CFA5B86}.Release|Any CPU.Build.0 = Release|Any CPU
{244E25C9-847A-49E0-B413-F4CA3E8F7CF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{244E25C9-847A-49E0-B413-F4CA3E8F7CF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{244E25C9-847A-49E0-B413-F4CA3E8F7CF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{244E25C9-847A-49E0-B413-F4CA3E8F7CF4}.Release|Any CPU.Build.0 = Release|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F}.Release|Any CPU.Build.0 = Release|Any CPU
{67E39614-131D-4C0E-9119-A1D7CF2DEEEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67E39614-131D-4C0E-9119-A1D7CF2DEEEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67E39614-131D-4C0E-9119-A1D7CF2DEEEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67E39614-131D-4C0E-9119-A1D7CF2DEEEA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{865886FC-EBBE-49E1-8F49-FECD0408EF6E} = {9A14C73A-D8FC-40C3-81FC-D0687F43FEAF}
{41035337-9829-4A6A-8EA9-42CC94734CE6} = {EAF27B74-0B27-4BEE-9F82-DD5812B21B17}
{CDD51CCC-0764-4C8C-8EA2-2DAAAF4CE0F9} = {34C0F65A-8BF2-40DA-B0E7-844930EE2A7B}
{0537FAD6-C2F9-48A9-A428-43430B079AC6} = {34C0F65A-8BF2-40DA-B0E7-844930EE2A7B}
{E28C5481-A68F-44AF-983F-EA127E70621A} = {34C0F65A-8BF2-40DA-B0E7-844930EE2A7B}
{9393676B-BE08-44C9-B556-7BE31CFA5B86} = {E28C5481-A68F-44AF-983F-EA127E70621A}
{244E25C9-847A-49E0-B413-F4CA3E8F7CF4} = {0537FAD6-C2F9-48A9-A428-43430B079AC6}
{0E2E2678-4DBB-4560-AF0E-86ECAF8D9B6F} = {E28C5481-A68F-44AF-983F-EA127E70621A}
{C7C340B3-FDDA-465A-B196-04B2EB8C8C58} = {34C0F65A-8BF2-40DA-B0E7-844930EE2A7B}
{67E39614-131D-4C0E-9119-A1D7CF2DEEEA} = {C7C340B3-FDDA-465A-B196-04B2EB8C8C58}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C0A37404-C50B-472E-9491-DDE2A4BDA882}
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![NuGet](https://img.shields.io/nuget/v/CorrelationId.svg)](https://www.nuget.org/packages/CorrelationId)
[![NuGet](https://img.shields.io/nuget/dt/CorrelationId.svg)](https://www.nuget.org/packages/CorrelationId)
[![Build Status](https://travis-ci.org/stevejgordon/CorrelationId.svg?branch=dev)](https://travis-ci.org/stevejgordon/CorrelationId)
[![Build Status](https://stevejgordon.visualstudio.com/CorrelationId/_apis/build/status/stevejgordon.CorrelationId)](https://stevejgordon.visualstudio.com/CorrelationId/_build/latest?definitionId=1)

This repo contains middleware for syncing a TraceIdentity (correlation ID) across ASP.NET Core APIs.

Expand All @@ -12,6 +13,21 @@ The TraceIdentifier on the HttpContext will be used for new requests and additio

Examples in the [wiki](https://github.com/stevejgordon/CorrelationId/wiki).

## Known Issue with ASP.NET Core 2.2.0

It appears that a [regression in the code for ASP.NET Core 2.2.0](https://github.com/aspnet/AspNetCore/issues/5144) means that setting the TraceIdentifier on the context via middleware results in the context becoming null when accessed further down in the pipeline. A fix is ready for 3.0.0 and the team plan to back-port this for the 2.2.2 release timeframe.

A workaround at this time is to disable the behaviour of updating the TraceIdentifier using the options when adding the middleware...

```
app.UseCorrelationId(new CorrelationIdOptions
{
Header = "X-Correlation-ID",
UseGuidForCorrelationId = true,
UpdateTraceIdentifier = false
});
```

## Installation

You should install [CorrelationId with NuGet](https://www.nuget.org/packages/CorrelationId/):
Expand Down
15 changes: 15 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pool:
vmImage: 'Ubuntu 16.04'

variables:
buildConfiguration: 'Release'

steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'

- task: DotNetCoreCLI@2
inputs:
command: test
projects: '**/test/*/*.csproj'
arguments: '--configuration $(buildConfiguration)'
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using CorrelationId;
using CorrelationId.Abstractions;
using Microsoft.AspNetCore.Mvc;

namespace MvcCorrelationIdSample.Controllers
namespace MvcSample.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
Expand Down
15 changes: 15 additions & 0 deletions samples/2.1/MvcSample/MvcSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\CorrelationId\CorrelationId.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace MvcCorrelationIdSample
namespace MvcSample
{
public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using CorrelationId;
using CorrelationId.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MvcCorrelationIdSample
namespace MvcSample
{
public class Startup
{
Expand All @@ -20,7 +21,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

services.AddCorrelationId();
services.AddDefaultCorrelationId();

services.AddScoped<ScopedClass>();
services.AddTransient<TransientClass>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CorrelationId;
using CorrelationId.Abstractions;

namespace MvcCorrelationIdSample
namespace MvcSample
{
public class ScopedClass
{
Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions samples/2.2/MvcSample/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using CorrelationId.Abstractions;
using Microsoft.AspNetCore.Mvc;

namespace MvcSample.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly ICorrelationContextAccessor _correlationContext;

public ValuesController(ICorrelationContextAccessor correlationContext)

{
_correlationContext = correlationContext;
}

// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
var correlation = _correlationContext.CorrelationContext.CorrelationId;

return new []
{
$"DirectAccessor={correlation}",
$"TraceIdentifier={HttpContext.TraceIdentifier}"
};
}
}
}
15 changes: 15 additions & 0 deletions samples/2.2/MvcSample/MvcSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\CorrelationId\CorrelationId.csproj" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions samples/2.2/MvcSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace MvcSample
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
29 changes: 29 additions & 0 deletions samples/2.2/MvcSample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59922/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "http://localhost:59922/api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MvcCorrelationIdSample": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:59923/api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:59923/"
}
}
}
43 changes: 43 additions & 0 deletions samples/2.2/MvcSample/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CorrelationId;
using CorrelationId.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace MvcSample
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

services.AddDefaultCorrelationId();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseCorrelationId(new CorrelationIdOptions
{
UpdateTraceIdentifier = true
});

app.UseMvc();
}
}
}
15 changes: 15 additions & 0 deletions samples/2.2/MvcSample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
47 changes: 47 additions & 0 deletions samples/3.1/MvcSample/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;

namespace MvcSample.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<WeatherForecastController> _logger;
private readonly IHttpClientFactory _httpClientFactory;

public WeatherForecastController(ILogger<WeatherForecastController> logger, IHttpClientFactory httpClientFactory)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogInformation("This is a log message to show the scope.");

var client = _httpClientFactory.CreateClient("MyClient");

client.GetAsync("https://www.example.com");

var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
Loading

0 comments on commit ae77ddc

Please sign in to comment.