Skip to content

Commit

Permalink
Update samples (#32)
Browse files Browse the repository at this point in the history
* Add logging

* Introduce controller, rather than anonymous handler; configure logging

* Add `netcoreapp2.1` sample; Rename `examples` directory to `samples` and adjust namespaces

* Fix dependency - for `netcoreapp2.1`, don't depend on `M.E.O.DataAnnotations` (-> apps running on 2.1 won't get options validation)

* Update GitHub's `ci.yml` after sample dir renaming

* Fix sample route

* Update samples; Post-merge fixes
  • Loading branch information
wdolek authored Aug 30, 2020
1 parent ff9729a commit 8123a86
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 40 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ on:
branches: [ master ]
paths-ignore:
- 'docs/**'
- 'examples/**'
- 'samples/**'
- '*.md'
pull_request:
branches: [ master ]
paths-ignore:
- 'docs/**'
- 'examples/**'
- '*.md'

jobs:
Expand Down
19 changes: 17 additions & 2 deletions W4k.AspNetCore.Correlator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution", "solution", "{8F
stylecop.json = stylecop.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "W4k.AspNetCore.Correlator.Example.NetCore3x", "examples\W4k.AspNetCore.Correlator.Example.NetCore3x\W4k.AspNetCore.Correlator.Example.NetCore3x.csproj", "{0FD3E847-FE2A-49B9-B87E-6F15226AFE6F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "W4k.AspNetCore.Correlator.Example.NetCore3.1", "samples\W4k.AspNetCore.Correlator.Example.NetCore3.1\W4k.AspNetCore.Correlator.Example.NetCore3.1.csproj", "{0FD3E847-FE2A-49B9-B87E-6F15226AFE6F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{260DCE5E-0649-40BF-9E55-84DFEBFC4F78}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{260DCE5E-0649-40BF-9E55-84DFEBFC4F78}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{5CA4490C-CA01-4C12-98B2-695FA3D2B821}"
EndProject
Expand All @@ -44,6 +44,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{F009A71B-E
docs\registration.md = docs\registration.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "W4k.AspNetCore.Correlator.Example.NetCore2.1", "samples\W4k.AspNetCore.Correlator.Example.NetCore2.1\W4k.AspNetCore.Correlator.Example.NetCore2.1.csproj", "{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -102,6 +104,18 @@ Global
{0FD3E847-FE2A-49B9-B87E-6F15226AFE6F}.Release|x64.Build.0 = Release|Any CPU
{0FD3E847-FE2A-49B9-B87E-6F15226AFE6F}.Release|x86.ActiveCfg = Release|Any CPU
{0FD3E847-FE2A-49B9-B87E-6F15226AFE6F}.Release|x86.Build.0 = Release|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Debug|x64.ActiveCfg = Debug|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Debug|x64.Build.0 = Debug|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Debug|x86.ActiveCfg = Debug|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Debug|x86.Build.0 = Debug|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Release|Any CPU.Build.0 = Release|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Release|x64.ActiveCfg = Release|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Release|x64.Build.0 = Release|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Release|x86.ActiveCfg = Release|Any CPU
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -113,6 +127,7 @@ Global
{0FD3E847-FE2A-49B9-B87E-6F15226AFE6F} = {260DCE5E-0649-40BF-9E55-84DFEBFC4F78}
{5CA4490C-CA01-4C12-98B2-695FA3D2B821} = {8FE80DF2-87C8-4639-AFB8-C97980565E70}
{59A6F617-4EF1-40BD-8759-280E90DDB789} = {5CA4490C-CA01-4C12-98B2-695FA3D2B821}
{BDA21198-DAEC-48BB-AE3E-B96ADF3F821D} = {260DCE5E-0649-40BF-9E55-84DFEBFC4F78}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5A4C3F87-5A3E-4AD2-AD75-AAE43EB7227A}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using W4k.AspNetCore.Correlator.Context;

namespace W4k.AspNetCore.Correlator.Example.NetCore21.Controllers
{
[Route("api/v1/[controller]")]
[ApiController]
public class HelloController : ControllerBase
{
private readonly ICorrelationContextAccessor _contextAccessor;
private readonly ILogger<HelloController> _logger;

public HelloController(ICorrelationContextAccessor contextAccessor, ILogger<HelloController> logger)
{
_contextAccessor = contextAccessor;
_logger = logger;
}

[HttpGet]
public ActionResult Hello()
{
_logger.LogInformation("Entering hello");

var correlationId = _contextAccessor.CorrelationContext.CorrelationId;
var result = correlationId.IsEmpty
? "<correlation missing>"
: correlationId;

_logger.LogInformation("Request almost finished, correlation: {correlationId}", correlationId);

return Ok(result);
}
}
}
26 changes: 26 additions & 0 deletions samples/W4k.AspNetCore.Correlator.Example.NetCore2.1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;

namespace W4k.AspNetCore.Correlator.Example.NetCore21
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddConsole(options =>
{
options.DisableColors = true;
options.IncludeScopes = true;
});
})
.UseStartup<Startup>();
}
}
51 changes: 51 additions & 0 deletions samples/W4k.AspNetCore.Correlator.Example.NetCore2.1/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using W4k.AspNetCore.Correlator.Extensions;
using W4k.AspNetCore.Correlator.Extensions.DependencyInjection;
using W4k.AspNetCore.Correlator.Options;

namespace W4k.AspNetCore.Correlator.Example.NetCore21
{
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.AddDefaultCorrelator(options =>
{
options.Forward = PropagationSettings.PropagateAs("X-Correlation-Id");
options.Emit = PropagationSettings.PropagateAs("X-Correlation-Id");
options.LoggingScope = LoggingScopeSettings.IncludeLoggingScope("Correlation");
});

services
.AddHttpClient("DummyClient")
.WithCorrelation();

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

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

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>W4k.AspNetCore.Correlator.Example.NetCore21</RootNamespace>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\..\src\W4k.AspNetCore.Correlator\W4k.AspNetCore.Correlator.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"W4k.AspNetCore.Correlator": "Debug"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using W4k.AspNetCore.Correlator.Context;

namespace W4k.AspNetCore.Correlator.Example.NetCore31.Controllers
{
[Route("api/v1/[controller]")]
[ApiController]
public class HelloController : ControllerBase
{
private readonly ICorrelationContextAccessor _contextAccessor;
private readonly ILogger<HelloController> _logger;

public HelloController(ICorrelationContextAccessor contextAccessor, ILogger<HelloController> logger)
{
_contextAccessor = contextAccessor;
_logger = logger;
}

[HttpGet]
public ActionResult Hello()
{
_logger.LogInformation("Entering hello");

var correlationId = _contextAccessor.CorrelationContext.CorrelationId;
var result = correlationId.IsEmpty
? "<correlation missing>"
: correlationId;

_logger.LogInformation("Request almost finished, correlation: {correlationId}", correlationId);

return Ok(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace W4k.AspNetCore.Correlator.Example.NetCore30
namespace W4k.AspNetCore.Correlator.Example.NetCore31
{
public class Program
{
Expand All @@ -12,6 +13,15 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.AddConsole(options =>
{
options.DisableColors = true;
options.IncludeScopes = true;
options.Format = Microsoft.Extensions.Logging.Console.ConsoleLoggerFormat.Default;
});
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using W4k.AspNetCore.Correlator.Extensions;
using W4k.AspNetCore.Correlator.Extensions.DependencyInjection;
using W4k.AspNetCore.Correlator.Options;

namespace W4k.AspNetCore.Correlator.Example.NetCore30
namespace W4k.AspNetCore.Correlator.Example.NetCore31
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// add correlator with additional setting
services.AddCorrelator(options =>
services.AddDefaultCorrelator(options =>
{
// forward, return using `Request-Id` HTTP header
options.Forward = PropagationSettings.PropagateAs("Request-Id");
options.Emit = PropagationSettings.PropagateAs("Request-Id");
options.Forward = PropagationSettings.PropagateAs("X-Correlation-Id");
options.Emit = PropagationSettings.PropagateAs("X-Correlation-Id");
options.LoggingScope = LoggingScopeSettings.IncludeLoggingScope("Correlation");
});

// add named HTTP client
services
.AddHttpClient("DummyClient")
.WithCorrelation();

services.AddControllers();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// use Correlator middleware
app.UseCorrelator();

if (env.IsDevelopment())
Expand All @@ -43,10 +42,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
endpoints.MapControllers();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>W4k.AspNetCore.Correlator.Example.NetCore31</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\W4k.AspNetCore.Correlator\W4k.AspNetCore.Correlator.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information",
"W4k.AspNetCore.Correlator": "Debug"
}
},
"AllowedHosts": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ public static ICorrelatorBuilder AddCorrelator(
throw new ArgumentNullException(nameof(configureOptions));
}

services.AddOptions<CorrelatorOptions>()
.Configure(configureOptions)
.Validate(
o =>
{
return o.ReadFrom.Count > 0;
},
$"Configure at least one correlation HTTP header, see property: {nameof(CorrelatorOptions.ReadFrom)}");
var optionsBuilder = services
.AddOptions<CorrelatorOptions>()
.Configure(configureOptions);

#if NETSTANDARD2_1
optionsBuilder.Validate(
options =>
{
return options.ReadFrom.Count > 0;
},
$"Configure at least one correlation HTTP header, see property: {nameof(CorrelatorOptions.ReadFrom)}");
#endif

services.AddTransient<CorrelatorHttpMessageHandler>()
.AddSingleton<CorrelationContextContainer>()
Expand Down
Loading

0 comments on commit 8123a86

Please sign in to comment.