-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
847a50f
commit 2a111ee
Showing
20 changed files
with
209 additions
and
33 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch API", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
"program": "${workspaceFolder}/src/HappyCode.NetCoreBoilerplate.Api/bin/Debug/netcoreapp3.1/HappyCode.NetCoreBoilerplate.Api.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}/src/HappyCode.NetCoreBoilerplate.Api", | ||
"stopAtEntry": false, | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)", | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"ASPNETCORE_URLS": "http://*:5000", | ||
} | ||
} | ||
] | ||
} | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debug API", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
"program": "${workspaceFolder}/src/HappyCode.NetCoreBoilerplate.Api/bin/Debug/net8.0/HappyCode.NetCoreBoilerplate.Api.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}/src/HappyCode.NetCoreBoilerplate.Api", | ||
"stopAtEntry": false, | ||
"serverReadyAction": { | ||
"action": "openExternally", | ||
"pattern": "\\bNow listening on:\\s+(https?://\\S+)", | ||
"uriFormat": "%s/swagger" | ||
}, | ||
"env": { | ||
"ASPNETCORE_ENVIRONMENT": "Development", | ||
"ASPNETCORE_URLS": "http://localhost:5000", | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/HappyCode.NetCoreBoilerplate.Api/Infrastructure/Logging/VersionEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using HappyCode.NetCoreBoilerplate.Core.Providers; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.Infrastructure.Logging; | ||
|
||
public class VersionEnricher : ILogEventEnricher | ||
{ | ||
private readonly VersionProvider _versionProvider; | ||
|
||
public VersionEnricher(VersionProvider versionProvider) | ||
{ | ||
_versionProvider = versionProvider; | ||
} | ||
|
||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
foreach (var item in _versionProvider.VersionEntries) | ||
{ | ||
logEvent.AddPropertyIfAbsent(new LogEventProperty(item.Key, new ScalarValue(item.Value))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/HappyCode.NetCoreBoilerplate.Core/Providers/VersionProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Collections; | ||
using System.Linq; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Core.Providers; | ||
|
||
public class VersionProvider | ||
{ | ||
private const string PREFIX = "HC_"; | ||
|
||
private readonly Lazy<Dictionary<string, string>> _versionEntries = new(GetVersionEntries); | ||
|
||
private static Dictionary<string, string> GetVersionEntries() | ||
{ | ||
var variables = Environment.GetEnvironmentVariables() | ||
.Cast<DictionaryEntry>() | ||
.Where(x => x.Key.ToString().StartsWith(PREFIX)) | ||
.ToDictionary( | ||
x => x.Key.ToString().Remove(0, PREFIX.Length), | ||
y => y.Value.ToString()); | ||
return variables; | ||
} | ||
|
||
public Dictionary<string, string> VersionEntries => _versionEntries.Value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...pyCode.NetCoreBoilerplate.Api.UnitTests/HappyCode.NetCoreBoilerplate.Api.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...e.NetCoreBoilerplate.Api.UnitTests/HappyCode.NetCoreBoilerplate.Api.UnitTests.runsettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RunSettings> | ||
<RunConfiguration> | ||
<EnvironmentVariables> | ||
<TEST_ENV>TEST_VALUE</TEST_ENV> | ||
<HC_SHA>36b90293</HC_SHA> | ||
<HC_VERSION>9.9.9</HC_VERSION> | ||
</EnvironmentVariables> | ||
</RunConfiguration> | ||
</RunSettings> |
38 changes: 38 additions & 0 deletions
38
...HappyCode.NetCoreBoilerplate.Api.UnitTests/Infrastructure/Logging/VersionEnricherTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using FluentAssertions; | ||
using HappyCode.NetCoreBoilerplate.Api.Infrastructure.Logging; | ||
using HappyCode.NetCoreBoilerplate.Core.Providers; | ||
using Serilog.Events; | ||
using Xunit; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Api.UnitTests.Infrastructure.Logging; | ||
|
||
public class VersionEnricherTests | ||
{ | ||
private readonly VersionEnricher _sut; | ||
|
||
public VersionEnricherTests() | ||
{ | ||
_sut = new VersionEnricher(new VersionProvider()); | ||
} | ||
|
||
[Fact] | ||
public void Properties_should_be_available() | ||
{ | ||
// Arrange | ||
var logEvent = GetEmptyLogEvent(); | ||
|
||
// Act | ||
_sut.Enrich(logEvent, null); | ||
|
||
// Assert | ||
logEvent.Properties["SHA"].ToString().Should().Contain("36b90293"); | ||
logEvent.Properties["VERSION"].ToString().Should().Contain("9.9.9"); | ||
} | ||
|
||
private static LogEvent GetEmptyLogEvent() | ||
{ | ||
return new LogEvent(DateTimeOffset.UtcNow, LogEventLevel.Verbose, null, | ||
new MessageTemplate(Guid.NewGuid().ToString(), []), []); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...Code.NetCoreBoilerplate.Core.UnitTests/HappyCode.NetCoreBoilerplate.Core.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...NetCoreBoilerplate.Core.UnitTests/HappyCode.NetCoreBoilerplate.Core.UnitTests.runsettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RunSettings> | ||
<RunConfiguration> | ||
<EnvironmentVariables> | ||
<TEST_ENV>TEST_VALUE</TEST_ENV> | ||
<HC_SHA>36b90293</HC_SHA> | ||
<HC_VERSION>9.9.9</HC_VERSION> | ||
</EnvironmentVariables> | ||
</RunConfiguration> | ||
</RunSettings> |
27 changes: 27 additions & 0 deletions
27
test/HappyCode.NetCoreBoilerplate.Core.UnitTests/Providers/VersionProviderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using FluentAssertions; | ||
using HappyCode.NetCoreBoilerplate.Core.Providers; | ||
using Xunit; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.Core.UnitTests.Providers; | ||
|
||
public class VersionProviderTests | ||
{ | ||
private readonly VersionProvider _provider; | ||
|
||
public VersionProviderTests() | ||
{ | ||
_provider = new VersionProvider(); | ||
} | ||
|
||
[Fact] | ||
public void Provided_should_returns_expected_values() | ||
{ | ||
// act | ||
var result = _provider.VersionEntries; | ||
|
||
// assert | ||
result.Should().NotContainKeys("TEST_ENV", "HC_SHA", "HC_VERSION"); | ||
result.Should().ContainKeys("SHA", "VERSION"); | ||
result.Should().HaveCount(2); | ||
} | ||
} |