Skip to content

Commit 8ffbe1b

Browse files
committed
Initial version
1 parent 6f447be commit 8ffbe1b

16 files changed

+5388
-1
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
obj

Diff for: .vscode/launch.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": ".NET Core Launch (console)",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
10+
"args": [],
11+
"cwd": "${workspaceRoot}",
12+
"externalConsole": false,
13+
"stopAtEntry": false,
14+
"internalConsoleOptions": "openOnSessionStart"
15+
},
16+
{
17+
"name": ".NET Core Attach",
18+
"type": "coreclr",
19+
"request": "attach",
20+
"processId": "${command.pickProcess}"
21+
}
22+
]
23+
}

Diff for: .vscode/tasks.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "0.1.0",
3+
"command": "dotnet",
4+
"isShellCommand": true,
5+
"args": [],
6+
"tasks": [
7+
{
8+
"taskName": "build",
9+
"args": [
10+
""
11+
],
12+
"isBuildCommand": true,
13+
"problemMatcher": "$msCompile"
14+
}
15+
]
16+
}

Diff for: README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
# SceneSkope.Extensions.Logging.RequestPathFilter.Internal
1+
# SceneSkope.Extensions.Logging.RequestPathFilter
22
Asp.Net Core RequestPath logging filter
3+
4+
Simple Asp.Net core logging filter to allow a request path to be ignored.
5+
Very useful to ignore probe paths when running behind an application gateway or load balancer.
6+
7+
To use add the following line to Startup.cs
8+
9+
var filteredFactory = loggerFactory.WithRequestPathFilter("/api/probe");
10+
11+
And use filteredFactory in the as the logger factory

Diff for: appveyor.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: '1.0.{build}'
2+
skip_tags: true
3+
image: Visual Studio 2015
4+
configuration: Release
5+
install:
6+
- ps: mkdir -Force ".\build\" | Out-Null
7+
- ps: Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile ".\build\installcli.ps1"
8+
- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetcli"
9+
- ps: '& .\build\installcli.ps1 -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath -Version 1.0.0-preview2-003121'
10+
- ps: $env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
11+
build_script:
12+
- ps: ./Build.ps1
13+
test: off
14+
artifacts:
15+
- path: artifacts/SceneSkope.*.nupkg
16+
deploy:
17+
- provider: NuGet
18+
api_key:
19+
secure: eaiytjZ8451bGqdim49VhzfsYNDviMmEm/lMbc34oqmznMUEb0BW/9/yFckWyzf1
20+
skip_symbols: true
21+
on:
22+
branch: /^(master|dev)$/
23+
- provider: GitHub
24+
auth_token:
25+
secure: ka0RY4J4jxtefevVfoAUgl+rQu1wmy1mb5ccHp9Gc87DZazKdITimPttRZuGGQ56
26+
artifact: /SceneSkope.*\.nupkg/
27+
tag: v$(appveyor_build_version)
28+
on:
29+
branch: master
30+
Binary file not shown.
Binary file not shown.

Diff for: build.ps1

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
echo "build: Build started"
2+
3+
Push-Location $PSScriptRoot
4+
5+
if(Test-Path .\artifacts) {
6+
echo "build: Cleaning .\artifacts"
7+
Remove-Item .\artifacts -Force -Recurse
8+
}
9+
Get-ChildItem -rec -Filter bin | Remove-Item -Recurse -Force
10+
Get-ChildItem -Recurse -Filter obj | Remove-Item -Recurse -Force
11+
12+
$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
13+
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
14+
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"]
15+
16+
$modifyVersion = $false
17+
$modifiedVersion = "1.0.0-*"
18+
if (($env:APPVEYOR_BUILD_VERSION -ne $NULL) -and ($suffix -eq "")) {
19+
$modifyVersion = $true
20+
$modifiedVersion = $env:APPVEYOR_BUILD_VERSION
21+
22+
Get-ChildItem -Path . -Filter project.json -Recurse |
23+
ForEach-Object {
24+
$content = get-content $_.FullName
25+
$content = $content.Replace("1.0.0-*", "$modifiedVersion")
26+
Set-Content $_.FullName $content -Encoding UTF8
27+
}
28+
}
29+
30+
31+
32+
& dotnet restore --no-cache
33+
34+
foreach ($test in ls test/*.Tests) {
35+
Push-Location $test
36+
37+
echo "build: Testing project in $test"
38+
39+
& dotnet test
40+
if($LASTEXITCODE -ne 0) { Pop-Location; exit 3 }
41+
42+
Pop-Location
43+
}
44+
45+
46+
echo "build: Version $modifiedVersion $suffix"
47+
dotnet build src/**/project.json --version-suffix=$suffix -c Release
48+
if($LASTEXITCODE -ne 0) { exit 1 }
49+
50+
foreach ($src in ls src/*) {
51+
Push-Location $src
52+
53+
echo "build: Packaging project in $src"
54+
55+
& dotnet pack --no-build -c Release -o ..\..\artifacts --version-suffix=$suffix
56+
if($LASTEXITCODE -ne 0) { Pop-Location; exit 1 }
57+
58+
Pop-Location
59+
}
60+
61+
if ($modifyVersion) {
62+
Get-ChildItem -Path . -Filter project.json -Recurse |
63+
ForEach-Object {
64+
$content = get-content $_.FullName
65+
$content = $content.Replace("$modifiedVersion", "1.0.0-*")
66+
Set-Content $_.FullName $content -Encoding UTF8
67+
}
68+
}
69+
70+
Pop-Location
71+

Diff for: global.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"projects": [
3+
"src",
4+
"test"
5+
],
6+
"sdk": {
7+
"version": "1.0.0-preview2-003121"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace SceneSkope.Extensions.Logging.RequestPathFilter.Internal
5+
{
6+
internal class ActionOnDispose : IDisposable
7+
{
8+
private readonly Action _disposer;
9+
public ActionOnDispose(Action disposer)
10+
{
11+
_disposer = disposer;
12+
}
13+
14+
public void Dispose()
15+
{
16+
_disposer();
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Threading;
6+
7+
namespace SceneSkope.Extensions.Logging.RequestPathFilter.Internal
8+
{
9+
internal class RequestPathFilterLogger : ILogger
10+
{
11+
private readonly RequestPathFilterLoggerProvider _provider;
12+
private readonly string _categoryName;
13+
private readonly ILogger _logger;
14+
private readonly string _requestPathToIgnore;
15+
16+
public RequestPathFilterLogger(RequestPathFilterLoggerProvider provider, ILogger logger, string categoryName, string requestPathToIgnore)
17+
{
18+
_provider = provider;
19+
_logger = logger;
20+
_categoryName = categoryName;
21+
_requestPathToIgnore = requestPathToIgnore;
22+
}
23+
24+
public IDisposable BeginScope<TState>(TState state)
25+
{
26+
var wrapped = _logger.BeginScope(state);
27+
return _provider.BeginScope(state, wrapped);
28+
}
29+
30+
public bool IsEnabled(LogLevel logLevel) => _logger.IsEnabled(logLevel);
31+
32+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
33+
{
34+
if (!_provider.RequestPathMatched && !_provider.DoesRequestPathMatch(state))
35+
{
36+
_logger.Log(logLevel, eventId, state, exception, formatter);
37+
}
38+
}
39+
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace SceneSkope.Extensions.Logging.RequestPathFilter.Internal
7+
{
8+
internal class RequestPathFilterLoggerFactory : ILoggerFactory
9+
{
10+
private readonly ILoggerFactory _wrappedFactory;
11+
private readonly string _requestPathToIgnore;
12+
13+
public RequestPathFilterLoggerFactory(ILoggerFactory factory, string requestPathToIgnore)
14+
{
15+
_wrappedFactory = factory;
16+
_requestPathToIgnore = requestPathToIgnore;
17+
18+
}
19+
public void AddProvider(ILoggerProvider provider)
20+
{
21+
var wrappedProvider = new RequestPathFilterLoggerProvider(provider, _requestPathToIgnore);
22+
_wrappedFactory.AddProvider(wrappedProvider);
23+
}
24+
25+
public ILogger CreateLogger(string categoryName) => _wrappedFactory.CreateLogger(categoryName);
26+
27+
public void Dispose()
28+
{
29+
// Nothing to dispose here - dispose should be called on the wrapped factory
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using System.Collections.Generic;
4+
#if ASYNCLOCAL
5+
using System.Threading;
6+
#else
7+
using System.Runtime.Remoting.Messaging;
8+
#endif
9+
10+
namespace SceneSkope.Extensions.Logging.RequestPathFilter.Internal
11+
{
12+
internal class RequestPathFilterLoggerProvider : ILoggerProvider
13+
{
14+
private readonly ILoggerProvider _provider;
15+
private readonly string _requestPathToIgnore;
16+
17+
public RequestPathFilterLoggerProvider(ILoggerProvider provider, string requestPathToIgnore)
18+
{
19+
_provider = provider;
20+
_requestPathToIgnore = requestPathToIgnore;
21+
}
22+
23+
public ILogger CreateLogger(string categoryName)
24+
{
25+
var logger = _provider.CreateLogger(categoryName);
26+
var wrapped = new RequestPathFilterLogger(this, logger, categoryName, _requestPathToIgnore);
27+
return wrapped;
28+
}
29+
30+
#if ASYNCLOCAL
31+
private AsyncLocal<bool> _requestPathMatched = new AsyncLocal<bool>();
32+
public bool RequestPathMatched {
33+
get {
34+
return _requestPathMatched.Value;
35+
}
36+
set {
37+
_requestPathMatched.Value = value;
38+
}
39+
}
40+
#else
41+
private readonly string _scopeKey = nameof(RequestPathFilterLoggerProvider) + "#" + Guid.NewGuid().ToString("N");
42+
public bool RequestPathMatched
43+
{
44+
get
45+
{
46+
var value = CallContext.LogicalGetData(_scopeKey);
47+
return (value != null) && (bool)value;
48+
}
49+
set
50+
{
51+
CallContext.LogicalSetData(_scopeKey, value);
52+
}
53+
}
54+
55+
#endif
56+
57+
58+
public IDisposable BeginScope<TState>(TState state, IDisposable wrapped)
59+
{
60+
if (RequestPathMatched)
61+
{
62+
return wrapped;
63+
}
64+
else
65+
{
66+
var matched = DoesRequestPathMatch(state);
67+
if (!matched)
68+
{
69+
return wrapped;
70+
}
71+
else
72+
{
73+
RequestPathMatched = true;
74+
return new ActionOnDispose(() =>
75+
{
76+
RequestPathMatched = false;
77+
wrapped.Dispose();
78+
});
79+
80+
}
81+
}
82+
}
83+
84+
public bool DoesRequestPathMatch<TState>(TState state)
85+
{
86+
var properties = state as IEnumerable<KeyValuePair<string, object>>;
87+
if (properties != null)
88+
{
89+
foreach (var property in properties)
90+
{
91+
if (property.Key == "RequestPath")
92+
{
93+
if (((string)property.Value).Equals(_requestPathToIgnore, StringComparison.OrdinalIgnoreCase))
94+
{
95+
return true;
96+
}
97+
else
98+
{
99+
return false;
100+
}
101+
}
102+
}
103+
}
104+
return false;
105+
}
106+
107+
public void Dispose() => _provider.Dispose();
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.Extensions.Logging;
5+
using SceneSkope.Extensions.Logging.RequestPathFilter.Internal;
6+
7+
namespace SceneSkope.Extensions.Logging.RequestPathFilter
8+
{
9+
public static class RequestPathFilterLoggerFactoryExtensions
10+
{
11+
public static ILoggerFactory WithRequestPathFilter(this ILoggerFactory factory, string requestPathToIgnore) =>
12+
new RequestPathFilterLoggerFactory(factory, requestPathToIgnore);
13+
}
14+
}

0 commit comments

Comments
 (0)