зеркало из https://github.com/SteeltoeOSS/Logging.git
Initial checkin
This commit is contained in:
Родитель
f9ed3a10fd
Коммит
212d1999e0
|
@ -0,0 +1,50 @@
|
|||
*.doc diff=astextplain
|
||||
*.DOC diff=astextplain
|
||||
*.docx diff=astextplain
|
||||
*.DOCX diff=astextplain
|
||||
*.dot diff=astextplain
|
||||
*.DOT diff=astextplain
|
||||
*.pdf diff=astextplain
|
||||
*.PDF diff=astextplain
|
||||
*.rtf diff=astextplain
|
||||
*.RTF diff=astextplain
|
||||
|
||||
*.jpg binary
|
||||
*.png binary
|
||||
*.gif binary
|
||||
|
||||
*.cs text=auto diff=csharp
|
||||
*.vb text=auto
|
||||
*.resx text=auto
|
||||
*.c text=auto
|
||||
*.cpp text=auto
|
||||
*.cxx text=auto
|
||||
*.h text=auto
|
||||
*.hxx text=auto
|
||||
*.py text=auto
|
||||
*.rb text=auto
|
||||
*.java text=auto
|
||||
*.html text=auto
|
||||
*.htm text=auto
|
||||
*.css text=auto
|
||||
*.scss text=auto
|
||||
*.sass text=auto
|
||||
*.less text=auto
|
||||
*.js text=auto
|
||||
*.lisp text=auto
|
||||
*.clj text=auto
|
||||
*.sql text=auto
|
||||
*.php text=auto
|
||||
*.lua text=auto
|
||||
*.m text=auto
|
||||
*.asm text=auto
|
||||
*.erl text=auto
|
||||
*.fs text=auto
|
||||
*.fsx text=auto
|
||||
*.hs text=auto
|
||||
|
||||
*.csproj text=auto
|
||||
*.vbproj text=auto
|
||||
*.fsproj text=auto
|
||||
*.dbproj text=auto
|
||||
*.sln text=auto eol=crlf
|
|
@ -286,3 +286,5 @@ __pycache__/
|
|||
*.btm.cs
|
||||
*.odx.cs
|
||||
*.xsd.cs
|
||||
|
||||
*.vscode
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
language: csharp
|
||||
dist: trusty
|
||||
env:
|
||||
global:
|
||||
- DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
|
||||
- DOTNET_CLI_TELEMETRY_OPTOUT: 1
|
||||
mono: none
|
||||
dotnet: 1.0.4
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
osx_image: xcode8.2
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- dev
|
||||
script:
|
||||
- ./scripts/travis_install.sh
|
||||
- ./scripts/travis_build.sh
|
||||
- ./scripts/travis_test.sh
|
||||
before_install:
|
||||
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then ulimit -n 1024; fi
|
|
@ -0,0 +1,45 @@
|
|||
version: '1.1.0-{branch}-{build}'
|
||||
init:
|
||||
- git config --global core.autocrlf true
|
||||
image: Visual Studio 2017
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- dev
|
||||
install:
|
||||
- scripts\appveyor_install.cmd
|
||||
test_script:
|
||||
- scripts\appveyor_tests.cmd
|
||||
build_script:
|
||||
- scripts\appveyor_build.cmd
|
||||
artifacts:
|
||||
- path: 'src\Steeltoe.Extensions.Logging.CloudFoundry\bin\Release\*.nupkg'
|
||||
deploy:
|
||||
- provider: NuGet
|
||||
skip_symbols: false
|
||||
artifact: /.*\.nupkg/
|
||||
server: https://www.myget.org/F/steeltoemaster/api/v2/package
|
||||
symbol_server: https://www.myget.org/F/steeltoemaster/symbols/api/v2/package
|
||||
api_key:
|
||||
secure: 246ApObnMYYvzDo9Ki72CxxuKfuoqYbrbUK2HLevGIGHXYY6f0OkAdE3EWCwNJ7g
|
||||
on:
|
||||
branch: master
|
||||
appveyor_repo_tag: false
|
||||
- provider: NuGet
|
||||
skip_symbols: false
|
||||
artifact: /.*\.nupkg/
|
||||
server: https://www.myget.org/F/steeltoedev/api/v2/package
|
||||
symbol_server: https://www.myget.org/F/steeltoedev/symbols/api/v2/package
|
||||
api_key:
|
||||
secure: 246ApObnMYYvzDo9Ki72CxxuKfuoqYbrbUK2HLevGIGHXYY6f0OkAdE3EWCwNJ7g
|
||||
on:
|
||||
branch: dev
|
||||
appveyor_repo_tag: false
|
||||
- provider: NuGet
|
||||
skip_symbols: true
|
||||
artifact: /.*\.nupkg/
|
||||
api_key:
|
||||
secure: RGkpu8xIMDC75IAd1qPRdueeTaYj+ddW2bNEw8S7e81940ELu+WV04uWAzqV34/C
|
||||
on:
|
||||
branch: master
|
||||
appveyor_repo_tag: true
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="SteeltoeDev" value="https://www.myget.org/F/steeltoedev/api/v3/index.json" />
|
||||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
|
||||
</packageSources>
|
||||
</configuration>
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="SteeltoeMaster" value="https://www.myget.org/F/steeltoemaster/api/v3/index.json" />
|
||||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
|
||||
</packageSources>
|
||||
</configuration>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
|
||||
</packageSources>
|
||||
</configuration>
|
|
@ -0,0 +1,12 @@
|
|||
<Project>
|
||||
<PropertyGroup>
|
||||
<SteeltoeVersion>$(STEELTOE_VERSION)</SteeltoeVersion>
|
||||
<SteeltoeVersionSuffix>$(STEELTOE_DASH_VERSION_SUFFIX)</SteeltoeVersionSuffix>
|
||||
<AspNetCoreVersion>1.1.0</AspNetCoreVersion>
|
||||
<CoreFxVersion>4.3.0</CoreFxVersion>
|
||||
<JsonNetVersion>9.0.1</JsonNetVersion>
|
||||
<TestSdkVersion>15.0.0</TestSdkVersion>
|
||||
<XunitVersion>2.3.0-beta2-build3683</XunitVersion>
|
||||
<XunitStudioVersion>2.3.0-beta2-build1317</XunitStudioVersion>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,12 @@
|
|||
<Project>
|
||||
<PropertyGroup>
|
||||
<SteeltoeVersion>$(STEELTOE_VERSION)</SteeltoeVersion>
|
||||
<SteeltoeVersionSuffix>$(STEELTOE_DASH_VERSION_SUFFIX)</SteeltoeVersionSuffix>
|
||||
<AspNetCoreVersion>1.1.0</AspNetCoreVersion>
|
||||
<CoreFxVersion>4.3.0</CoreFxVersion>
|
||||
<JsonNetVersion>9.0.1</JsonNetVersion>
|
||||
<TestSdkVersion>15.0.0</TestSdkVersion>
|
||||
<XunitVersion>2.3.0-beta2-build3683</XunitVersion>
|
||||
<XunitStudioVersion>2.3.0-beta2-build1317</XunitStudioVersion>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,12 @@
|
|||
<Project>
|
||||
<PropertyGroup>
|
||||
<SteeltoeVersion>$(STEELTOE_VERSION)</SteeltoeVersion>
|
||||
<SteeltoeVersionSuffix>$(STEELTOE_DASH_VERSION_SUFFIX)</SteeltoeVersionSuffix>
|
||||
<AspNetCoreVersion>1.1.0</AspNetCoreVersion>
|
||||
<CoreFxVersion>4.3.0</CoreFxVersion>
|
||||
<JsonNetVersion>9.0.1</JsonNetVersion>
|
||||
<TestSdkVersion>15.0.0</TestSdkVersion>
|
||||
<XunitVersion>2.3.0-beta2-build3683</XunitVersion>
|
||||
<XunitStudioVersion>2.3.0-beta2-build1317</XunitStudioVersion>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="SteeltoeDev" value="https://www.myget.org/F/steeltoedev/api/v3/index.json" />
|
||||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
|
||||
</packageSources>
|
||||
</configuration>
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,15 @@
|
|||
:: @ECHO OFF
|
||||
|
||||
:: Build packages
|
||||
cd src\Steeltoe.Extensions.Logging.CloudFoundry
|
||||
dotnet restore --configfile ..\..\nuget.config
|
||||
IF NOT "%APPVEYOR_REPO_TAG_NAME%"=="" (
|
||||
IF NOT "%STEELTOE_VERSION_SUFFIX%"=="" (
|
||||
dotnet pack --configuration %BUILD_TYPE% --version-suffix %STEELTOE_VERSION_SUFFIX%
|
||||
) ELSE (
|
||||
dotnet pack --configuration %BUILD_TYPE%
|
||||
)
|
||||
)
|
||||
|
||||
IF "%APPVEYOR_REPO_TAG_NAME%"=="" (dotnet pack --configuration %BUILD_TYPE% --version-suffix %STEELTOE_VERSION_SUFFIX% --include-symbols --include-source)
|
||||
cd ..\..
|
|
@ -0,0 +1,24 @@
|
|||
@ECHO OFF
|
||||
:: Output dotnet info
|
||||
dotnet --info
|
||||
|
||||
SET CI_BUILD=%APPVEYOR%
|
||||
SET number=00000%APPVEYOR_BUILD_NUMBER%
|
||||
SET STEELTOE_VERSION=1.1.0
|
||||
SET STEELTOE_VERSION_SUFFIX=%APPVEYOR_REPO_BRANCH%-%number:~-5%
|
||||
IF NOT "%APPVEYOR_REPO_TAG_NAME%"=="" SET STEELTOE_VERSION_SUFFIX=%APPVEYOR_REPO_TAG_NAME:~6,5%
|
||||
IF NOT "%STEELTOE_VERSION_SUFFIX%"=="" (
|
||||
SET STEELTOE_VERSION_SUFFIX=%STEELTOE_VERSION_SUFFIX: =%
|
||||
SET STEELTOE_DASH_VERSION_SUFFIX=-%STEELTOE_VERSION_SUFFIX%
|
||||
) ELSE (
|
||||
SET STEELTOE_DASH_VERSION_SUFFIX=
|
||||
)
|
||||
echo "Version Suffix:" %STEELTOE_VERSION_SUFFIX%
|
||||
SET BUILD_TYPE=Release
|
||||
IF "%APPVEYOR_REPO_BRANCH%"=="master" COPY config\nuget-master.config .\nuget.config
|
||||
IF "%APPVEYOR_REPO_BRANCH%"=="dev" COPY config\nuget-dev.config .\nuget.config
|
||||
IF NOT "%APPVEYOR_REPO_TAG_NAME%"=="" COPY config\nuget.config .\nuget.config
|
||||
IF "%APPVEYOR_REPO_BRANCH%"=="master" COPY config\versions-master.props .\versions.props
|
||||
IF "%APPVEYOR_REPO_BRANCH%"=="dev" COPY config\versions-dev.props .\versions.props
|
||||
IF NOT "%APPVEYOR_REPO_TAG_NAME%"=="" COPY config\versions.props .\versions.props
|
||||
IF "%APPVEYOR_REPO_BRANCH%"=="dev" SET BUILD_TYPE=Debug
|
|
@ -0,0 +1,14 @@
|
|||
@ECHO OFF
|
||||
|
||||
:: Run unit tests
|
||||
cd test\Steeltoe.Extensions.Logging.CloudFoundry.Test
|
||||
dotnet restore --configfile ..\..\nuget.config
|
||||
dotnet xunit -verbose
|
||||
if not "%errorlevel%"=="0" goto failure
|
||||
cd ..\..
|
||||
echo Unit Tests Pass
|
||||
goto success
|
||||
:failure
|
||||
echo Unit Tests Failure
|
||||
exit -1
|
||||
:success
|
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo Code is built in Unit Tests
|
||||
|
||||
cd src/Steeltoe.Extensions.Logging.CloudFoundry
|
||||
dotnet restore --configfile ../../nuget.config
|
||||
cd ../..
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#!/bin/bash
|
||||
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi
|
||||
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install openssl ; fi
|
||||
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/ ; fi
|
||||
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/ ; fi
|
||||
|
||||
|
||||
dotnet --info
|
||||
|
||||
export CI_BUILD=True
|
||||
export STEELTOE_VERSION=1.1.0
|
||||
if [[ "$TRAVIS_BRANCH" == "master" ]]; then cp config/nuget-master.config ./nuget.config ; fi
|
||||
if [[ "$TRAVIS_BRANCH" == "dev" ]]; then cp config/nuget-dev.config ./nuget.config ; fi
|
||||
if [[ "$TRAVIS_TAG" != "" ]]; then cp config/nuget.config ./nuget.config ; fi
|
||||
if [[ "$TRAVIS_BRANCH" == "master" ]]; then cp config/versions-master.props ./versions.props ; fi
|
||||
if [[ "$TRAVIS_BRANCH" == "dev" ]]; then cp config/versions-dev.props ./versions.props ; fi
|
||||
if [[ "$TRAVIS_TAG" != "" ]]; then cp config/versions.props ./versions.props ; fi
|
|
@ -0,0 +1,8 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Run unit tests
|
||||
cd test/Steeltoe.Extensions.Logging.CloudFoundry.Test
|
||||
dotnet restore --configfile ../../nuget.config
|
||||
dotnet xunit -verbose -framework netcoreapp1.1
|
||||
if [[ $? != 0 ]]; then exit 1 ; fi
|
||||
cd ../..
|
|
@ -0,0 +1,68 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry
|
||||
{
|
||||
public static class CloudFoundryLoggerFactoryExtensions
|
||||
{
|
||||
public static ILoggerFactory AddCloudFoundry(this ILoggerFactory factory)
|
||||
{
|
||||
return factory.AddCloudFoundry(includeScopes: false);
|
||||
}
|
||||
|
||||
public static ILoggerFactory AddCloudFoundry(this ILoggerFactory factory, bool includeScopes)
|
||||
{
|
||||
factory.AddCloudFoundry((n, l) => l >= LogLevel.Information, includeScopes);
|
||||
return factory;
|
||||
}
|
||||
|
||||
public static ILoggerFactory AddCloudFoundry(this ILoggerFactory factory, LogLevel minLevel)
|
||||
{
|
||||
factory.AddCloudFoundry(minLevel, includeScopes: false);
|
||||
return factory;
|
||||
}
|
||||
|
||||
public static ILoggerFactory AddCloudFoundry(
|
||||
this ILoggerFactory factory,
|
||||
LogLevel minLevel,
|
||||
bool includeScopes)
|
||||
{
|
||||
factory.AddCloudFoundry((category, logLevel) => logLevel >= minLevel, includeScopes);
|
||||
return factory;
|
||||
}
|
||||
|
||||
public static ILoggerFactory AddCloudFoundry(
|
||||
this ILoggerFactory factory,
|
||||
Func<string, LogLevel, bool> filter)
|
||||
{
|
||||
factory.AddCloudFoundry(filter, includeScopes: false);
|
||||
return factory;
|
||||
}
|
||||
|
||||
public static ILoggerFactory AddCloudFoundry(
|
||||
this ILoggerFactory factory,
|
||||
Func<string, LogLevel, bool> filter,
|
||||
bool includeScopes)
|
||||
{
|
||||
// factory.AddProvider(new CloudFoundryLoggerProvider(filter, includeScopes));
|
||||
factory.AddProvider(CloudFoundryLoggerProvider.CreateSingleton(filter, includeScopes));
|
||||
return factory;
|
||||
}
|
||||
|
||||
public static ILoggerFactory AddCloudFoundry(
|
||||
this ILoggerFactory factory,
|
||||
ICloudFoundryLoggerSettings settings)
|
||||
{
|
||||
//factory.AddProvider(new CloudFoundryLoggerProvider(settings));
|
||||
factory.AddProvider(CloudFoundryLoggerProvider.CreateSingleton(settings));
|
||||
return factory;
|
||||
}
|
||||
|
||||
public static ILoggerFactory AddCloudFoundry(this ILoggerFactory factory, IConfiguration configuration)
|
||||
{
|
||||
var settings = new CloudFoundryLoggerSettings(configuration);
|
||||
return factory.AddCloudFoundry(settings);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,173 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry
|
||||
{
|
||||
|
||||
public class CloudFoundryLoggerProvider : ICloudFoundryLoggerProvider
|
||||
{
|
||||
|
||||
private ConcurrentDictionary<string, ConsoleLogger> _loggers = new ConcurrentDictionary<string, ConsoleLogger>();
|
||||
|
||||
private ConsoleLoggerProvider _delegate;
|
||||
|
||||
private bool _filter = false;
|
||||
private ICloudFoundryLoggerSettings _settings;
|
||||
|
||||
internal static CloudFoundryLoggerProvider _self;
|
||||
|
||||
public static ICloudFoundryLoggerProvider Instance { get { return _self; } }
|
||||
|
||||
internal static ILoggerProvider CreateSingleton(Func<string, LogLevel, bool> filter, bool includeScopes)
|
||||
{
|
||||
if (_self != null)
|
||||
{
|
||||
throw new InvalidOperationException("CloudFoundryLoggerProvider already created");
|
||||
}
|
||||
|
||||
return _self = new CloudFoundryLoggerProvider(filter, includeScopes);
|
||||
}
|
||||
|
||||
internal static ILoggerProvider CreateSingleton(ICloudFoundryLoggerSettings settings)
|
||||
{
|
||||
if (_self != null)
|
||||
{
|
||||
throw new InvalidOperationException("CloudFoundryLoggerProvider already created");
|
||||
}
|
||||
|
||||
return _self = new CloudFoundryLoggerProvider(settings);
|
||||
}
|
||||
|
||||
public CloudFoundryLoggerProvider(Func<string, LogLevel, bool> filter, bool includeScopes)
|
||||
{
|
||||
_delegate = new ConsoleLoggerProvider(filter, includeScopes);
|
||||
_filter = true;
|
||||
_settings = null;
|
||||
}
|
||||
|
||||
public CloudFoundryLoggerProvider(ICloudFoundryLoggerSettings settings)
|
||||
{
|
||||
_delegate = new ConsoleLoggerProvider(settings);
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string name)
|
||||
{
|
||||
ConsoleLogger created = _delegate.CreateLogger(name) as ConsoleLogger;
|
||||
return _loggers.GetOrAdd(name, created);
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetKeyPrefixes(string name)
|
||||
{
|
||||
while (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
yield return name;
|
||||
var lastIndexOfDot = name.LastIndexOf('.');
|
||||
if (lastIndexOfDot == -1)
|
||||
{
|
||||
yield return "Default";
|
||||
break;
|
||||
}
|
||||
name = name.Substring(0, lastIndexOfDot);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_delegate.Dispose();
|
||||
_delegate = null;
|
||||
_settings = null;
|
||||
_loggers = null;
|
||||
_self = null;
|
||||
}
|
||||
|
||||
public ICollection<ILoggerConfiguration> GetLoggerConfigurations()
|
||||
{
|
||||
Dictionary<string, ILoggerConfiguration> results = new Dictionary<string, ILoggerConfiguration>();
|
||||
|
||||
LogLevel configuredDefault = GetConfiguredLevel("Default") ?? LogLevel.None;
|
||||
LogLevel effictiveDefault = configuredDefault;
|
||||
|
||||
results.Add("Default", new LoggerConfiguration("Default", configuredDefault, effictiveDefault));
|
||||
foreach (var logger in _loggers)
|
||||
{
|
||||
|
||||
foreach (var prefix in GetKeyPrefixes(logger.Value.Name))
|
||||
{
|
||||
if (prefix != "Default")
|
||||
{
|
||||
var name = prefix;
|
||||
LogLevel? configured = GetConfiguredLevel(name);
|
||||
LogLevel effective = GetEffectiveLevel(name);
|
||||
var config = new LoggerConfiguration(name, configured, effective);
|
||||
if (results.ContainsKey(name))
|
||||
{
|
||||
if (!results[name].Equals(config))
|
||||
{
|
||||
throw new InvalidProgramException("Shouldn't happen");
|
||||
}
|
||||
}
|
||||
results[name] = config;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return results.Values;
|
||||
}
|
||||
|
||||
|
||||
public void SetLogLevel(string category, LogLevel level)
|
||||
{
|
||||
if (!_filter)
|
||||
{
|
||||
_settings.SetLogLevel(category, level);
|
||||
}
|
||||
|
||||
}
|
||||
private LogLevel GetEffectiveLevel(string name)
|
||||
{
|
||||
if (_filter)
|
||||
{
|
||||
return LogLevel.None;
|
||||
}
|
||||
|
||||
if (_settings != null)
|
||||
{
|
||||
foreach (var prefix in GetKeyPrefixes(name))
|
||||
{
|
||||
LogLevel level;
|
||||
if (_settings.TryGetSwitch(prefix, out level))
|
||||
{
|
||||
return level;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return LogLevel.None;
|
||||
}
|
||||
private LogLevel? GetConfiguredLevel(string name)
|
||||
{
|
||||
if (_filter)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_settings != null)
|
||||
{
|
||||
LogLevel level;
|
||||
if (_settings.TryGetSwitch(name, out level))
|
||||
{
|
||||
return level;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry
|
||||
|
||||
{
|
||||
public class CloudFoundryLoggerSettings : ICloudFoundryLoggerSettings
|
||||
{
|
||||
private IConsoleLoggerSettings _settings;
|
||||
private ConfigurationReloadToken _changeToken;
|
||||
|
||||
private IDictionary<string, LogLevel> DynamicSwitches { get; set; }
|
||||
|
||||
public CloudFoundryLoggerSettings()
|
||||
{
|
||||
DynamicSwitches = new Dictionary<string, LogLevel>();
|
||||
}
|
||||
|
||||
public CloudFoundryLoggerSettings(IConfiguration configuration)
|
||||
{
|
||||
_changeToken = new ConfigurationReloadToken();
|
||||
_settings = new ConfigurationConsoleLoggerSettings(configuration);
|
||||
_settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
|
||||
DynamicSwitches = new Dictionary<string, LogLevel>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void OnConfigurationReload(object obj)
|
||||
{
|
||||
// Configuration changed;
|
||||
_changeToken.OnReload();
|
||||
}
|
||||
|
||||
|
||||
public IChangeToken ChangeToken {
|
||||
get { return _changeToken; }
|
||||
|
||||
}
|
||||
|
||||
public bool IncludeScopes
|
||||
{
|
||||
get
|
||||
{
|
||||
return _settings.IncludeScopes;
|
||||
}
|
||||
}
|
||||
|
||||
public IConsoleLoggerSettings Reload()
|
||||
{
|
||||
_settings = _settings.Reload();
|
||||
_changeToken = new ConfigurationReloadToken();
|
||||
_settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool TryGetSwitch(string name, out LogLevel level)
|
||||
{
|
||||
if (DynamicSwitches.TryGetValue(name, out level))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return _settings.TryGetSwitch(name, out level);
|
||||
|
||||
}
|
||||
|
||||
public void SetLogLevel(string category, LogLevel level)
|
||||
{
|
||||
DynamicSwitches[category] = level;
|
||||
_changeToken.OnReload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry
|
||||
{
|
||||
public interface ICloudFoundryLoggerProvider : ILoggerProvider
|
||||
{
|
||||
ICollection<ILoggerConfiguration> GetLoggerConfigurations();
|
||||
void SetLogLevel(string category, LogLevel level);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry
|
||||
{
|
||||
public interface ICloudFoundryLoggerSettings : IConsoleLoggerSettings
|
||||
{
|
||||
void SetLogLevel(string category, LogLevel level);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry
|
||||
{
|
||||
public interface ILoggerConfiguration
|
||||
{
|
||||
string Name { get; }
|
||||
LogLevel? ConfiguredLevel { get; }
|
||||
LogLevel EffectiveLevel { get; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry
|
||||
{
|
||||
public class LoggerConfiguration : ILoggerConfiguration
|
||||
{
|
||||
public LoggerConfiguration(string name, LogLevel? configured, LogLevel effective)
|
||||
{
|
||||
Name = name;
|
||||
ConfiguredLevel = configured;
|
||||
EffectiveLevel = effective;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public LogLevel? ConfiguredLevel { get; }
|
||||
|
||||
public LogLevel EffectiveLevel { get; }
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
LoggerConfiguration lc = obj as LoggerConfiguration;
|
||||
if (lc == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return this.Name == lc.Name &&
|
||||
this.ConfiguredLevel == lc.ConfiguredLevel &&
|
||||
this.EffectiveLevel == lc.EffectiveLevel;
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return "[" + Name + "," + ConfiguredLevel + "," + EffectiveLevel + "]";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Import Project="..\..\versions.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<Description>Steeltoe Console Logger</Description>
|
||||
<Authors>Pivotal;dtillman</Authors>
|
||||
<VersionPrefix>$(SteeltoeVersion)</VersionPrefix>
|
||||
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
|
||||
<TargetFrameworks>net46;netstandard1.6</TargetFrameworks>
|
||||
<AssemblyName>Steeltoe.Extensions.Logging.CloudFoundry</AssemblyName>
|
||||
<PackageId>Steeltoe.Extensions.Logging.CloudFoundry</PackageId>
|
||||
<PackageTags>Spring Cloud;Logging;Management</PackageTags>
|
||||
<PackageIconUrl>http://steeltoe.io/images/transparent.png</PackageIconUrl>
|
||||
<PackageProjectUrl>http://steeltoe.io</PackageProjectUrl>
|
||||
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(CI_BUILD)' == ''">
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(CI_BUILD)' == 'True'">
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,136 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.IO;
|
||||
using Xunit;
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry.Test
|
||||
{
|
||||
public class CloudFoundryLoggerProviderTest
|
||||
{
|
||||
[Fact]
|
||||
public void Create_CreatesCorrectLogger()
|
||||
{
|
||||
var appsettings = @"
|
||||
{
|
||||
'Logging': {
|
||||
'IncludeScopes': false,
|
||||
'LogLevel': {
|
||||
'Default': 'Information',
|
||||
'System': 'Information',
|
||||
'Microsoft': 'Information',
|
||||
'A': 'Information'
|
||||
}
|
||||
}
|
||||
}";
|
||||
var config = GetConfig(appsettings);
|
||||
LoggerFactory fac = new LoggerFactory();
|
||||
var loggingSection = config.GetSection("Logging");
|
||||
|
||||
var settings = new CloudFoundryLoggerSettings(loggingSection);
|
||||
var provider = new CloudFoundryLoggerProvider(settings);
|
||||
fac.AddProvider(provider);
|
||||
|
||||
ILogger logger = fac.CreateLogger(typeof(A.B.C.D.TestClass));
|
||||
Assert.NotNull(logger);
|
||||
Assert.True(logger.IsEnabled(LogLevel.Information));
|
||||
Assert.False(logger.IsEnabled(LogLevel.Debug));
|
||||
|
||||
provider.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetLogLevel_UpdatesLogger()
|
||||
{
|
||||
var appsettings = @"
|
||||
{
|
||||
'Logging': {
|
||||
'IncludeScopes': false,
|
||||
'LogLevel': {
|
||||
'Default': 'Information',
|
||||
'System': 'Information',
|
||||
'Microsoft': 'Information',
|
||||
'A': 'Information'
|
||||
}
|
||||
}
|
||||
}";
|
||||
var config = GetConfig(appsettings);
|
||||
LoggerFactory fac = new LoggerFactory();
|
||||
var loggingSection = config.GetSection("Logging");
|
||||
|
||||
var settings = new CloudFoundryLoggerSettings(loggingSection);
|
||||
var provider = new CloudFoundryLoggerProvider(settings);
|
||||
fac.AddProvider(provider);
|
||||
|
||||
ILogger logger = fac.CreateLogger(typeof(A.B.C.D.TestClass));
|
||||
Assert.NotNull(logger);
|
||||
Assert.True(logger.IsEnabled(LogLevel.Information));
|
||||
Assert.False(logger.IsEnabled(LogLevel.Debug));
|
||||
|
||||
provider.SetLogLevel("A", LogLevel.Debug);
|
||||
Assert.True(logger.IsEnabled(LogLevel.Information));
|
||||
Assert.True(logger.IsEnabled(LogLevel.Debug));
|
||||
|
||||
|
||||
provider.SetLogLevel("A", LogLevel.Information);
|
||||
Assert.True(logger.IsEnabled(LogLevel.Information));
|
||||
Assert.False(logger.IsEnabled(LogLevel.Debug));
|
||||
|
||||
provider.Dispose();
|
||||
}
|
||||
[Fact]
|
||||
public void GetLoggerConfigurations_ReturnsExpected()
|
||||
{
|
||||
var appsettings = @"
|
||||
{
|
||||
'Logging': {
|
||||
'IncludeScopes': false,
|
||||
'LogLevel': {
|
||||
'Default': 'Information',
|
||||
'System': 'Information',
|
||||
'Microsoft': 'Information',
|
||||
'A': 'Information'
|
||||
}
|
||||
}
|
||||
}";
|
||||
var config = GetConfig(appsettings);
|
||||
LoggerFactory fac = new LoggerFactory();
|
||||
var loggingSection = config.GetSection("Logging");
|
||||
|
||||
var settings = new CloudFoundryLoggerSettings(loggingSection);
|
||||
var provider = new CloudFoundryLoggerProvider(settings);
|
||||
fac.AddProvider(provider);
|
||||
|
||||
ILogger logger = fac.CreateLogger(typeof(A.B.C.D.TestClass));
|
||||
|
||||
var logConfig = provider.GetLoggerConfigurations();
|
||||
Assert.Equal(6, logConfig.Count);
|
||||
Assert.Contains(new LoggerConfiguration("Default", LogLevel.Information, LogLevel.Information), logConfig);
|
||||
Assert.Contains(new LoggerConfiguration("A.B.C.D.TestClass", null, LogLevel.Information), logConfig);
|
||||
Assert.Contains(new LoggerConfiguration("A.B.C.D", null, LogLevel.Information), logConfig);
|
||||
Assert.Contains(new LoggerConfiguration("A.B.C", null, LogLevel.Information), logConfig);
|
||||
Assert.Contains(new LoggerConfiguration("A.B", null, LogLevel.Information), logConfig);
|
||||
Assert.Contains(new LoggerConfiguration("A", LogLevel.Information, LogLevel.Information), logConfig);
|
||||
|
||||
provider.Dispose();
|
||||
|
||||
}
|
||||
|
||||
private IConfiguration GetConfig(string json)
|
||||
{
|
||||
var path = TestHelpers.CreateTempFile(json);
|
||||
string directory = Path.GetDirectoryName(path);
|
||||
string fileName = Path.GetFileName(path);
|
||||
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
|
||||
configurationBuilder.SetBasePath(directory);
|
||||
|
||||
configurationBuilder.AddJsonFile(fileName);
|
||||
var config = configurationBuilder.Build();
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace A.B.C.D
|
||||
{
|
||||
class TestClass { }
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Import Project="..\..\versions.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<Description>Unit test project for Steeltoe.Extensions.Logging.CloudFoundry</Description>
|
||||
<TargetFrameworks>net46;netcoreapp1.1</TargetFrameworks>
|
||||
<AssemblyName>Steeltoe.Extensions.Logging.CloudFoundry.Test</AssemblyName>
|
||||
<PackageId>Steeltoe.Extensions.Logging.CloudFoundry.Test</PackageId>
|
||||
<PackageTags>Spring Cloud;Logging;Management;Monitoring</PackageTags>
|
||||
<PackageProjectUrl>http://steeltoe.io</PackageProjectUrl>
|
||||
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="xunit.runner.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Steeltoe.Extensions.Logging.CloudFoundry\Steeltoe.Extensions.Logging.CloudFoundry.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(AspNetCoreVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(AspNetCoreVersion)" />
|
||||
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
|
||||
<PackageReference Include="xunit" Version="$(XunitVersion)" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitStudioVersion)" />
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="$(XunitVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,34 @@
|
|||
using System.IO;
|
||||
|
||||
|
||||
namespace Steeltoe.Extensions.Logging.CloudFoundry.Test
|
||||
{
|
||||
public static class TestHelpers
|
||||
{
|
||||
public static string CreateTempFile(string contents)
|
||||
{
|
||||
var tempFile = Path.GetTempFileName();
|
||||
File.WriteAllText(tempFile, contents);
|
||||
return tempFile;
|
||||
|
||||
}
|
||||
public static Stream StringToStream(string str)
|
||||
{
|
||||
var memStream = new MemoryStream();
|
||||
var textWriter = new StreamWriter(memStream);
|
||||
textWriter.Write(str);
|
||||
textWriter.Flush();
|
||||
memStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return memStream;
|
||||
}
|
||||
|
||||
public static string StreamToString(Stream stream)
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
var reader = new StreamReader(stream);
|
||||
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
<Project>
|
||||
<Import Project="config\versions-dev.props" />
|
||||
</Project>
|
Загрузка…
Ссылка в новой задаче