├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── nuget-push.sh └── src ├── Tests ├── slf4net.Moqs │ ├── Factories │ │ ├── TestConfigurableLoggerFactory.cs │ │ ├── TestLoggerFactory.cs │ │ └── TestNamedLoggerFactory.cs │ ├── MoqFactory.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── slf4net.Moqs.csproj │ └── slf4net.snk ├── slf4net.NLog.Tests │ ├── NLogLoggerAdapterTest.cs │ ├── NLogLoggerFactoryTest.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── slf4net.NLog.Tests.csproj │ └── slf4net.snk ├── slf4net.Tests │ ├── App.config │ ├── BasicSlf4netServiceProviderTest.cs │ ├── Configuration │ │ ├── ConfigurationTests.config │ │ └── SlfConfigurationSectionTest.cs │ ├── EventLogHelperTest.cs │ ├── ExploratoryTester.cs │ ├── Factories │ │ ├── Internal │ │ │ └── SubstituteLoggerFactoryTest.cs │ │ ├── NOPLoggerFactoryTest.cs │ │ └── NamedLoggerFactoryBaseTest.cs │ ├── GDCTest.cs │ ├── Internal │ │ ├── NOPMdcAdapterTest.cs │ │ ├── NOPSlf4netServiceProviderTest.cs │ │ └── SubstituteServiceProviderTest.cs │ ├── LoggerFactoryTest.cs │ ├── MDCTest.cs │ ├── MDLCTest.cs │ ├── NOPLoggerTest.cs │ ├── NamedLoggerBaseTest.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Resolvers │ │ ├── AppConfigFactoryResolverTest.cs │ │ ├── LegacyServiceProviderResolverTest.cs │ │ └── NOPLoggerFactoryResolverTest.cs │ ├── slf4net.Tests.csproj │ └── slf4net.snk └── slf4net.log4net.Tests │ ├── Log4netLoggerAdapterTest.cs │ ├── Log4netLoggerFactoryTest.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── log4net.config │ ├── log4net.extra.config │ ├── slf4net.log4net.Tests.csproj │ └── slf4net.snk ├── slf4net.NLog ├── NLogLoggerAdapter.cs ├── NLogLoggerFactory.cs ├── NLogMdcAdapter.cs ├── app.config.transform ├── slf4net.NLog.csproj ├── slf4net.snk └── web.config.transform ├── slf4net.log4net ├── Internal │ ├── IXmlConfigurator.cs │ └── XmlConfiguratorWrapper.cs ├── Log4netLoggerAdapter.cs ├── Log4netLoggerFactory.cs ├── Log4netMdcAdapter.cs ├── XmlConfiguratorHelper.cs ├── app.config.transform ├── slf4net.log4net.csproj ├── slf4net.snk └── web.config.transform ├── slf4net.sln ├── slf4net.targets └── slf4net ├── ActivatorUtils.cs ├── Configuration ├── FactoryConfigurationElement.cs └── SlfConfigurationSection.cs ├── Ensure.cs ├── Factories ├── Internal │ └── SubstituteLoggerFactory.cs ├── NOPLoggerFactory.cs └── NamedLoggerFactoryBase.cs ├── GDC.cs ├── GetPublicKey.bat ├── IConfigurableLoggerFactory.cs ├── IFactoryResolver.cs ├── ILogger.cs ├── ILoggerFactory.cs ├── IMdcAdapter.cs ├── ISlf4netServiceProvider.cs ├── ISlf4netServiceProviderResolver.cs ├── Internal ├── BasicSlf4netServiceProvider.cs ├── ConsoleHelper.cs ├── NOPMdcAdapter.cs ├── NOPSlf4netServiceProvider.cs └── SubstituteServiceProvider.cs ├── LoggerFactory.cs ├── MDC.cs ├── MDLC.cs ├── NOPLogger.cs ├── NamedLoggerBase.cs ├── Resolvers ├── AppConfigFactoryResolver.cs ├── LegacyServiceProviderResolver.cs └── NOPLoggerFactoryResolver.cs ├── slf4net.PublicKey ├── slf4net.csproj └── slf4net.snk /.gitignore: -------------------------------------------------------------------------------- 1 | # git ignore file 2 | Thumbs.db 3 | *.obj 4 | *.exe 5 | *.pdb 6 | *.vspscc 7 | *.suo 8 | *.bak 9 | *.log 10 | [Bb]in 11 | [Dd]ebug*/ 12 | obj/ 13 | [Rr]elease*/ 14 | _ReSharper*/ 15 | [Tt]est[Rr]esults* 16 | *.vssscc 17 | *.tmp 18 | *.userprefs 19 | 20 | # NuGet files to exclude 21 | nuget/ 22 | packages/ 23 | *.nupkg 24 | !NuGet.exe 25 | 26 | # NCrunch files to exclude 27 | _NCrunch_*/ 28 | *.ncrunchsolution 29 | *.ncrunchproject 30 | *.cache 31 | *.user 32 | 33 | # NUnit files to exclude 34 | TestResult.xml 35 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: csharp 2 | solution: src/slf4net.sln 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2020 EF Learning Labs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the “Software”), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple Logging Facade for .Net (slf4net) 2 | ======== 3 | 4 | [![NuGet](https://img.shields.io/nuget/v/slf4net.svg)](https://www.nuget.org/packages/slf4net) 5 | [![NuGet](https://img.shields.io/nuget/dt/slf4net.svg)](https://www.nuget.org/packages/slf4net) 6 | [![Build Status](https://travis-ci.org/ef-labs/slf4net.svg?branch=develop)](https://travis-ci.org/ef-labs/slf4net) 7 | [![License: MIT](https://img.shields.io/badge/license-MIT-yellow.svg)](https://ef-labs.mit-license.org) 8 | 9 | 10 | The slf4net project serves as a light weight abstraction layer for various logging frameworks such as log4net and NLog. This allows the end user to plug in the desired logging framework at deployment time. 11 | 12 | 13 | This project was inspired by the [Java slf4j project](http://www.slf4j.org/) (which it tries to follow as closely as possible) and an earlier [.Net slf project](http://slf.codeplex.com/). 14 | 15 | 16 | Installing slf4net 17 | ------------------- 18 | Find slf4net on nuget.org: http://nuget.org/packages?q=slf4net 19 | 20 | When deploying your application, choose either the slf4net.log4net or slf4net.NLog package, or create your own wrapper for your favourite logging framework. 21 | 22 | 23 | Configuration 24 | ------------- 25 | See the [configuration wiki page](https://github.com/ef-labs/slf4net/wiki/Configuration). 26 | 27 | 28 | Getting started 29 | ---------------- 30 | Intall the slf4net nuget package to your project. If a logger factory is not configured, a NOPLogger (no operation logger) will be used. Start logging. 31 | 32 | ```c# 33 | using System; 34 | 35 | namespace slf4net.Samples 36 | { 37 | public class MyClass 38 | { 39 | 40 | private static readonly slf4net.ILogger _logger = slf4net.LoggerFactory.GetLogger(typeof(MyClass)); 41 | 42 | public void Foo() 43 | { 44 | _logger.Trace("Foo started at {0}.", DateTime.Now); 45 | 46 | // Do some work 47 | 48 | _logger.Trace("Foo() completed at {0}.", DateTime.Now); 49 | } 50 | 51 | } 52 | } 53 | ``` 54 | 55 | 56 | License terms 57 | ------------- 58 | slf4net is published under the [MIT license](https://ef-labs.mit-license.org). 59 | 60 | 61 | Building 62 | ------------- 63 | 64 | Rebuild and run tests: 65 | 66 | ```bash 67 | msbuild -t:clean,rebuild,test -restore -p:Configuration=Release src/slf4net.sln 68 | ``` 69 | 70 | Package: 71 | 72 | ```bash 73 | version=1.0.0 74 | path=${PWD}/nuget 75 | msbuild -t:clean,rebuild,test,pack -restore -p:Configuration=Release,Version=${version},PackageOutputPath=${path} src/slf4net.sln 76 | ``` 77 | -------------------------------------------------------------------------------- /nuget-push.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | cd "${0%/*}"/nuget/ 4 | 5 | for f in *.nupkg; do 6 | nuget push "${f}" -Source https://nuget.org/ 7 | done 8 | 9 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Moqs/Factories/TestConfigurableLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | namespace slf4net.Moqs.Factories 25 | { 26 | public class TestConfigurableLoggerFactory : ILoggerFactory, IConfigurableLoggerFactory 27 | { 28 | 29 | public string FactoryData { get; set; } 30 | 31 | public ILogger GetLogger(string name) 32 | { 33 | return MoqFactory.Logger(name).Object; 34 | } 35 | 36 | public void Init(string factoryData) 37 | { 38 | this.FactoryData = factoryData; 39 | } 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Moqs/Factories/TestLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | namespace slf4net.Moqs.Factories 25 | { 26 | public class TestLoggerFactory : ILoggerFactory 27 | { 28 | public ILogger GetLogger(string name) 29 | { 30 | return MoqFactory.Logger(name).Object; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Moqs/Factories/TestNamedLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using slf4net.Factories; 25 | 26 | namespace slf4net.Moqs.Factories 27 | { 28 | public class TestNamedLoggerFactory : NamedLoggerFactoryBase 29 | { 30 | protected override ILogger CreateLogger(string name) 31 | { 32 | return MoqFactory.Logger(name).Object; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Moqs/MoqFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using Moq; 25 | 26 | namespace slf4net.Moqs 27 | { 28 | public static class MoqFactory 29 | { 30 | 31 | public static Mock Logger(string name = "") 32 | { 33 | var mock = new Mock(); 34 | mock.SetupGet(m => m.Name).Returns(name); 35 | 36 | return mock; 37 | } 38 | 39 | public static Mock LoggerFactory() 40 | { 41 | var mock = new Mock(); 42 | mock.Setup(m => m.GetLogger(It.IsAny())).Returns((string name) => Logger(name).Object); 43 | 44 | return mock; 45 | } 46 | 47 | public static Mock FactoryResolver() 48 | { 49 | var mock = new Mock(); 50 | mock.Setup(m => m.GetFactory()).Returns(MoqFactory.LoggerFactory().Object); 51 | 52 | return mock; 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Moqs/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Reflection; 25 | using System.Runtime.InteropServices; 26 | 27 | // General Information about an assembly is controlled through the following 28 | // set of attributes. Change these attribute values to modify the information 29 | // associated with an assembly. 30 | [assembly: AssemblyTitle("slf4net.Moqs")] 31 | [assembly: AssemblyDescription("")] 32 | [assembly: AssemblyConfiguration("")] 33 | [assembly: AssemblyCompany("EF Education First")] 34 | [assembly: AssemblyProduct("slf4net.Moqs")] 35 | [assembly: AssemblyCopyright("Copyright © 2020 EF Learning Labs")] 36 | [assembly: AssemblyTrademark("")] 37 | [assembly: AssemblyCulture("")] 38 | 39 | // Setting ComVisible to false makes the types in this assembly not visible 40 | // to COM components. If you need to access a type in this assembly from 41 | // COM, set the ComVisible attribute to true on that type. 42 | [assembly: ComVisible(false)] 43 | 44 | // The following GUID is for the ID of the typelib if this project is exposed to COM 45 | [assembly: Guid("b1752d74-9c4b-4269-a024-7dc533a6f259")] 46 | 47 | // Version information for an assembly consists of the following four values: 48 | // 49 | // Major Version 50 | // Minor Version 51 | // Build Number 52 | // Revision 53 | // 54 | // You can specify all the values or you can default the Build and Revision Numbers 55 | // by using the '*' as shown below: 56 | // [assembly: AssemblyVersion("1.0.*")] 57 | [assembly: AssemblyVersion("1.0.0.0")] 58 | [assembly: AssemblyFileVersion("1.0.0.0")] 59 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Moqs/slf4net.Moqs.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 2.0 7 | {6554750C-D44D-47C9-81A6-9C0353C91E81} 8 | Library 9 | Properties 10 | slf4net.Moqs 11 | slf4net.Moqs 12 | v4.8 13 | 512 14 | ..\..\ 15 | true 16 | 17 | 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | true 36 | 37 | 38 | slf4net.snk 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | {F969A551-EE0E-4129-9E88-AC5291985589} 58 | slf4net 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Moqs/slf4net.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/Tests/slf4net.Moqs/slf4net.snk -------------------------------------------------------------------------------- /src/Tests/slf4net.NLog.Tests/NLogLoggerAdapterTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using NUnit.Framework; 25 | using Moq; 26 | using NLog; 27 | using slf4net.NLog; 28 | 29 | namespace slf4net.Tests 30 | { 31 | 32 | 33 | /// 34 | ///This is a test class for NLogLoggerAdapterTest and is intended 35 | ///to contain all NLogLoggerAdapterTest Unit Tests 36 | /// 37 | [TestFixture] 38 | public class NLogLoggerAdapterTest 39 | { 40 | /// 41 | ///A test for NLogLoggerAdapter Constructor 42 | /// 43 | [Test] 44 | public void NLogLoggerAdapter_Test() 45 | { 46 | var logger = new Mock(); 47 | logger.CallBase = true; 48 | NLogLoggerAdapter target = new NLogLoggerAdapter(logger.Object); 49 | 50 | ExploratoryTester.TestILogger(target); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Tests/slf4net.NLog.Tests/NLogLoggerFactoryTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using NUnit.Framework; 25 | using slf4net.NLog; 26 | 27 | namespace slf4net.Tests 28 | { 29 | 30 | 31 | /// 32 | ///This is a test class for NLogLoggerFactoryTest and is intended 33 | ///to contain all NLogLoggerFactoryTest Unit Tests 34 | /// 35 | [TestFixture] 36 | public class NLogLoggerFactoryTest 37 | { 38 | /// 39 | ///A test for CreateLogger 40 | /// 41 | [Test] 42 | public void NLogLoggerFactory_CreateLoggerTest() 43 | { 44 | TestNLogLoggerFactory target = new TestNLogLoggerFactory(); 45 | string name = "Test logger name"; 46 | ILogger actual; 47 | 48 | actual = target.CreateLogger_ForTest(name); 49 | Assert.IsNotNull(actual); 50 | } 51 | 52 | private class TestNLogLoggerFactory : NLogLoggerFactory 53 | { 54 | 55 | public ILogger CreateLogger_ForTest(string name) 56 | { 57 | return base.CreateLogger(name); 58 | } 59 | 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Tests/slf4net.NLog.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Reflection; 25 | using System.Runtime.InteropServices; 26 | 27 | // General Information about an assembly is controlled through the following 28 | // set of attributes. Change these attribute values to modify the information 29 | // associated with an assembly. 30 | [assembly: AssemblyTitle("slf4net.NLog.Tests")] 31 | [assembly: AssemblyDescription("")] 32 | [assembly: AssemblyConfiguration("")] 33 | [assembly: AssemblyCompany("EF Education First")] 34 | [assembly: AssemblyProduct("slf4net.NLog.Tests")] 35 | [assembly: AssemblyCopyright("Copyright © 2020 EF Learning Labs")] 36 | [assembly: AssemblyTrademark("")] 37 | [assembly: AssemblyCulture("")] 38 | 39 | // Setting ComVisible to false makes the types in this assembly not visible 40 | // to COM components. If you need to access a type in this assembly from 41 | // COM, set the ComVisible attribute to true on that type. 42 | [assembly: ComVisible(false)] 43 | 44 | // The following GUID is for the ID of the typelib if this project is exposed to COM 45 | [assembly: Guid("ff62bd62-6987-40d4-8261-4ded69e46e24")] 46 | 47 | // Version information for an assembly consists of the following four values: 48 | // 49 | // Major Version 50 | // Minor Version 51 | // Build Number 52 | // Revision 53 | // 54 | // You can specify all the values or you can default the Build and Revision Numbers 55 | // by using the '*' as shown below: 56 | [assembly: AssemblyVersion("1.0.0.0")] 57 | [assembly: AssemblyFileVersion("1.0.0.0")] 58 | -------------------------------------------------------------------------------- /src/Tests/slf4net.NLog.Tests/slf4net.NLog.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 2.0 7 | {1ABB0B2B-1E3F-4BA4-8AD7-4763A16D8E00} 8 | Library 9 | Properties 10 | slf4net.Tests 11 | slf4net.NLog.Tests 12 | v4.8 13 | 512 14 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | ..\..\ 16 | true 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | true 37 | 38 | 39 | slf4net.snk 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | ExploratoryTester.cs 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | {AB8A3AB3-06C9-4D74-BE75-3CC3E72287A4} 58 | slf4net.NLog 59 | 60 | 61 | {f969a551-ee0e-4129-9e88-ac5291985589} 62 | slf4net 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /src/Tests/slf4net.NLog.Tests/slf4net.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/Tests/slf4net.NLog.Tests/slf4net.snk -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | factory configuration data 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | factory configuration data 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/BasicSlf4netServiceProviderTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using Moq; 24 | using NUnit.Framework; 25 | using slf4net.Internal; 26 | 27 | namespace slf4net.Tests 28 | { 29 | [TestFixture] 30 | public class BasicSlf4netServiceProviderTest 31 | { 32 | [Test] 33 | public void GetLoggerFactory() 34 | { 35 | var loggerFactory = Mock.Of(); 36 | var provider = new BasicSlf4netServiceProvider(loggerFactory, null); 37 | Assert.AreEqual(loggerFactory, provider.GetLoggerFactory()); 38 | } 39 | 40 | [Test] 41 | public void GetMdcAdapter() 42 | { 43 | var mdcAdapter = Mock.Of(); 44 | var provider = new BasicSlf4netServiceProvider(null, mdcAdapter); 45 | Assert.AreEqual(mdcAdapter, provider.GetMdcAdapter()); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Configuration/ConfigurationTests.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | simple factory data 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/EventLogHelperTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System; 25 | using System.Configuration; 26 | using NUnit.Framework; 27 | using slf4net.Internal; 28 | 29 | namespace slf4net.Tests 30 | { 31 | 32 | 33 | /// 34 | ///This is a test class for EventLogHelperTest and is intended 35 | ///to contain all EventLogHelperTest Unit Tests 36 | /// 37 | [TestFixture] 38 | public class EventLogHelperTest 39 | { 40 | /// 41 | ///A test for WriteEntry 42 | /// 43 | [Test] 44 | public void EventLogHelper_WriteEntry() 45 | { 46 | string message = "Test message"; 47 | 48 | ConsoleHelper.WriteLine(message); 49 | 50 | } 51 | 52 | /// 53 | ///A test for WriteEntry 54 | /// 55 | [Test] 56 | public void EventLogHelper_WriteEntry_NoException() 57 | { 58 | string message = "Test message"; 59 | Exception ex = null; 60 | 61 | ConsoleHelper.WriteLine(message, ex); 62 | 63 | } 64 | 65 | /// 66 | ///A test for WriteEntry 67 | /// 68 | [Test] 69 | public void EventLogHelper_WriteEntry_WithException() 70 | { 71 | string message = "Test message"; 72 | 73 | try 74 | { 75 | ThrowException1(); 76 | } 77 | catch (Exception ex) 78 | { 79 | ConsoleHelper.WriteLine(message, ex); 80 | } 81 | 82 | } 83 | 84 | private void ThrowException1() 85 | { 86 | ThrowException2(); 87 | } 88 | 89 | private void ThrowException2() 90 | { 91 | ThrowException3(); 92 | } 93 | 94 | private void ThrowException3() 95 | { 96 | try 97 | { 98 | throw new Exception("Inner unit test exception"); 99 | } 100 | catch (Exception ex) 101 | { 102 | throw new ConfigurationErrorsException("Outer unit test exception", ex); 103 | } 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/ExploratoryTester.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Diagnostics; 27 | using System.Reflection; 28 | 29 | namespace slf4net.Tests 30 | { 31 | /// 32 | /// A class that performs exploratory tests on classes 33 | /// 34 | public static class ExploratoryTester 35 | { 36 | /// 37 | /// Performs exploratory testing on an ILogger instance. This 38 | /// test for robustness against null values being passed for 39 | /// all method arguments 40 | /// 41 | /// 42 | /// A list of failures 43 | public static List TestILogger(ILogger logger) 44 | { 45 | List failures = new List(); 46 | 47 | // obtain all the methods 48 | Type iLoggerType = typeof(ILogger); 49 | MethodInfo[] methods = iLoggerType.GetMethods(); 50 | 51 | foreach (MethodInfo method in methods) 52 | { 53 | // skip the Log method, it does not accept null (should it?) 54 | if (method.Name == "Log") 55 | continue; 56 | 57 | string result = TestILoggerMethod(logger, method); 58 | if (result != null) 59 | { 60 | failures.Add(result); 61 | } 62 | } 63 | 64 | return failures; 65 | } 66 | 67 | /// 68 | /// Creates an argument list suitabel for invocation of the 69 | /// given ILogger method 70 | /// 71 | /// 72 | /// 73 | public static List CreateArgumentList(MethodInfo method) 74 | { 75 | // construct a null argument list 76 | List argumentList = new List(); 77 | foreach (ParameterInfo parameter in method.GetParameters()) 78 | { 79 | // currenlty the xxxFormat methods delegate to String.Format 80 | // for formatting. This method will throw an exception if either 81 | // the format string or argument list is null. Therefore, we 82 | // ensure that this does not happen here. 83 | if (parameter.Name == "format") 84 | { 85 | argumentList.Add(""); 86 | } 87 | else if (parameter.Name == "args") 88 | { 89 | argumentList.Add(new object[] {""}); 90 | } 91 | else 92 | { 93 | argumentList.Add(null); 94 | } 95 | } 96 | 97 | return argumentList; 98 | } 99 | 100 | /// 101 | /// Tests an individual ILogger method 102 | /// 103 | /// Details of the failure - if one occured. 104 | /// Or null otherwise 105 | private static string TestILoggerMethod(ILogger logger, MethodInfo method) 106 | { 107 | List argumentList = CreateArgumentList(method); 108 | 109 | // invoke the method and ensure that it does not throw an expcetion. 110 | try 111 | { 112 | Debug.WriteLine("Invoking method: " + method.ToString()); 113 | method.Invoke(logger, argumentList.ToArray()); 114 | } 115 | catch (Exception e) 116 | { 117 | return string.Format("The method {0} failed with exception {1}", 118 | method, e.InnerException); 119 | } 120 | 121 | return null; 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Factories/Internal/SubstituteLoggerFactoryTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Collections.Generic; 25 | using NUnit.Framework; 26 | using slf4net.Factories.Internal; 27 | 28 | namespace slf4net.Tests.Factories.Internal 29 | { 30 | 31 | 32 | /// 33 | ///This is a test class for SubstituteLoggerFactoryTest and is intended 34 | ///to contain all SubstituteLoggerFactoryTest Unit Tests 35 | /// 36 | [TestFixture] 37 | public class SubstituteLoggerFactoryTest 38 | { 39 | /// 40 | ///A test for GetLogger 41 | /// 42 | [Test] 43 | public void Factories_SubstituteLoggerFactory_GetLoggerTest() 44 | { 45 | SubstituteLoggerFactory target = new SubstituteLoggerFactory(); 46 | string name = "foo"; 47 | ILogger expected = NOPLogger.Instance; 48 | ILogger actual; 49 | 50 | actual = target.GetLogger(name); 51 | Assert.AreEqual(expected, actual); 52 | 53 | } 54 | 55 | /// 56 | ///A test for GetLoggerNameList 57 | /// 58 | [Test] 59 | public void Factories_SubstituteLoggerFactory_GetLoggerNameListTest() 60 | { 61 | SubstituteLoggerFactory target = new SubstituteLoggerFactory(); 62 | string name = "foo"; 63 | IList actual; 64 | 65 | actual = target.GetLoggerNameList(); 66 | Assert.AreEqual(0, actual.Count); 67 | 68 | target.GetLogger(name).Debug("test 1"); 69 | 70 | actual = target.GetLoggerNameList(); 71 | Assert.AreEqual(1, actual.Count); 72 | Assert.AreEqual(name, actual[0]); 73 | 74 | name = "bar"; 75 | target.GetLogger(name).Debug("test 2"); 76 | 77 | actual = target.GetLoggerNameList(); 78 | Assert.AreEqual(2, actual.Count); 79 | Assert.AreEqual(name, actual[1]); 80 | 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Factories/NOPLoggerFactoryTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using NUnit.Framework; 25 | using slf4net.Factories; 26 | 27 | namespace slf4net.Tests.Factories 28 | { 29 | 30 | 31 | /// 32 | ///This is a test class for NOPLoggerFactoryTest and is intended 33 | ///to contain all NOPLoggerFactoryTest Unit Tests 34 | /// 35 | [TestFixture] 36 | public class NOPLoggerFactoryTest 37 | { 38 | /// 39 | ///A test for GetLogger 40 | /// 41 | [Test] 42 | public void Factories_NOPLoggerFactory_GetLogger_ReturnsNOPLoggerSingleton() 43 | { 44 | var target = NOPLoggerFactory.Instance; 45 | string name = "foo"; 46 | ILogger expected = NOPLogger.Instance; 47 | ILogger actual; 48 | 49 | actual = target.GetLogger(name); 50 | Assert.AreEqual(expected, actual); 51 | } 52 | 53 | /// 54 | ///A test for GetLogger 55 | /// 56 | [Test] 57 | public void Factories_NOPLoggerFactory_GetLogger_ReturnsSameLogger() 58 | { 59 | var target = NOPLoggerFactory.Instance; 60 | string name = "foo"; 61 | ILogger expected = NOPLogger.Instance; 62 | ILogger actual; 63 | 64 | actual = target.GetLogger(name); 65 | Assert.AreEqual(expected, actual); 66 | 67 | string name2 = "other"; 68 | var logger2 = target.GetLogger(name2); 69 | 70 | Assert.AreEqual(actual, logger2); 71 | } 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Factories/NamedLoggerFactoryBaseTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using NUnit.Framework; 25 | using slf4net.Factories; 26 | using slf4net.Moqs.Factories; 27 | 28 | namespace slf4net.Tests.Factories 29 | { 30 | [TestFixture] 31 | public class NamedLoggerFactoryBaseTest 32 | { 33 | [Test] 34 | public void Factories_NamedLoggerFactoryBase_GetLogger_ReturnsLoggerWithGivenName() 35 | { 36 | NamedLoggerFactoryBase target = new TestNamedLoggerFactory(); 37 | 38 | ILogger logger = target.GetLogger("foo"); 39 | Assert.AreEqual("foo", logger.Name); 40 | } 41 | 42 | [Test] 43 | public void Factories_NamedLoggerFactoryBase_GetLogger_With_Null_Uses_Default_Name() 44 | { 45 | NamedLoggerFactoryBase target = new TestNamedLoggerFactory(); 46 | 47 | var logger = target.GetLogger(null); 48 | Assert.AreEqual(string.Empty, logger.Name); 49 | } 50 | 51 | [Test] 52 | public void Factories_NamedLoggerFactoryBase_GetLogger_WithSameName_ReturnsSameInstance() 53 | { 54 | NamedLoggerFactoryBase target = new TestNamedLoggerFactory(); 55 | 56 | ILogger logger = target.GetLogger("foo"); 57 | ILogger loggerTwo = target.GetLogger("foo"); 58 | 59 | Assert.AreSame(logger, loggerTwo); 60 | } 61 | 62 | [Test] 63 | public void Factories_NamedLoggerFactoryBase_GetLogger_WithSameNameCaseInsensitive_ReturnsSameInstance() 64 | { 65 | NamedLoggerFactoryBase target = new TestNamedLoggerFactory(); 66 | 67 | ILogger logger = target.GetLogger("foo"); 68 | ILogger loggerTwo = target.GetLogger("FOO"); 69 | 70 | Assert.AreSame(logger, loggerTwo); 71 | } 72 | 73 | [Test] 74 | public void Factories_NamedLoggerFactoryBase_Requesting_Loggers_By_Different_Names_Returns_Different_Instances() 75 | { 76 | NamedLoggerFactoryBase target = new TestNamedLoggerFactory(); 77 | 78 | ILogger logger = target.GetLogger("foo"); 79 | ILogger loggerTwo = target.GetLogger("foo.bar"); 80 | 81 | Assert.AreNotSame(logger, loggerTwo); 82 | } 83 | 84 | [Test] 85 | public void Factories_NamedLoggerFactoryBase_GetLogger_WithDifferentNames_ReturnsLoggerWithGivenNames() 86 | { 87 | NamedLoggerFactoryBase target = new TestNamedLoggerFactory(); 88 | 89 | string name1 = "foo"; 90 | string name2 = "foo.bar"; 91 | 92 | ILogger logger1 = target.GetLogger(name1); 93 | ILogger logger2 = target.GetLogger(name2); 94 | 95 | Assert.AreEqual(name1, logger1.Name); 96 | Assert.AreEqual(name2, logger2.Name); 97 | 98 | logger2 = target.GetLogger(name1); 99 | Assert.AreEqual(logger1, logger2); 100 | 101 | logger1 = target.GetLogger(name2); 102 | logger2 = target.GetLogger(name2); 103 | Assert.AreEqual(logger1, logger2); 104 | } 105 | 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/GDCTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using Moq; 24 | using NUnit.Framework; 25 | 26 | namespace slf4net.Tests 27 | { 28 | [TestFixture] 29 | public class GDCTest 30 | { 31 | private readonly IMdcAdapter _mockAdapter = Mock.Of(); 32 | 33 | [SetUp] 34 | public void SetUp() 35 | { 36 | var resolver = Mock.Of(); 37 | var provider = Mock.Of(); 38 | 39 | Mock.Get(resolver).Setup(r => r.GetProvider()).Returns(provider); 40 | Mock.Get(provider).Setup(p => p.GetMdcAdapter()).Returns(_mockAdapter); 41 | 42 | LoggerFactory.SetServiceProviderResolver(resolver); 43 | } 44 | 45 | [Test] 46 | public void Get() 47 | { 48 | const string key = "key1"; 49 | var expected = new object(); 50 | 51 | Mock.Get(_mockAdapter).Setup(a => a.GlobalGet(key)).Returns(expected); 52 | 53 | var actual = GDC.Get(key); 54 | 55 | Assert.AreEqual(expected, actual); 56 | Mock.Get(_mockAdapter).Verify(a => a.GlobalGet(key), Times.Once); 57 | } 58 | 59 | [Test] 60 | public void Set() 61 | { 62 | const string key = "key1"; 63 | var expected = new object(); 64 | 65 | GDC.Set(key, expected); 66 | Mock.Get(_mockAdapter).Verify(a => a.GlobalSet(key, expected), Times.Once); 67 | } 68 | 69 | [Test] 70 | public void Remove() 71 | { 72 | const string key = "key1"; 73 | 74 | GDC.Remove(key); 75 | Mock.Get(_mockAdapter).Verify(a => a.GlobalRemove(key), Times.Once); 76 | } 77 | 78 | [Test] 79 | public void Clear() 80 | { 81 | GDC.Clear(); 82 | Mock.Get(_mockAdapter).Verify(a => a.GlobalClear(), Times.Once); 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Internal/NOPMdcAdapterTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using NUnit.Framework; 24 | using slf4net.Internal; 25 | 26 | namespace slf4net.Tests.Internal 27 | { 28 | [TestFixture] 29 | public class NOPMdcAdapterTest 30 | { 31 | private static readonly string Key = "key"; 32 | private static readonly object Value = new object(); 33 | private readonly NOPMdcAdapter _adapter = NOPMdcAdapter.Instance; 34 | 35 | [Test] 36 | public void Get() 37 | { 38 | Assert.IsNull(_adapter.Get(Key)); 39 | Assert.IsNull(_adapter.Get(null)); 40 | } 41 | 42 | [Test] 43 | public void Set() 44 | { 45 | _adapter.Set(Key, Value); 46 | _adapter.Set(null, null); 47 | } 48 | 49 | [Test] 50 | public void Remove() 51 | { 52 | _adapter.Remove(Key); 53 | _adapter.Remove(null); 54 | } 55 | 56 | [Test] 57 | public void Clear() 58 | { 59 | _adapter.Clear(); 60 | } 61 | 62 | [Test] 63 | public void LogicalGet() 64 | { 65 | Assert.IsNull(_adapter.LogicalGet(Key)); 66 | Assert.IsNull(_adapter.LogicalGet(null)); 67 | } 68 | 69 | [Test] 70 | public void LogicalSet() 71 | { 72 | _adapter.LogicalSet(Key, Value); 73 | _adapter.LogicalSet(null, null); 74 | } 75 | 76 | [Test] 77 | public void LogicalRemove() 78 | { 79 | _adapter.LogicalRemove(Key); 80 | _adapter.LogicalRemove(null); 81 | } 82 | 83 | [Test] 84 | public void LogicalClear() 85 | { 86 | _adapter.LogicalClear(); 87 | } 88 | 89 | [Test] 90 | public void GlobalGet() 91 | { 92 | Assert.IsNull(_adapter.GlobalGet(Key)); 93 | Assert.IsNull(_adapter.GlobalGet(null)); 94 | } 95 | 96 | [Test] 97 | public void GlobalSet() 98 | { 99 | _adapter.GlobalSet(Key, Value); 100 | _adapter.GlobalSet(null, null); 101 | } 102 | 103 | [Test] 104 | public void GlobalRemove() 105 | { 106 | _adapter.GlobalRemove(Key); 107 | _adapter.GlobalRemove(null); 108 | } 109 | 110 | [Test] 111 | public void GlobalClear() 112 | { 113 | _adapter.GlobalClear(); 114 | } 115 | } 116 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Internal/NOPSlf4netServiceProviderTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using NUnit.Framework; 24 | using slf4net.Factories; 25 | using slf4net.Internal; 26 | 27 | namespace slf4net.Tests.Internal 28 | { 29 | [TestFixture] 30 | public class NOPSlf4netServiceProviderTest 31 | { 32 | private readonly NOPSlf4netServiceProvider _provider = NOPSlf4netServiceProvider.Instance; 33 | 34 | [Test] 35 | public void GetLoggerFactory() 36 | { 37 | var loggerFactory = _provider.GetLoggerFactory(); 38 | Assert.That(loggerFactory, Is.InstanceOf()); 39 | } 40 | 41 | [Test] 42 | public void GetMdcAdapter() 43 | { 44 | var mdcAdapter = _provider.GetMdcAdapter(); 45 | Assert.That(mdcAdapter, Is.InstanceOf()); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Internal/SubstituteServiceProviderTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using NUnit.Framework; 24 | using slf4net.Factories.Internal; 25 | using slf4net.Internal; 26 | 27 | namespace slf4net.Tests.Internal 28 | { 29 | [TestFixture] 30 | public class SubstituteServiceProviderTest 31 | { 32 | private readonly SubstituteServiceProvider _provider = new SubstituteServiceProvider(); 33 | 34 | [Test] 35 | public void GetLoggerFactory() 36 | { 37 | var loggerFactory = _provider.GetLoggerFactory(); 38 | Assert.That(loggerFactory, Is.InstanceOf()); 39 | } 40 | 41 | [Test] 42 | public void GetMdcAdapter() 43 | { 44 | var mdcAdapter = _provider.GetMdcAdapter(); 45 | Assert.That(mdcAdapter, Is.InstanceOf()); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/MDCTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using Moq; 24 | using NUnit.Framework; 25 | 26 | namespace slf4net.Tests 27 | { 28 | [TestFixture] 29 | public class MDCTest 30 | { 31 | private readonly IMdcAdapter _mockAdapter = Mock.Of(); 32 | 33 | [SetUp] 34 | public void SetUp() 35 | { 36 | var resolver = Mock.Of(); 37 | var provider = Mock.Of(); 38 | 39 | Mock.Get(resolver).Setup(r => r.GetProvider()).Returns(provider); 40 | Mock.Get(provider).Setup(p => p.GetMdcAdapter()).Returns(_mockAdapter); 41 | 42 | LoggerFactory.SetServiceProviderResolver(resolver); 43 | } 44 | 45 | [Test] 46 | public void Get() 47 | { 48 | const string key = "key1"; 49 | var expected = new object(); 50 | 51 | Mock.Get(_mockAdapter).Setup(a => a.Get(key)).Returns(expected); 52 | 53 | var actual = MDC.Get(key); 54 | 55 | Assert.AreEqual(expected, actual); 56 | Mock.Get(_mockAdapter).Verify(a => a.Get(key), Times.Once); 57 | } 58 | 59 | [Test] 60 | public void Set() 61 | { 62 | const string key = "key1"; 63 | var expected = new object(); 64 | 65 | MDC.Set(key, expected); 66 | Mock.Get(_mockAdapter).Verify(a => a.Set(key, expected), Times.Once); 67 | } 68 | 69 | [Test] 70 | public void Remove() 71 | { 72 | const string key = "key1"; 73 | 74 | MDC.Remove(key); 75 | Mock.Get(_mockAdapter).Verify(a => a.Remove(key), Times.Once); 76 | } 77 | 78 | [Test] 79 | public void Clear() 80 | { 81 | MDC.Clear(); 82 | Mock.Get(_mockAdapter).Verify(a => a.Clear(), Times.Once); 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/MDLCTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using Moq; 24 | using NUnit.Framework; 25 | 26 | namespace slf4net.Tests 27 | { 28 | [TestFixture] 29 | public class MDLCTest 30 | { 31 | private readonly IMdcAdapter _mockAdapter = Mock.Of(); 32 | 33 | [SetUp] 34 | public void SetUp() 35 | { 36 | var resolver = Mock.Of(); 37 | var provider = Mock.Of(); 38 | 39 | Mock.Get(resolver).Setup(r => r.GetProvider()).Returns(provider); 40 | Mock.Get(provider).Setup(p => p.GetMdcAdapter()).Returns(_mockAdapter); 41 | 42 | LoggerFactory.SetServiceProviderResolver(resolver); 43 | } 44 | 45 | [Test] 46 | public void Get() 47 | { 48 | const string key = "key1"; 49 | var expected = new object(); 50 | 51 | Mock.Get(_mockAdapter).Setup(a => a.LogicalGet(key)).Returns(expected); 52 | 53 | var actual = MDLC.Get(key); 54 | 55 | Assert.AreEqual(expected, actual); 56 | Mock.Get(_mockAdapter).Verify(a => a.LogicalGet(key), Times.Once); 57 | } 58 | 59 | [Test] 60 | public void Set() 61 | { 62 | const string key = "key1"; 63 | var expected = new object(); 64 | 65 | MDLC.Set(key, expected); 66 | Mock.Get(_mockAdapter).Verify(a => a.LogicalSet(key, expected), Times.Once); 67 | } 68 | 69 | [Test] 70 | public void Remove() 71 | { 72 | const string key = "key1"; 73 | 74 | MDLC.Remove(key); 75 | Mock.Get(_mockAdapter).Verify(a => a.LogicalRemove(key), Times.Once); 76 | } 77 | 78 | [Test] 79 | public void Clear() 80 | { 81 | MDLC.Clear(); 82 | Mock.Get(_mockAdapter).Verify(a => a.LogicalClear(), Times.Once); 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/NOPLoggerTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Collections.Generic; 25 | using System.Linq; 26 | using NUnit.Framework; 27 | 28 | namespace slf4net.Tests 29 | { 30 | [TestFixture] 31 | public class NOPLoggerTest 32 | { 33 | 34 | [Test] 35 | public void NOPLogger_NameTest() 36 | { 37 | NOPLogger logger = NOPLogger.Instance; 38 | string actual; 39 | string expected = "NOP"; 40 | 41 | actual = logger.Name; 42 | Assert.AreEqual(expected, actual); 43 | } 44 | 45 | [Test] 46 | public void NOPLogger_AllLogMethodsTest() 47 | { 48 | NOPLogger logger = NOPLogger.Instance; 49 | 50 | List failures = ExploratoryTester.TestILogger(logger); 51 | 52 | Assert.AreEqual(0, failures.Count, 53 | failures.Count != 0 ? failures.Aggregate((s1, s2) => s1 + System.Environment.NewLine + s2) : ""); 54 | 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("slf4net.Tests")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("EF Education First")] 11 | [assembly: AssemblyProduct("slf4net.Tests")] 12 | [assembly: AssemblyCopyright("Copyright © 2020 EF Learning Labs")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM componenets. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("0d7c879b-92d4-4e16-bc66-87ade32da01b")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Revision and Build Numbers 32 | // by using the '*' as shown below: 33 | [assembly: AssemblyVersion("1.0.0.0")] 34 | [assembly: AssemblyFileVersion("1.0.0.0")] 35 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Resolvers/AppConfigFactoryResolverTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System; 25 | using System.Configuration; 26 | using NUnit.Framework; 27 | using slf4net.Internal; 28 | using slf4net.Moqs.Factories; 29 | using slf4net.Resolvers; 30 | 31 | namespace slf4net.Tests 32 | { 33 | /// 34 | ///This is a test class for AppConfigFactoryResolverTest and is intended 35 | ///to contain all AppConfigFactoryResolverTest Unit Tests 36 | /// 37 | [TestFixture] 38 | public class AppConfigFactoryResolverTest 39 | { 40 | /// 41 | ///A test for AppConfigFactoryResolver Constructor 42 | /// 43 | [Test] 44 | public void Resolvers_AppConfigFactoryResolver_MissingSection() 45 | { 46 | var target = new AppConfigFactoryResolver(); 47 | 48 | var provider = target.GetProvider(); 49 | Assert.IsNull(provider); 50 | 51 | var factory = target.GetFactory(); 52 | Assert.IsNull(factory); 53 | } 54 | 55 | /// 56 | ///A test for AppConfigFactoryResolver Constructor 57 | /// 58 | [Test] 59 | public void Resolvers_AppConfigFactoryResolver_InvalidSection() 60 | { 61 | try 62 | { 63 | AppConfigFactoryResolver target = new AppConfigFactoryResolver("slf4net-wrong-type"); 64 | Assert.Fail(); 65 | } 66 | catch (ConfigurationErrorsException) 67 | { 68 | // Expected 69 | } 70 | } 71 | 72 | /// 73 | ///A test for AppConfigFactoryResolver Constructor 74 | /// 75 | [Test] 76 | public void Resolvers_AppConfigFactoryResolver_AltSectionName1() 77 | { 78 | var target = new AppConfigFactoryResolver("slf4net-alt-name1"); 79 | 80 | var provider = target.GetProvider(); 81 | Assert.IsInstanceOf(provider); 82 | 83 | var factory = target.GetFactory(); 84 | Assert.IsInstanceOf(factory); 85 | } 86 | 87 | /// 88 | ///A test for AppConfigFactoryResolver Constructor 89 | /// 90 | [Test] 91 | public void Resolvers_AppConfigFactoryResolver_AltSectionName2() 92 | { 93 | var target = new AppConfigFactoryResolver("slf4net-alt-name2"); 94 | 95 | var provider = target.GetProvider(); 96 | Assert.IsInstanceOf(provider); 97 | 98 | var factory = target.GetFactory(); 99 | Assert.IsInstanceOf(factory); 100 | } 101 | 102 | /// 103 | ///A test for AppConfigFactoryResolver Constructor 104 | /// 105 | [Test] 106 | public void Resolvers_AppConfigFactoryResolver_InvalidType() 107 | { 108 | try 109 | { 110 | var target = new AppConfigFactoryResolver("slf4net-invalid"); 111 | Assert.Fail(); 112 | } 113 | catch (TypeLoadException) 114 | { 115 | // Expected 116 | } 117 | } 118 | 119 | /// 120 | ///A test for AppConfigFactoryResolver Constructor 121 | /// 122 | [Test] 123 | public void Resolvers_AppConfigFactoryResolver_MissingType() 124 | { 125 | try 126 | { 127 | var target = new AppConfigFactoryResolver("slf4net-missing"); 128 | Assert.Fail(); 129 | } 130 | catch (TypeLoadException) 131 | { 132 | // Expected 133 | } 134 | } 135 | 136 | /// 137 | ///A test for AppConfigFactoryResolver Constructor 138 | /// 139 | [Test] 140 | public void Resolvers_AppConfigFactoryResolver_WrongType() 141 | { 142 | try 143 | { 144 | var target = new AppConfigFactoryResolver("slf4net-wrong-factory-type"); 145 | Assert.Fail(); 146 | } 147 | catch (InvalidCastException) 148 | { 149 | // Expected 150 | } 151 | } 152 | 153 | /// 154 | ///A test for AppConfigFactoryResolver Constructor 155 | /// 156 | [Test] 157 | public void Resolvers_AppConfigFactoryResolver_ConfigurableFactory() 158 | { 159 | AppConfigFactoryResolver target = new AppConfigFactoryResolver("slf4net-configurable"); 160 | 161 | var factory = target.GetFactory(); 162 | Assert.IsNotNull(factory); 163 | Assert.IsInstanceOf(factory); 164 | 165 | var cf = (TestConfigurableLoggerFactory) factory; 166 | Assert.AreEqual("factory configuration data", cf.FactoryData); 167 | } 168 | 169 | /// 170 | ///A test for AppConfigFactoryResolver Constructor 171 | /// 172 | [Test] 173 | public void Resolvers_AppConfigFactoryResolver_ConfigurableFactoryNoData() 174 | { 175 | AppConfigFactoryResolver target = new AppConfigFactoryResolver("slf4net-configurable-no-data"); 176 | 177 | var factory = target.GetFactory(); 178 | Assert.IsNotNull(factory); 179 | Assert.IsInstanceOf(factory); 180 | 181 | var cf = (TestConfigurableLoggerFactory) factory; 182 | Assert.AreEqual(null, cf.FactoryData); 183 | } 184 | 185 | /// 186 | ///A test for AppConfigFactoryResolver Constructor 187 | /// 188 | [Test] 189 | public void Resolvers_AppConfigFactoryResolver_ConfigurableFactoryInvalid() 190 | { 191 | try 192 | { 193 | AppConfigFactoryResolver target = new AppConfigFactoryResolver("slf4net-configurable-invalid"); 194 | Assert.Fail(); 195 | } 196 | catch (ConfigurationErrorsException) 197 | { 198 | // Expected 199 | } 200 | } 201 | 202 | public class TestConfigurationSection : ConfigurationSection 203 | { 204 | } 205 | } 206 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Resolvers/LegacyServiceProviderResolverTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using System; 24 | using Moq; 25 | using NUnit.Framework; 26 | using slf4net.Factories; 27 | using slf4net.Internal; 28 | using slf4net.Resolvers; 29 | 30 | namespace slf4net.Tests.Resolvers 31 | { 32 | [TestFixture] 33 | public class LegacyServiceProviderResolverTest 34 | { 35 | [Test] 36 | public void Ctor_Null() 37 | { 38 | try 39 | { 40 | var resolver = new LegacyServiceProviderResolver(null); 41 | Assert.Fail(); 42 | } 43 | catch (ArgumentNullException) 44 | { 45 | // Expected 46 | } 47 | } 48 | 49 | [Test] 50 | public void Ctor_Null_LoggerFactory() 51 | { 52 | var factoryResolver = Mock.Of(); 53 | var resolver = new LegacyServiceProviderResolver(factoryResolver); 54 | var provider = resolver.GetProvider(); 55 | 56 | Assert.NotNull(provider); 57 | Assert.That(provider.GetLoggerFactory(), Is.InstanceOf()); 58 | Assert.That(provider.GetMdcAdapter(), Is.InstanceOf()); 59 | } 60 | 61 | [Test] 62 | public void Ctor_FactoryResolver() 63 | { 64 | var factoryResolver = Mock.Of(); 65 | var loggerFactory = Mock.Of(); 66 | 67 | Mock.Get(factoryResolver).Setup(r => r.GetFactory()).Returns(loggerFactory); 68 | 69 | var resolver = new LegacyServiceProviderResolver(factoryResolver); 70 | var provider = resolver.GetProvider(); 71 | 72 | Assert.NotNull(provider); 73 | Assert.AreSame(loggerFactory, provider.GetLoggerFactory()); 74 | Assert.That(provider.GetMdcAdapter(), Is.InstanceOf()); 75 | } 76 | 77 | [Test] 78 | public void Ctor_ServiceProvider() 79 | { 80 | var mock = new Mock(); 81 | mock.As(); 82 | var serviceProvider = mock.Object; 83 | var factoryResolver = Mock.Of(); 84 | 85 | Mock.Get(factoryResolver).Setup(r => r.GetFactory()).Returns(serviceProvider); 86 | 87 | var resolver = new LegacyServiceProviderResolver(factoryResolver); 88 | var provider = resolver.GetProvider(); 89 | 90 | Assert.NotNull(provider); 91 | Assert.AreSame(serviceProvider, provider); 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/Resolvers/NOPLoggerFactoryResolverTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using NUnit.Framework; 25 | using slf4net.Factories; 26 | using slf4net.Resolvers; 27 | 28 | namespace slf4net.Tests 29 | { 30 | 31 | 32 | /// 33 | ///This is a test class for NOPLoggerFactoryResolverTest and is intended 34 | ///to contain all NOPLoggerFactoryResolverTest Unit Tests 35 | /// 36 | [TestFixture] 37 | public class NOPLoggerFactoryResolverTest 38 | { 39 | 40 | /// 41 | ///A test for GetFactory 42 | /// 43 | [Test] 44 | public void Resolvers_NOPLoggerFactoryResolver_GetFactoryTest() 45 | { 46 | NOPLoggerFactoryResolver target = NOPLoggerFactoryResolver.Instance; 47 | ILoggerFactory expected = NOPLoggerFactory.Instance; 48 | ILoggerFactory actual; 49 | 50 | actual = target.GetFactory(); 51 | Assert.AreEqual(expected, actual); 52 | 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/slf4net.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 2.0 7 | {2AECA60D-4A10-433C-881A-ADE69E2A3DA1} 8 | Library 9 | Properties 10 | slf4net.Tests 11 | slf4net.Tests 12 | v4.8 13 | 512 14 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | ..\..\ 16 | true 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | true 37 | 38 | 39 | slf4net.snk 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | Always 73 | 74 | 75 | 76 | 77 | 78 | {F969A551-EE0E-4129-9E88-AC5291985589} 79 | slf4net 80 | 81 | 82 | {6554750C-D44D-47C9-81A6-9C0353C91E81} 83 | slf4net.Moqs 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /src/Tests/slf4net.Tests/slf4net.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/Tests/slf4net.Tests/slf4net.snk -------------------------------------------------------------------------------- /src/Tests/slf4net.log4net.Tests/Log4netLoggerAdapterTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using log4net; 25 | using NUnit.Framework; 26 | using Moq; 27 | using slf4net.log4net; 28 | using Core = log4net.Core; 29 | 30 | namespace slf4net.Tests 31 | { 32 | 33 | 34 | /// 35 | ///This is a test class for Log4netLoggerAdapterTest and is intended 36 | ///to contain all Log4netLoggerAdapterTest Unit Tests 37 | /// 38 | [TestFixture] 39 | public class Log4netLoggerAdapterTest 40 | { 41 | /// 42 | ///A test for Log4netLoggerAdapter Constructor 43 | /// 44 | [Test] 45 | public void Log4netLoggerAdapter_Test() 46 | { 47 | var log = new Mock(); 48 | log.SetupGet(m => m.Name).Returns("Test logger name"); 49 | log.Setup(m => m.IsEnabledFor(It.IsAny())).Returns(true); 50 | 51 | var logger = new Mock(); 52 | logger.SetupGet(m => m.Logger).Returns(log.Object); 53 | 54 | Log4netLoggerAdapter target = new Log4netLoggerAdapter(logger.Object); 55 | ExploratoryTester.TestILogger(target); 56 | 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Tests/slf4net.log4net.Tests/Log4netLoggerFactoryTest.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System; 25 | using System.Collections.Generic; 26 | using System.IO; 27 | using System.Xml; 28 | using log4net.Repository; 29 | using Moq; 30 | using NUnit.Framework; 31 | using slf4net.log4net; 32 | using slf4net.log4net.Internal; 33 | 34 | namespace slf4net.Tests 35 | { 36 | /// 37 | ///This is a test class for Log4netLoggerFactoryTest and is intended 38 | ///to contain all Log4netLoggerFactoryTest Unit Tests 39 | /// 40 | [TestFixture] 41 | public class Log4netLoggerFactoryTest 42 | { 43 | private Mock _configurator; 44 | private TestLog4netLoggerFactory _target; 45 | 46 | [SetUp] 47 | public void SetUp() 48 | { 49 | _configurator = new Mock(); 50 | _target = new TestLog4netLoggerFactory(_configurator.Object); 51 | Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory; 52 | } 53 | 54 | /// 55 | ///A test for CreateLogger 56 | /// 57 | [Test] 58 | public void Log4netLoggerFactory_CreateLoggerTest() 59 | { 60 | var name = "Test logger name"; 61 | 62 | var actual = _target.CreateLogger_ForTest(name); 63 | Assert.IsNotNull(actual); 64 | Assert.IsInstanceOf(actual); 65 | } 66 | 67 | /// 68 | ///A test for Init 69 | /// 70 | [Test] 71 | public void Log4netLoggerFactory_Init_NullTest() 72 | { 73 | _target.Init(null); 74 | _configurator.Verify(mock => mock.Configure(It.IsAny()), Times.Once); 75 | } 76 | 77 | /// 78 | ///A test for Init 79 | /// 80 | [Test] 81 | public void Log4netLoggerFactory_Init_EmptyTest() 82 | { 83 | _target.Init(string.Empty); 84 | _configurator.Verify(mock => mock.Configure(It.IsAny()), Times.Once); 85 | } 86 | 87 | /// 88 | ///A test for Init 89 | /// 90 | [Test] 91 | public void Log4netLoggerFactory_Init_XmlTest() 92 | { 93 | string factoryData; 94 | 95 | factoryData = ""; 96 | _target.Init(factoryData); 97 | _configurator.Verify(mock => mock.Configure(It.IsAny()), Times.Once); 98 | } 99 | 100 | /// 101 | ///A test for Init 102 | /// 103 | [Test] 104 | public void Log4netLoggerFactory_Init_BadXmlTest() 105 | { 106 | var factoryData = ""; 107 | 108 | try 109 | { 110 | _target.Init(factoryData); 111 | Assert.Fail(); 112 | } 113 | catch (XmlException) 114 | { 115 | // Expected 116 | } 117 | } 118 | 119 | /// 120 | ///A test for Init 121 | /// 122 | [Test] 123 | public void Log4netLoggerFactory_Init_ValidXmlTest() 124 | { 125 | var captures = new List(); 126 | _configurator.Setup(x => x.Configure(It.IsAny(), Capture.In(captures))); 127 | 128 | var factoryData = 129 | ""; 130 | _target.Init(factoryData); 131 | 132 | _configurator.Verify(mock => mock.Configure(It.IsAny(), It.IsAny()), 133 | Times.Once); 134 | 135 | Assert.AreEqual(1, captures.Count); 136 | Assert.AreEqual(Path.Combine(TestContext.CurrentContext.TestDirectory, "log4net.config"), 137 | captures[0].FullName); 138 | } 139 | 140 | /// 141 | ///A test for Init 142 | /// 143 | [Test] 144 | public void Log4netLoggerFactory_Init_ValidXml_Missing_Test() 145 | { 146 | var factoryData = 147 | ""; 148 | _target.Init(factoryData); 149 | 150 | _configurator.Verify(mock => mock.Configure(It.IsAny()), 151 | Times.Once); 152 | } 153 | 154 | /// 155 | ///A test for Init 156 | /// 157 | [Test] 158 | public void Log4netLoggerFactory_Init_ValidXml_Watch_Test() 159 | { 160 | var captures = new List(); 161 | _configurator.Setup(x => x.ConfigureAndWatch(It.IsAny(), Capture.In(captures))); 162 | 163 | var factoryData = 164 | ""; 165 | _target.Init(factoryData); 166 | 167 | _configurator.Verify(mock => mock.ConfigureAndWatch(It.IsAny(), It.IsAny()), 168 | Times.Once); 169 | 170 | Assert.AreEqual(1, captures.Count); 171 | Assert.AreEqual(Path.Combine(TestContext.CurrentContext.TestDirectory, "log4net.config"), 172 | captures[0].FullName); 173 | } 174 | 175 | /// 176 | ///A test for Init 177 | /// 178 | [Test] 179 | public void Log4netLoggerFactory_Init_ValidXml_MultiConfig_Test() 180 | { 181 | var factoryData = 182 | ""; 183 | _target.Init(factoryData); 184 | _configurator.Verify(mock => mock.Configure(It.IsAny(), It.IsAny()), 185 | Times.Once); 186 | } 187 | 188 | /// 189 | ///A test for Init 190 | /// 191 | [Test] 192 | public void Log4netLoggerFactory_Init_ValidXml_MultiConfig_Missing_Test() 193 | { 194 | var factoryData = 195 | ""; 196 | _target.Init(factoryData); 197 | _configurator.Verify(mock => mock.Configure(It.IsAny(), It.IsAny()), 198 | Times.Once); 199 | } 200 | 201 | private class TestLog4netLoggerFactory : Log4netLoggerFactory 202 | { 203 | public TestLog4netLoggerFactory(IXmlConfigurator configurator) : base(configurator) 204 | { 205 | } 206 | 207 | public ILogger CreateLogger_ForTest(string name) 208 | { 209 | return base.CreateLogger(name); 210 | } 211 | } 212 | } 213 | } -------------------------------------------------------------------------------- /src/Tests/slf4net.log4net.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Reflection; 25 | using System.Runtime.CompilerServices; 26 | using System.Runtime.InteropServices; 27 | 28 | // General Information about an assembly is controlled through the following 29 | // set of attributes. Change these attribute values to modify the information 30 | // associated with an assembly. 31 | [assembly: AssemblyTitle("slf4net.log4net.Tests")] 32 | [assembly: AssemblyDescription("")] 33 | [assembly: AssemblyConfiguration("")] 34 | [assembly: AssemblyCompany("EF Education First")] 35 | [assembly: AssemblyProduct("slf4net.log4net.Tests")] 36 | [assembly: AssemblyCopyright("Copyright © 2020 EF Learning Labs")] 37 | [assembly: AssemblyTrademark("")] 38 | [assembly: AssemblyCulture("")] 39 | 40 | // Setting ComVisible to false makes the types in this assembly not visible 41 | // to COM components. If you need to access a type in this assembly from 42 | // COM, set the ComVisible attribute to true on that type. 43 | [assembly: ComVisible(false)] 44 | 45 | // The following GUID is for the ID of the typelib if this project is exposed to COM 46 | [assembly: Guid("bb0aa619-1dbb-49c2-adb8-6899e42d71d6")] 47 | 48 | // Version information for an assembly consists of the following four values: 49 | // 50 | // Major Version 51 | // Minor Version 52 | // Build Number 53 | // Revision 54 | // 55 | // You can specify all the values or you can default the Build and Revision Numbers 56 | // by using the '*' as shown below: 57 | [assembly: AssemblyVersion("1.0.0.0")] 58 | [assembly: AssemblyFileVersion("1.0.0.0")] 59 | -------------------------------------------------------------------------------- /src/Tests/slf4net.log4net.Tests/log4net.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/Tests/slf4net.log4net.Tests/log4net.extra.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/Tests/slf4net.log4net.Tests/slf4net.log4net.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 2.0 7 | {90B6EE59-950A-4E48-9235-F2A16A6B5AFE} 8 | Library 9 | Properties 10 | slf4net.Tests 11 | slf4net.log4net.Tests 12 | v4.8 13 | 512 14 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | ..\..\ 16 | true 17 | 18 | 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | true 37 | 38 | 39 | slf4net.snk 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | ExploratoryTester.cs 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | {67C30AFE-0952-4C45-B1BA-38EA1CBA6B42} 58 | slf4net.log4net 59 | 60 | 61 | {F969A551-EE0E-4129-9E88-AC5291985589} 62 | slf4net 63 | 64 | 65 | 66 | 67 | Always 68 | 69 | 70 | Always 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /src/Tests/slf4net.log4net.Tests/slf4net.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/Tests/slf4net.log4net.Tests/slf4net.snk -------------------------------------------------------------------------------- /src/slf4net.NLog/NLogLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using NLog; 25 | using slf4net.Factories; 26 | 27 | namespace slf4net.NLog 28 | { 29 | /// 30 | /// An implementation of the 31 | /// interface which creates instances 32 | /// that use the NLog framework as the underlying logging 33 | /// mechanism. 34 | /// 35 | public class NLogLoggerFactory : NamedLoggerFactoryBase, ISlf4netServiceProvider 36 | { 37 | private readonly NLogMdcAdapter _mdcAdapter = new NLogMdcAdapter(); 38 | 39 | /// 40 | protected override ILogger CreateLogger(string name) 41 | { 42 | var nlogLogger = LogManager.GetLogger(name); 43 | return new NLogLoggerAdapter(nlogLogger); 44 | } 45 | 46 | /// 47 | public ILoggerFactory GetLoggerFactory() 48 | { 49 | return this; 50 | } 51 | 52 | /// 53 | public IMdcAdapter GetMdcAdapter() 54 | { 55 | return _mdcAdapter; 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /src/slf4net.NLog/NLogMdcAdapter.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using NLog; 24 | 25 | namespace slf4net.NLog 26 | { 27 | /// 28 | /// The NLog implementation of 29 | /// 30 | public class NLogMdcAdapter : IMdcAdapter 31 | { 32 | /// 33 | public object Get(string key) 34 | { 35 | return MappedDiagnosticsContext.Get(key); 36 | } 37 | 38 | /// 39 | public void Set(string key, object value) 40 | { 41 | MappedDiagnosticsContext.Set(key, value); 42 | } 43 | 44 | /// 45 | public void Remove(string key) 46 | { 47 | MappedDiagnosticsContext.Remove(key); 48 | } 49 | 50 | /// 51 | public void Clear() 52 | { 53 | MappedDiagnosticsContext.Clear(); 54 | } 55 | 56 | /// 57 | public object LogicalGet(string key) 58 | { 59 | return MappedDiagnosticsLogicalContext.Get(key); 60 | } 61 | 62 | /// 63 | public void LogicalSet(string key, object value) 64 | { 65 | MappedDiagnosticsLogicalContext.Set(key, value); 66 | } 67 | 68 | /// 69 | public void LogicalRemove(string key) 70 | { 71 | MappedDiagnosticsLogicalContext.Remove(key); 72 | } 73 | 74 | /// 75 | public void LogicalClear() 76 | { 77 | MappedDiagnosticsLogicalContext.Clear(); 78 | } 79 | 80 | /// 81 | public object GlobalGet(string key) 82 | { 83 | return GlobalDiagnosticsContext.Get(key); 84 | } 85 | 86 | /// 87 | public void GlobalSet(string key, object value) 88 | { 89 | GlobalDiagnosticsContext.Set(key, value); 90 | } 91 | 92 | /// 93 | public void GlobalRemove(string key) 94 | { 95 | GlobalDiagnosticsContext.Remove(key); 96 | } 97 | 98 | /// 99 | public void GlobalClear() 100 | { 101 | GlobalDiagnosticsContext.Clear(); 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /src/slf4net.NLog/app.config.transform: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/slf4net.NLog/slf4net.NLog.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net40;netstandard2.0 5 | slf4net facade for the NLog logging framework 6 | 7 | 8 | 9 | bin\Debug\slf4net.NLog.xml 10 | 11 | 12 | 13 | bin\Release\slf4net.NLog.xml 14 | true 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/slf4net.NLog/slf4net.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/slf4net.NLog/slf4net.snk -------------------------------------------------------------------------------- /src/slf4net.NLog/web.config.transform: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/slf4net.log4net/Internal/IXmlConfigurator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.IO; 3 | using System.Xml; 4 | using log4net.Repository; 5 | 6 | namespace slf4net.log4net.Internal 7 | { 8 | /// 9 | /// Wrapper for slf4net XmlConfigurator 10 | /// 11 | public interface IXmlConfigurator 12 | { 13 | /// 14 | /// 15 | /// 16 | /// 17 | /// 18 | ICollection Configure(ILoggerRepository repository); 19 | 20 | /// 21 | /// 22 | /// 23 | /// 24 | /// 25 | /// 26 | ICollection Configure(ILoggerRepository repository, XmlElement element); 27 | 28 | /// 29 | /// 30 | /// 31 | /// 32 | /// 33 | /// 34 | ICollection Configure(ILoggerRepository repository, FileInfo configFile); 35 | 36 | /// 37 | /// 38 | /// 39 | /// 40 | /// 41 | /// 42 | ICollection ConfigureAndWatch(ILoggerRepository repository, FileInfo configFile); 43 | } 44 | } -------------------------------------------------------------------------------- /src/slf4net.log4net/Internal/XmlConfiguratorWrapper.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.IO; 3 | using System.Xml; 4 | using log4net.Config; 5 | using log4net.Repository; 6 | 7 | namespace slf4net.log4net.Internal 8 | { 9 | internal class XmlConfiguratorWrapper : IXmlConfigurator 10 | { 11 | public ICollection Configure(ILoggerRepository repository) 12 | { 13 | return XmlConfigurator.Configure(repository); 14 | } 15 | 16 | public ICollection Configure(ILoggerRepository repository, XmlElement element) 17 | { 18 | return XmlConfigurator.Configure(repository, element); 19 | } 20 | 21 | public ICollection Configure(ILoggerRepository repository, FileInfo configFile) 22 | { 23 | return XmlConfigurator.Configure(repository, configFile); 24 | } 25 | 26 | public ICollection ConfigureAndWatch(ILoggerRepository repository, FileInfo configFile) 27 | { 28 | return XmlConfigurator.ConfigureAndWatch(repository, configFile); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/slf4net.log4net/Log4netLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using log4net; 25 | using slf4net.Factories; 26 | using slf4net.log4net.Internal; 27 | 28 | namespace slf4net.log4net 29 | { 30 | /// 31 | /// An implementation of the 32 | /// interface which creates instances 33 | /// that use the log4net framework as the underlying logging 34 | /// mechanism. 35 | /// 36 | public class Log4netLoggerFactory : NamedLoggerFactoryBase, IConfigurableLoggerFactory, ISlf4netServiceProvider 37 | { 38 | private readonly IXmlConfigurator _configurator; 39 | private readonly Log4netMdcAdapter _mdcAdapter = new Log4netMdcAdapter(); 40 | private static bool _isInitialized; 41 | private static readonly object Locker = new object(); 42 | 43 | /// 44 | /// The slf4net repository name 45 | /// 46 | public static readonly string SLF4NET_REPOSITORY = "slf4net-repository"; 47 | 48 | /// 49 | /// Default constructor 50 | /// 51 | public Log4netLoggerFactory() : this(new XmlConfiguratorWrapper()) 52 | { 53 | } 54 | 55 | /// 56 | /// Injection constructor 57 | /// 58 | /// 59 | public Log4netLoggerFactory(IXmlConfigurator configurator) 60 | { 61 | _configurator = configurator; 62 | } 63 | 64 | /// 65 | protected override ILogger CreateLogger(string name) 66 | { 67 | EnsureInitialized(); 68 | var log4netLogger = LogManager.GetLogger(SLF4NET_REPOSITORY, name); 69 | return new Log4netLoggerAdapter(log4netLogger); 70 | } 71 | 72 | /// 73 | public void Init(string factoryData) 74 | { 75 | lock (Locker) 76 | { 77 | var helper = new XmlConfiguratorHelper(factoryData, _configurator); 78 | helper.Configure(); 79 | _isInitialized = true; 80 | } 81 | } 82 | 83 | private void EnsureInitialized() 84 | { 85 | if (!_isInitialized) 86 | { 87 | lock (Locker) 88 | { 89 | if (!_isInitialized) 90 | { 91 | Init(null); 92 | } 93 | } 94 | } 95 | } 96 | 97 | /// 98 | public ILoggerFactory GetLoggerFactory() 99 | { 100 | return this; 101 | } 102 | 103 | /// 104 | public IMdcAdapter GetMdcAdapter() 105 | { 106 | return _mdcAdapter; 107 | } 108 | } 109 | } -------------------------------------------------------------------------------- /src/slf4net.log4net/Log4netMdcAdapter.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using log4net; 24 | 25 | namespace slf4net.log4net 26 | { 27 | /// 28 | /// The log4net implementation of 29 | /// 30 | public class Log4netMdcAdapter : IMdcAdapter 31 | { 32 | /// 33 | public object Get(string key) 34 | { 35 | return ThreadContext.Properties[key]; 36 | } 37 | 38 | /// 39 | public void Set(string key, object value) 40 | { 41 | ThreadContext.Properties[key] = value; 42 | } 43 | 44 | /// 45 | public void Remove(string key) 46 | { 47 | ThreadContext.Properties.Remove(key); 48 | } 49 | 50 | /// 51 | public void Clear() 52 | { 53 | ThreadContext.Properties.Clear(); 54 | } 55 | 56 | /// 57 | public object LogicalGet(string key) 58 | { 59 | return LogicalThreadContext.Properties[key]; 60 | } 61 | 62 | /// 63 | public void LogicalSet(string key, object value) 64 | { 65 | LogicalThreadContext.Properties[key] = value; 66 | } 67 | 68 | /// 69 | public void LogicalRemove(string key) 70 | { 71 | LogicalThreadContext.Properties.Remove(key); 72 | } 73 | 74 | /// 75 | public void LogicalClear() 76 | { 77 | LogicalThreadContext.Properties.Clear(); 78 | } 79 | 80 | /// 81 | public object GlobalGet(string key) 82 | { 83 | return GlobalContext.Properties[key]; 84 | } 85 | 86 | /// 87 | public void GlobalSet(string key, object value) 88 | { 89 | GlobalContext.Properties[key] = value; 90 | } 91 | 92 | /// 93 | public void GlobalRemove(string key) 94 | { 95 | GlobalContext.Properties.Remove(key); 96 | } 97 | 98 | /// 99 | public void GlobalClear() 100 | { 101 | GlobalContext.Properties.Clear(); 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /src/slf4net.log4net/XmlConfiguratorHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Xml; 5 | using System.Xml.Linq; 6 | using log4net.Core; 7 | using log4net.Repository; 8 | using slf4net.Internal; 9 | using slf4net.log4net.Internal; 10 | 11 | namespace slf4net.log4net 12 | { 13 | internal class XmlConfiguratorHelper 14 | { 15 | private const string CONFIG_FILE = "configFile"; 16 | private const string WATCH = "watch"; 17 | private const string VALUE = "value"; 18 | 19 | private readonly IXmlConfigurator _configurator; 20 | private readonly IList _configFiles = new List(); 21 | private bool _watch; 22 | 23 | private static readonly ILoggerRepository _repository = 24 | LoggerManager.CreateRepository(Log4netLoggerFactory.SLF4NET_REPOSITORY); 25 | 26 | 27 | public XmlConfiguratorHelper(string factoryData, IXmlConfigurator configurator) 28 | { 29 | _configurator = configurator; 30 | if (string.IsNullOrEmpty(factoryData)) 31 | { 32 | return; 33 | } 34 | 35 | var settings = new XmlReaderSettings() 36 | { 37 | ConformanceLevel = ConformanceLevel.Auto, 38 | IgnoreComments = true, 39 | IgnoreWhitespace = true, 40 | }; 41 | 42 | using (var sr = new StringReader(factoryData)) 43 | using (var xr = XmlReader.Create(sr, settings)) 44 | { 45 | while (xr.Read()) 46 | { 47 | if (xr.NodeType == XmlNodeType.Element) 48 | { 49 | switch (xr.Name) 50 | { 51 | case CONFIG_FILE: 52 | { 53 | if (xr.MoveToAttribute(VALUE) && !string.IsNullOrEmpty(xr.Value)) 54 | { 55 | _configFiles.Add(xr.Value); 56 | } 57 | 58 | break; 59 | } 60 | case WATCH: 61 | { 62 | if (xr.MoveToAttribute(VALUE)) 63 | { 64 | if (bool.TryParse(xr.Value, out var value)) 65 | { 66 | _watch = value; 67 | } 68 | } 69 | 70 | break; 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | 78 | public void Configure() 79 | { 80 | var files = new List(); 81 | 82 | foreach (var configFile in _configFiles) 83 | { 84 | var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile); 85 | var file = new FileInfo(fileName); 86 | 87 | if (file.Exists) 88 | { 89 | files.Add(file); 90 | } 91 | else 92 | { 93 | ConsoleHelper.WriteLine($"log4net config file {configFile} does not exist"); 94 | } 95 | } 96 | 97 | switch (files.Count) 98 | { 99 | case 0: 100 | _configurator.Configure(_repository); 101 | break; 102 | case 1: 103 | { 104 | ConfigureOne(files[0]); 105 | break; 106 | } 107 | default: 108 | { 109 | ConfigureMulti(files); 110 | break; 111 | } 112 | } 113 | } 114 | 115 | private void ConfigureOne(FileInfo file) 116 | { 117 | if (_watch) 118 | { 119 | _configurator.ConfigureAndWatch(_repository, file); 120 | } 121 | else 122 | { 123 | _configurator.Configure(_repository, file); 124 | } 125 | } 126 | 127 | private void ConfigureMulti(IEnumerable files) 128 | { 129 | // Need to combine multiple files 130 | XElement root = null; 131 | var name = "log4net"; 132 | 133 | foreach (var file in files) 134 | { 135 | var doc = XDocument.Load(file.FullName); 136 | var element = doc.Element(name); 137 | 138 | if (element == null) 139 | { 140 | ConsoleHelper.WriteLine($"log4net config file {file.FullName} missing root {name} element"); 141 | continue; 142 | } 143 | 144 | if (root == null) 145 | { 146 | root = element; 147 | } 148 | else 149 | { 150 | root.Add(element.Nodes()); 151 | } 152 | } 153 | 154 | if (root == null) 155 | { 156 | _configurator.Configure(_repository); 157 | } 158 | else 159 | { 160 | XmlDocument doc = new XmlDocument(); 161 | XmlElement node = doc.ReadNode(root.CreateReader()) as XmlElement; 162 | _configurator.Configure(_repository, node); 163 | } 164 | } 165 | } 166 | } -------------------------------------------------------------------------------- /src/slf4net.log4net/app.config.transform: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/slf4net.log4net/slf4net.log4net.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net40;netstandard2.0 5 | slf4net facade for the log4net logging framework 6 | 7 | 8 | 9 | bin\Debug\slf4net.log4net.xml 10 | 11 | 12 | 13 | true 14 | bin\Release\slf4net.log4net.xml 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/slf4net.log4net/slf4net.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/slf4net.log4net/slf4net.snk -------------------------------------------------------------------------------- /src/slf4net.log4net/web.config.transform: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/slf4net.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B90B0A41-FF2A-4B90-A530-39AB7D0D456C}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "slf4net", "slf4net\slf4net.csproj", "{F969A551-EE0E-4129-9E88-AC5291985589}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "slf4net.Moqs", "Tests\slf4net.Moqs\slf4net.Moqs.csproj", "{6554750C-D44D-47C9-81A6-9C0353C91E81}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "slf4net.Tests", "Tests\slf4net.Tests\slf4net.Tests.csproj", "{2AECA60D-4A10-433C-881A-ADE69E2A3DA1}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "slf4net.log4net", "slf4net.log4net\slf4net.log4net.csproj", "{67C30AFE-0952-4C45-B1BA-38EA1CBA6B42}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "slf4net.NLog", "slf4net.NLog\slf4net.NLog.csproj", "{AB8A3AB3-06C9-4D74-BE75-3CC3E72287A4}" 15 | EndProject 16 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "slf4net.log4net.Tests", "Tests\slf4net.log4net.Tests\slf4net.log4net.Tests.csproj", "{90B6EE59-950A-4E48-9235-F2A16A6B5AFE}" 17 | EndProject 18 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "slf4net.NLog.Tests", "Tests\slf4net.NLog.Tests\slf4net.NLog.Tests.csproj", "{1ABB0B2B-1E3F-4BA4-8AD7-4763A16D8E00}" 19 | EndProject 20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4D73B518-5798-4A3E-BB3C-63ADF6836943}" 21 | ProjectSection(SolutionItems) = preProject 22 | ..\.gitignore = ..\.gitignore 23 | ..\.travis.yml = ..\.travis.yml 24 | ..\nuget-push.sh = ..\nuget-push.sh 25 | ..\README.md = ..\README.md 26 | slf4net.targets = slf4net.targets 27 | ..\LICENSE = ..\LICENSE 28 | EndProjectSection 29 | EndProject 30 | Global 31 | GlobalSection(TestCaseManagementSettings) = postSolution 32 | CategoryFile = slf4net.vsmdi 33 | EndGlobalSection 34 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 35 | Debug|Any CPU = Debug|Any CPU 36 | Release|Any CPU = Release|Any CPU 37 | EndGlobalSection 38 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 39 | {F969A551-EE0E-4129-9E88-AC5291985589}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {F969A551-EE0E-4129-9E88-AC5291985589}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {F969A551-EE0E-4129-9E88-AC5291985589}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {F969A551-EE0E-4129-9E88-AC5291985589}.Release|Any CPU.Build.0 = Release|Any CPU 43 | {6554750C-D44D-47C9-81A6-9C0353C91E81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {6554750C-D44D-47C9-81A6-9C0353C91E81}.Debug|Any CPU.Build.0 = Debug|Any CPU 45 | {6554750C-D44D-47C9-81A6-9C0353C91E81}.Release|Any CPU.ActiveCfg = Release|Any CPU 46 | {6554750C-D44D-47C9-81A6-9C0353C91E81}.Release|Any CPU.Build.0 = Release|Any CPU 47 | {2AECA60D-4A10-433C-881A-ADE69E2A3DA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 48 | {2AECA60D-4A10-433C-881A-ADE69E2A3DA1}.Debug|Any CPU.Build.0 = Debug|Any CPU 49 | {2AECA60D-4A10-433C-881A-ADE69E2A3DA1}.Release|Any CPU.ActiveCfg = Release|Any CPU 50 | {2AECA60D-4A10-433C-881A-ADE69E2A3DA1}.Release|Any CPU.Build.0 = Release|Any CPU 51 | {67C30AFE-0952-4C45-B1BA-38EA1CBA6B42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 52 | {67C30AFE-0952-4C45-B1BA-38EA1CBA6B42}.Debug|Any CPU.Build.0 = Debug|Any CPU 53 | {67C30AFE-0952-4C45-B1BA-38EA1CBA6B42}.Release|Any CPU.ActiveCfg = Release|Any CPU 54 | {67C30AFE-0952-4C45-B1BA-38EA1CBA6B42}.Release|Any CPU.Build.0 = Release|Any CPU 55 | {AB8A3AB3-06C9-4D74-BE75-3CC3E72287A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 56 | {AB8A3AB3-06C9-4D74-BE75-3CC3E72287A4}.Debug|Any CPU.Build.0 = Debug|Any CPU 57 | {AB8A3AB3-06C9-4D74-BE75-3CC3E72287A4}.Release|Any CPU.ActiveCfg = Release|Any CPU 58 | {AB8A3AB3-06C9-4D74-BE75-3CC3E72287A4}.Release|Any CPU.Build.0 = Release|Any CPU 59 | {90B6EE59-950A-4E48-9235-F2A16A6B5AFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 60 | {90B6EE59-950A-4E48-9235-F2A16A6B5AFE}.Debug|Any CPU.Build.0 = Debug|Any CPU 61 | {90B6EE59-950A-4E48-9235-F2A16A6B5AFE}.Release|Any CPU.ActiveCfg = Release|Any CPU 62 | {90B6EE59-950A-4E48-9235-F2A16A6B5AFE}.Release|Any CPU.Build.0 = Release|Any CPU 63 | {1ABB0B2B-1E3F-4BA4-8AD7-4763A16D8E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 64 | {1ABB0B2B-1E3F-4BA4-8AD7-4763A16D8E00}.Debug|Any CPU.Build.0 = Debug|Any CPU 65 | {1ABB0B2B-1E3F-4BA4-8AD7-4763A16D8E00}.Release|Any CPU.ActiveCfg = Release|Any CPU 66 | {1ABB0B2B-1E3F-4BA4-8AD7-4763A16D8E00}.Release|Any CPU.Build.0 = Release|Any CPU 67 | EndGlobalSection 68 | GlobalSection(SolutionProperties) = preSolution 69 | HideSolutionNode = FALSE 70 | EndGlobalSection 71 | GlobalSection(NestedProjects) = preSolution 72 | {6554750C-D44D-47C9-81A6-9C0353C91E81} = {B90B0A41-FF2A-4B90-A530-39AB7D0D456C} 73 | {2AECA60D-4A10-433C-881A-ADE69E2A3DA1} = {B90B0A41-FF2A-4B90-A530-39AB7D0D456C} 74 | {90B6EE59-950A-4E48-9235-F2A16A6B5AFE} = {B90B0A41-FF2A-4B90-A530-39AB7D0D456C} 75 | {1ABB0B2B-1E3F-4BA4-8AD7-4763A16D8E00} = {B90B0A41-FF2A-4B90-A530-39AB7D0D456C} 76 | EndGlobalSection 77 | EndGlobal 78 | -------------------------------------------------------------------------------- /src/slf4net.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | EF Education First 5 | https://github.com/ef-labs/slf4net 6 | MIT 7 | Copyright © 2020 EF Learning Labs 8 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 9 | 10 | true 11 | slf4net.snk 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/slf4net/ActivatorUtils.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System; 25 | 26 | namespace slf4net 27 | { 28 | /// 29 | /// Provides helper methods that supplement the 30 | /// . 31 | /// 32 | internal static class ActivatorUtils 33 | { 34 | /// 35 | /// Instantiates an object of the given type. 36 | /// 37 | /// The type being instantiated 38 | /// The full type name 39 | /// 40 | internal static T Instantiate(string typeName) where T : class 41 | { 42 | Type type = Type.GetType(typeName, throwOnError: true, ignoreCase: false); 43 | T obj = Activator.CreateInstance(type) as T; 44 | 45 | if (obj == null) 46 | { 47 | var msg = string.Format("The item [type={0}], is not of type {1}.", type, typeof(T)); 48 | throw new InvalidCastException(msg); 49 | } 50 | 51 | return obj; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/slf4net/Configuration/FactoryConfigurationElement.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Configuration; 25 | using System.Xml; 26 | 27 | namespace slf4net.Configuration 28 | { 29 | /// 30 | /// The factory element within the application 31 | /// configuration. 32 | /// 33 | public class FactoryConfigurationElement : ConfigurationElement 34 | { 35 | 36 | private const string TYPE_ATTRIBUTE = "type"; 37 | private const string FACTORY_DATA_ELEMENT = "factory-data"; 38 | 39 | /// 40 | /// Gets custom factory data, if available. This is data of the factory 41 | /// element, that is wrapped into an element named factory-data. 42 | /// The contents of the factory-data element can be as simple as 43 | /// a literal, or a complex XML element. 44 | /// Defaults to null. 45 | /// 46 | /// 47 | /// The following example shows two possible configurations - one with 48 | /// a literal as custom configuration data, one with XML based configuration: 49 | /// 50 | /// <slf> 51 | /// <!-- a factory configuration with literal content --> 52 | /// <factory name="simple"> 53 | /// <factory-data> 54 | /// simple data 55 | /// </factory-data> 56 | /// </factory> 57 | /// 58 | /// <!-- a factory configuration with XML sub elements --> 59 | /// <factory name="complex"> 60 | /// <factory-data> 61 | /// <child foo="bar"> 62 | /// <subchild>foobar</subchild> 63 | /// </child> 64 | /// </factory-data> 65 | /// </factory> 66 | /// </slf> 67 | /// 68 | /// 69 | public string FactoryData { get; set; } 70 | 71 | /// 72 | /// Indicates the factory Type 73 | /// 74 | [ConfigurationProperty(TYPE_ATTRIBUTE, IsRequired = false)] 75 | public string Type 76 | { 77 | get 78 | { 79 | return (string)this[TYPE_ATTRIBUTE]; 80 | } 81 | set 82 | { 83 | this[TYPE_ATTRIBUTE] = value; 84 | } 85 | } 86 | 87 | 88 | /// 89 | /// Parses custom factory data into the property, 90 | /// if refers to a factory-data element. 91 | /// 92 | /// 93 | /// 94 | /// True if the element is a (supported) factory-data element. 95 | /// If it's an other unexpected element, this method returns false. 96 | protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader) 97 | { 98 | if (elementName != FACTORY_DATA_ELEMENT) 99 | { 100 | //we really don't know that element - delegate to base class (just returns false) 101 | return base.OnDeserializeUnrecognizedElement(elementName, reader); 102 | } 103 | 104 | //parse the contents of the element 105 | //(the string does not contain the "factory-data" element markup) 106 | FactoryData = reader.ReadInnerXml().Trim(); 107 | return true; 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/slf4net/Configuration/SlfConfigurationSection.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Configuration; 25 | 26 | namespace slf4net.Configuration 27 | { 28 | /// 29 | /// The slf4net configuration section to define which type to use. 30 | /// 31 | public class SlfConfigurationSection : ConfigurationSection 32 | { 33 | 34 | private const string FactoryElementName = "factory"; 35 | 36 | /// 37 | /// Defines the to use. 38 | /// 39 | [ConfigurationProperty(FactoryElementName)] 40 | public FactoryConfigurationElement Factory 41 | { 42 | get { return (FactoryConfigurationElement)this[FactoryElementName]; } 43 | set { this[FactoryElementName] = value; } 44 | } 45 | 46 | } 47 | } -------------------------------------------------------------------------------- /src/slf4net/Ensure.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System; 25 | 26 | namespace slf4net 27 | { 28 | /// 29 | /// Provides common runtime validation functionality. 30 | /// 31 | internal static class Ensure 32 | { 33 | /// 34 | /// Makes sure a given argument is not null. 35 | /// 36 | /// Type of the argument. 37 | /// The submitted parameter value. 38 | /// The name of the argument. 39 | /// If 40 | /// is a null reference. 41 | public static void ArgumentNotNull(T argument, string argumentName) where T : class 42 | { 43 | if (argument == null) throw new ArgumentNullException(argumentName); 44 | } 45 | 46 | } 47 | } -------------------------------------------------------------------------------- /src/slf4net/Factories/Internal/SubstituteLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Collections.Generic; 25 | using System.Linq; 26 | 27 | namespace slf4net.Factories.Internal 28 | { 29 | /// 30 | /// A temporary logger factory used during initialization 31 | /// 32 | public class SubstituteLoggerFactory : ILoggerFactory 33 | { 34 | 35 | private IList _loggerNameList = new List(); 36 | 37 | /// 38 | public ILogger GetLogger(string name) 39 | { 40 | lock (_loggerNameList) 41 | { 42 | _loggerNameList.Add(name); 43 | } 44 | 45 | return NOPLogger.Instance; 46 | } 47 | 48 | /// 49 | /// 50 | /// 51 | /// 52 | public string[] GetLoggerNameList() 53 | { 54 | lock (_loggerNameList) 55 | { 56 | return _loggerNameList.ToArray(); 57 | } 58 | } 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/slf4net/Factories/NOPLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | namespace slf4net.Factories 25 | { 26 | /// 27 | /// A no operation implementation of the 28 | /// which simply returns a instance. 29 | /// 30 | public class NOPLoggerFactory : ILoggerFactory 31 | { 32 | 33 | #region Singleton 34 | 35 | /// 36 | /// Singleton instance. 37 | /// 38 | private static readonly NOPLoggerFactory _instance = new NOPLoggerFactory(); 39 | 40 | /// 41 | /// Provides access to the singleton instance of 42 | /// the class. 43 | /// 44 | public static NOPLoggerFactory Instance 45 | { 46 | get { return _instance; } 47 | } 48 | 49 | /// 50 | /// Private constructor. A reference to the Singleton 51 | /// instance of this class is available through the 52 | /// static property. 53 | /// 54 | private NOPLoggerFactory() 55 | { 56 | } 57 | 58 | #endregion 59 | 60 | #region ILoggerFactory Implementation 61 | 62 | /// 63 | /// Obtains an instance that is identified by 64 | /// the given name. 65 | /// 66 | /// The logger name. 67 | /// An instance 68 | public ILogger GetLogger(string name) 69 | { 70 | return NOPLogger.Instance; 71 | } 72 | 73 | #endregion 74 | 75 | } 76 | } -------------------------------------------------------------------------------- /src/slf4net/Factories/NamedLoggerFactoryBase.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Collections.Generic; 25 | using System.Globalization; 26 | 27 | namespace slf4net.Factories 28 | { 29 | /// 30 | /// A base class for factories which created named logger 31 | /// instance. 32 | /// 33 | public abstract class NamedLoggerFactoryBase : ILoggerFactory 34 | { 35 | /// 36 | /// A cache of named loggers 37 | /// 38 | private readonly Dictionary _loggers = new Dictionary(); 39 | /// 40 | /// An object which is used for locking 41 | /// 42 | private readonly object _lockObj = new object(); 43 | 44 | /// 45 | /// Obtains an instance that is identified by 46 | /// the given name. 47 | /// 48 | /// The logger name. 49 | /// A factory that can be identified by the 50 | /// given name in the target output for this logger 51 | public ILogger GetLogger(string name) 52 | { 53 | if (name == null) name = string.Empty; 54 | string lowerName = name.ToLower(CultureInfo.InvariantCulture); 55 | 56 | ILogger logger; 57 | 58 | if (!_loggers.TryGetValue(lowerName, out logger)) 59 | { 60 | lock (_lockObj) 61 | { 62 | if (!_loggers.TryGetValue(lowerName, out logger)) 63 | { 64 | logger = CreateLogger(name); 65 | _loggers.Add(lowerName, logger); 66 | } 67 | } 68 | } 69 | 70 | return logger; 71 | } 72 | 73 | /// 74 | /// Constructs a logger with the given name 75 | /// 76 | /// The logger name. 77 | /// A logger with the given name. 78 | protected abstract ILogger CreateLogger(string name); 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/slf4net/GDC.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | namespace slf4net 24 | { 25 | /// 26 | /// A facade for the underlying logging framework's Global Diagnostic Context implementation. 27 | /// 28 | public static class GDC 29 | { 30 | /// 31 | /// Get the diagnostic context identified by the key parameter from the global diagnostic context map. 32 | /// 33 | /// The property key 34 | /// 35 | public static object Get(string key) 36 | { 37 | return MDC.GetMdcAdapter().GlobalGet(key); 38 | } 39 | 40 | /// 41 | /// Set a diagnostic context value (the val parameter) as identified with the key parameter into the global diagnostic context map. 42 | /// 43 | /// The property key 44 | /// The property value 45 | public static void Set(string key, object value) 46 | { 47 | MDC.GetMdcAdapter().GlobalSet(key, value); 48 | } 49 | 50 | /// 51 | /// Remove the diagnostic context identified by the key parameter from the global diagnostic context map. 52 | /// 53 | /// The property key 54 | public static void Remove(string key) 55 | { 56 | MDC.GetMdcAdapter().GlobalRemove(key); 57 | } 58 | 59 | /// 60 | /// Clear all entries in the global diagnostic context map. 61 | /// 62 | public static void Clear() 63 | { 64 | MDC.GetMdcAdapter().GlobalClear(); 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /src/slf4net/GetPublicKey.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | SET sn="C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\sn.exe" 3 | %sn% -p slf4net.snk slf4net.PublicKey 4 | %sn% -tp slf4net.PublicKey 5 | PAUSE -------------------------------------------------------------------------------- /src/slf4net/IConfigurableLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | namespace slf4net 25 | { 26 | /// 27 | /// Common interface for logger factories that can 28 | /// be initialized with custom configuration data. 29 | /// 30 | public interface IConfigurableLoggerFactory : ILoggerFactory 31 | { 32 | /// 33 | /// Inits the plug-in with configured factory data. 34 | /// 35 | /// Retrieved factory settings. 36 | /// This parameter is null if no configuration at all 37 | /// was found. 38 | void Init(string factoryData); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/slf4net/IFactoryResolver.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | namespace slf4net 25 | { 26 | /// 27 | /// Responsible for finding and creating 28 | /// instances which are being used to create loggers of a given implementation. 29 | /// 30 | public interface IFactoryResolver 31 | { 32 | /// 33 | /// Determines a factory which in turn creates an 34 | /// instance based on a 35 | /// request for a named logger. 36 | /// 37 | /// A factory which in turn is responsible for creating 38 | /// a given implementation. 39 | ILoggerFactory GetFactory(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/slf4net/ILoggerFactory.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | namespace slf4net 25 | { 26 | /// 27 | /// instances manufacture 28 | /// instances by name. These factory methods may create new instances 29 | /// or retrieve cached / pooled instances depending on the the 30 | /// name of the requested logger. 31 | /// 32 | public interface ILoggerFactory 33 | { 34 | /// 35 | /// Returns an appropriate instance as specified by the name parameter. 36 | /// 37 | /// The name of the logger to return. 38 | ILogger GetLogger(string name); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/slf4net/IMdcAdapter.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | namespace slf4net 24 | { 25 | /// 26 | /// An adapter for the underlying logging framework's MDC implementation 27 | /// 28 | public interface IMdcAdapter 29 | { 30 | /// 31 | /// Get the diagnostic context identified by the key parameter from the current thread's diagnostic context map. 32 | /// 33 | /// The property key 34 | /// 35 | object Get(string key); 36 | 37 | /// 38 | /// Set a diagnostic context value (the val parameter) as identified with the key parameter into the current thread's diagnostic context map. 39 | /// 40 | /// The property key 41 | /// The property value 42 | void Set(string key, object value); 43 | 44 | /// 45 | /// Remove the diagnostic context identified by the key parameter from the current thread's diagnostic context map. 46 | /// 47 | /// The property key 48 | void Remove(string key); 49 | 50 | /// 51 | /// Clear all entries in the current thread's diagnostic context map. 52 | /// 53 | void Clear(); 54 | 55 | /// 56 | /// Get the diagnostic context identified by the key parameter from the logical diagnostic context map. 57 | /// 58 | /// The property key 59 | /// 60 | object LogicalGet(string key); 61 | 62 | /// 63 | /// Set a diagnostic context value (the val parameter) as identified with the key parameter into the logical diagnostic context map. 64 | /// 65 | /// The property key 66 | /// The property value 67 | void LogicalSet(string key, object value); 68 | 69 | /// 70 | /// Remove the diagnostic context identified by the key parameter from the logical diagnostic context map. 71 | /// 72 | /// The property key 73 | void LogicalRemove(string key); 74 | 75 | /// 76 | /// Clear all entries in the logical diagnostic context map. 77 | /// 78 | void LogicalClear(); 79 | 80 | /// 81 | /// Get the diagnostic context identified by the key parameter from the global diagnostic context map. 82 | /// 83 | /// The property key 84 | /// 85 | object GlobalGet(string key); 86 | 87 | /// 88 | /// Set a diagnostic context value (the val parameter) as identified with the key parameter into the global diagnostic context map. 89 | /// 90 | /// The property key 91 | /// The property value 92 | void GlobalSet(string key, object value); 93 | 94 | /// 95 | /// Remove the diagnostic context identified by the key parameter from the global diagnostic context map. 96 | /// 97 | /// The property key 98 | void GlobalRemove(string key); 99 | 100 | /// 101 | /// Clear all entries in the global diagnostic context map. 102 | /// 103 | void GlobalClear(); 104 | } 105 | } -------------------------------------------------------------------------------- /src/slf4net/ISlf4netServiceProvider.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | namespace slf4net 24 | { 25 | /// 26 | /// Provides the implementing slf4net service 27 | /// 28 | public interface ISlf4netServiceProvider 29 | { 30 | /// 31 | /// Returns a factory which in turn creates an instance 32 | /// 33 | /// A factory which in turn is responsible for creating a given implementation. 34 | ILoggerFactory GetLoggerFactory(); 35 | 36 | /// 37 | /// Returns an MDC adapter for working with mapped diagnostic context properties 38 | /// 39 | /// An MDC adapter 40 | IMdcAdapter GetMdcAdapter(); 41 | } 42 | } -------------------------------------------------------------------------------- /src/slf4net/ISlf4netServiceProviderResolver.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | namespace slf4net 24 | { 25 | /// 26 | /// A resolver for the slf4net service provider 27 | /// 28 | public interface ISlf4netServiceProviderResolver 29 | { 30 | /// 31 | /// Return the slf4net service provider implementation 32 | /// 33 | /// 34 | ISlf4netServiceProvider GetProvider(); 35 | } 36 | } -------------------------------------------------------------------------------- /src/slf4net/Internal/BasicSlf4netServiceProvider.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | namespace slf4net.Internal 24 | { 25 | internal class BasicSlf4netServiceProvider : ISlf4netServiceProvider 26 | { 27 | private readonly ILoggerFactory _loggerFactory; 28 | private readonly IMdcAdapter _mdcAdapter; 29 | 30 | internal BasicSlf4netServiceProvider(ILoggerFactory loggerFactory, IMdcAdapter mdcAdapter) 31 | { 32 | _loggerFactory = loggerFactory; 33 | _mdcAdapter = mdcAdapter; 34 | } 35 | 36 | public ILoggerFactory GetLoggerFactory() 37 | { 38 | return _loggerFactory; 39 | } 40 | 41 | public IMdcAdapter GetMdcAdapter() 42 | { 43 | return _mdcAdapter; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /src/slf4net/Internal/ConsoleHelper.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System; 25 | using System.Diagnostics; 26 | using System.Text; 27 | 28 | namespace slf4net.Internal 29 | { 30 | /// 31 | /// Console helper class 32 | /// 33 | public static class ConsoleHelper 34 | { 35 | 36 | /// 37 | /// Writes details to console out (or error if there's an exception) 38 | /// 39 | /// 40 | /// 41 | public static void WriteLine(string message, Exception ex = null) 42 | { 43 | var sb = new StringBuilder() 44 | .Append("slf4net: ") 45 | .Append(message); 46 | 47 | try 48 | { 49 | WriteAppDomainInfo(sb); 50 | WriteExceptionInfo(ex, sb); 51 | } 52 | catch 53 | { 54 | // ignored 55 | } 56 | 57 | message = sb.ToString(); 58 | Debug.WriteLine(message); 59 | 60 | if (ex == null) 61 | { 62 | Console.Out.WriteLine(message); 63 | } 64 | else 65 | { 66 | Console.Error.WriteLine(message); 67 | } 68 | } 69 | 70 | private static void WriteAppDomainInfo(StringBuilder sb) 71 | { 72 | var ad = AppDomain.CurrentDomain; 73 | 74 | if (ad == null) 75 | { 76 | return; 77 | } 78 | 79 | sb.AppendLine() 80 | .AppendLine() 81 | .Append("Application information:") 82 | .AppendLine() 83 | .AppendFormat(" Application domain: {0}", ad.FriendlyName) 84 | .AppendLine() 85 | .AppendFormat(" Application path: {0}", ad.BaseDirectory) 86 | .AppendLine() 87 | .AppendFormat(" Machine name: {0}", Environment.MachineName); 88 | 89 | } 90 | 91 | private static void WriteExceptionInfo(Exception ex, StringBuilder sb) 92 | { 93 | 94 | while (ex != null) 95 | { 96 | sb.AppendLine() 97 | .AppendLine() 98 | .Append("Exception information:") 99 | .AppendLine() 100 | .AppendFormat(" Exception type: {0}", ex.GetType().Name) 101 | .AppendLine() 102 | .AppendFormat(" Exception message: {0}", ex.Message) 103 | .AppendLine() 104 | .AppendLine() 105 | .Append(ex.StackTrace); 106 | 107 | ex = ex.InnerException; 108 | } 109 | 110 | } 111 | 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/slf4net/Internal/NOPMdcAdapter.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | namespace slf4net.Internal 24 | { 25 | /// 26 | /// A NOP implementation of the for logging frameworks that don't implement MDC 27 | /// 28 | public class NOPMdcAdapter : IMdcAdapter 29 | { 30 | /// 31 | /// Singleton instance 32 | /// 33 | public static readonly NOPMdcAdapter Instance = new NOPMdcAdapter(); 34 | 35 | private NOPMdcAdapter() 36 | { 37 | } 38 | 39 | /// 40 | public object Get(string key) 41 | { 42 | return null; 43 | } 44 | 45 | /// 46 | public void Set(string key, object value) 47 | { 48 | } 49 | 50 | /// 51 | public void Remove(string key) 52 | { 53 | } 54 | 55 | /// 56 | public void Clear() 57 | { 58 | } 59 | 60 | /// 61 | public object LogicalGet(string key) 62 | { 63 | return null; 64 | } 65 | 66 | /// 67 | public void LogicalSet(string key, object value) 68 | { 69 | } 70 | 71 | /// 72 | public void LogicalRemove(string key) 73 | { 74 | } 75 | 76 | /// 77 | public void LogicalClear() 78 | { 79 | } 80 | 81 | /// 82 | public object GlobalGet(string key) 83 | { 84 | return null; 85 | } 86 | 87 | /// 88 | public void GlobalSet(string key, object value) 89 | { 90 | } 91 | 92 | /// 93 | public void GlobalRemove(string key) 94 | { 95 | } 96 | 97 | /// 98 | public void GlobalClear() 99 | { 100 | } 101 | } 102 | } -------------------------------------------------------------------------------- /src/slf4net/Internal/NOPSlf4netServiceProvider.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using slf4net.Factories; 24 | 25 | namespace slf4net.Internal 26 | { 27 | internal class NOPSlf4netServiceProvider : ISlf4netServiceProvider 28 | { 29 | private readonly NOPLoggerFactory _loggerFactory = NOPLoggerFactory.Instance; 30 | private readonly NOPMdcAdapter _mdcAdapter = NOPMdcAdapter.Instance; 31 | 32 | public static readonly NOPSlf4netServiceProvider Instance = new NOPSlf4netServiceProvider(); 33 | 34 | private NOPSlf4netServiceProvider() 35 | { 36 | } 37 | 38 | public ILoggerFactory GetLoggerFactory() 39 | { 40 | return _loggerFactory; 41 | } 42 | 43 | public IMdcAdapter GetMdcAdapter() 44 | { 45 | return _mdcAdapter; 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/slf4net/Internal/SubstituteServiceProvider.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using slf4net.Factories.Internal; 24 | 25 | namespace slf4net.Internal 26 | { 27 | internal class SubstituteServiceProvider : ISlf4netServiceProvider 28 | { 29 | private readonly SubstituteLoggerFactory _loggerFactory = new SubstituteLoggerFactory(); 30 | private readonly NOPMdcAdapter _mdcAdapter = NOPMdcAdapter.Instance; 31 | 32 | public SubstituteLoggerFactory GetLoggerFactory() 33 | { 34 | return _loggerFactory; 35 | } 36 | 37 | ILoggerFactory ISlf4netServiceProvider.GetLoggerFactory() 38 | { 39 | return GetLoggerFactory(); 40 | } 41 | 42 | public IMdcAdapter GetMdcAdapter() 43 | { 44 | return _mdcAdapter; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /src/slf4net/MDC.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using slf4net.Internal; 24 | 25 | namespace slf4net 26 | { 27 | /// 28 | /// A facade for the underlying logging framework's Mapped Diagnostic Context implementation. 29 | /// 30 | public static class MDC 31 | { 32 | /// 33 | /// Get the diagnostic context identified by the key parameter from the current thread's diagnostic context map. 34 | /// 35 | /// The property key 36 | /// 37 | public static object Get(string key) 38 | { 39 | return GetMdcAdapter().Get(key); 40 | } 41 | 42 | /// 43 | /// Set a diagnostic context value (the val parameter) as identified with the key parameter into the current thread's diagnostic context map. 44 | /// 45 | /// The property key 46 | /// The property value 47 | public static void Set(string key, object value) 48 | { 49 | GetMdcAdapter().Set(key, value); 50 | } 51 | 52 | /// 53 | /// Remove the diagnostic context identified by the key parameter from the current thread's diagnostic context map. 54 | /// 55 | /// The property key 56 | public static void Remove(string key) 57 | { 58 | GetMdcAdapter().Remove(key); 59 | } 60 | 61 | /// 62 | /// Clear all entries in the current thread's diagnostic context map. 63 | /// 64 | public static void Clear() 65 | { 66 | GetMdcAdapter().Clear(); 67 | } 68 | 69 | internal static IMdcAdapter GetMdcAdapter() 70 | { 71 | var provider = LoggerFactory.GetProvider(); 72 | IMdcAdapter mdcAdapter; 73 | 74 | if (provider != null) 75 | { 76 | mdcAdapter = provider.GetMdcAdapter() ?? NOPMdcAdapter.Instance; 77 | } 78 | else 79 | { 80 | ConsoleHelper.WriteLine("Failed to find provider."); 81 | ConsoleHelper.WriteLine("Defaulting to no-operation MDCAdapter implementation."); 82 | mdcAdapter = NOPMdcAdapter.Instance; 83 | } 84 | 85 | return mdcAdapter; 86 | } 87 | } 88 | } -------------------------------------------------------------------------------- /src/slf4net/MDLC.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | namespace slf4net 24 | { 25 | /// 26 | /// A facade for the underlying logging framework's Mapped Diagnostic Logical Context implementation. 27 | /// 28 | public static class MDLC 29 | { 30 | /// 31 | /// Get the diagnostic context identified by the key parameter from the logical diagnostic context map. 32 | /// 33 | /// The property key 34 | /// 35 | public static object Get(string key) 36 | { 37 | return MDC.GetMdcAdapter().LogicalGet(key); 38 | } 39 | 40 | /// 41 | /// Set a diagnostic context value (the val parameter) as identified with the key parameter into the logical diagnostic context map. 42 | /// 43 | /// The property key 44 | /// The property value 45 | public static void Set(string key, object value) 46 | { 47 | MDC.GetMdcAdapter().LogicalSet(key, value); 48 | } 49 | 50 | /// 51 | /// Remove the diagnostic context identified by the key parameter from the logical diagnostic context map. 52 | /// 53 | /// The property key 54 | public static void Remove(string key) 55 | { 56 | MDC.GetMdcAdapter().LogicalRemove(key); 57 | } 58 | 59 | /// 60 | /// Clear all entries in the logical diagnostic context map. 61 | /// 62 | public static void Clear() 63 | { 64 | MDC.GetMdcAdapter().LogicalClear(); 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /src/slf4net/NamedLoggerBase.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Runtime.Serialization; 25 | using System; 26 | 27 | namespace slf4net 28 | { 29 | /// 30 | /// Serves as base class for named logger implementations providing serialization capabilities. 31 | /// 32 | [Serializable] 33 | public abstract class NamedLoggerBase : ISerializable 34 | { 35 | 36 | private const string SERIALIZATION_INFO_NAME = "name"; 37 | 38 | #region Constructors 39 | 40 | /// 41 | /// Constructs a logger instance with the given name. 42 | /// 43 | /// the logger name 44 | protected NamedLoggerBase(string name) 45 | { 46 | Name = name; 47 | } 48 | 49 | /// 50 | /// Constructs an un-named logger 51 | /// 52 | protected NamedLoggerBase() 53 | { 54 | } 55 | 56 | #endregion 57 | 58 | #region Properties 59 | 60 | /// 61 | /// The name of this logger - typically this is written to the 62 | /// log itself along with the formatted message. 63 | /// 64 | public string Name { get; protected set; } 65 | 66 | #endregion 67 | 68 | #region ISerializable 69 | 70 | /// 71 | /// Sets the object data into the serialization info using an IObjectReference serialization helper class 72 | /// 73 | protected virtual void GetObjectData(SerializationInfo info, StreamingContext context) 74 | { 75 | info.SetType(typeof(NamedLoggerSerializationHelper)); 76 | info.AddValue(SERIALIZATION_INFO_NAME, this.Name); 77 | } 78 | 79 | void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) 80 | { 81 | this.GetObjectData(info, context); 82 | } 83 | 84 | [Serializable] 85 | private class NamedLoggerSerializationHelper : IObjectReference, ISerializable 86 | { 87 | 88 | private string _name; 89 | 90 | /// 91 | /// Deserialization constructor 92 | /// 93 | protected NamedLoggerSerializationHelper(SerializationInfo info, StreamingContext context) 94 | { 95 | // Store the logger name 96 | _name = info.GetString(SERIALIZATION_INFO_NAME); 97 | } 98 | 99 | public object GetRealObject(StreamingContext context) 100 | { 101 | // Return the instance from the logger factory 102 | return LoggerFactory.GetLogger(_name); 103 | } 104 | 105 | 106 | public void GetObjectData(SerializationInfo info, StreamingContext context) 107 | { 108 | // Do nothing, should not be called as the serialization helper is not serialized itself. 109 | } 110 | } 111 | 112 | #endregion 113 | 114 | } 115 | } -------------------------------------------------------------------------------- /src/slf4net/Resolvers/AppConfigFactoryResolver.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using System.Configuration; 25 | using slf4net.Configuration; 26 | using slf4net.Factories; 27 | using slf4net.Internal; 28 | 29 | namespace slf4net.Resolvers 30 | { 31 | /// 32 | /// Resolves factories to be used for logger instantiation based 33 | /// on settings taken from the application's default configuration 34 | /// file (App.config or Web.config). 35 | /// 36 | public class AppConfigFactoryResolver : IFactoryResolver, ISlf4netServiceProviderResolver 37 | { 38 | /// 39 | /// The name of the SLF configuration section 40 | /// 41 | public const string CONFIG_SECTION_NAME = "slf4net"; 42 | 43 | private readonly string _sectionName; 44 | private ISlf4netServiceProvider _provider; 45 | 46 | #region Constructors 47 | 48 | /// 49 | /// Default constructor using the standard configuration section name 50 | /// 51 | public AppConfigFactoryResolver() : this(CONFIG_SECTION_NAME) 52 | { 53 | } 54 | 55 | /// 56 | /// Constructor where a custom configuration section name is used. 57 | /// 58 | /// The name of the configuration section 59 | public AppConfigFactoryResolver(string sectionName) 60 | { 61 | _sectionName = sectionName; 62 | LoadConfiguration(); 63 | } 64 | 65 | #endregion 66 | 67 | #region ISlf4netServiceProviderResolver/IFactoryResolver Implementation 68 | 69 | /// 70 | /// Returns the instance to use. 71 | /// 72 | /// 73 | public ISlf4netServiceProvider GetProvider() 74 | { 75 | return _provider; 76 | } 77 | 78 | /// 79 | /// Returns the instance from the slf4net service provider. 80 | /// 81 | /// 82 | public ILoggerFactory GetFactory() 83 | { 84 | return GetProvider()?.GetLoggerFactory(); 85 | } 86 | 87 | #endregion 88 | 89 | #region Non-Public Methods 90 | 91 | /// 92 | /// Loads the resolver configuration from the application's config file. 93 | /// 94 | protected virtual void LoadConfiguration() 95 | { 96 | var configSection = ConfigurationManager.GetSection(_sectionName); 97 | 98 | // Return if no section exists 99 | if (configSection == null) 100 | { 101 | return; 102 | } 103 | 104 | var factory = ReadConfiguration(configSection as SlfConfigurationSection); 105 | 106 | if (factory is ISlf4netServiceProvider provider) 107 | { 108 | _provider = provider; 109 | } 110 | else 111 | { 112 | _provider = new BasicSlf4netServiceProvider(factory, NOPMdcAdapter.Instance); 113 | } 114 | } 115 | 116 | /// 117 | /// Reads the and instantiates the ILoggerFactory defined in configuration. 118 | /// 119 | /// 120 | protected virtual ILoggerFactory ReadConfiguration(SlfConfigurationSection configSection) 121 | { 122 | // Throw exception if section is not a SlfConfigurationSection 123 | if (configSection == null) 124 | { 125 | var msg = 126 | $"Configuration section named {_sectionName} must be type {typeof(SlfConfigurationSection).FullName}."; 127 | throw new ConfigurationErrorsException(msg); 128 | } 129 | 130 | // Construct the factory 131 | return CreateFactoryInstance(configSection.Factory); 132 | } 133 | 134 | /// 135 | /// Creates a factory based on a given configuration. 136 | /// If the factory provides invalid information, an error is logged through 137 | /// the internal logger, and a returned. 138 | /// 139 | /// The configuration that provides type 140 | /// information for the that is being created. 141 | /// Factory instance. 142 | private ILoggerFactory CreateFactoryInstance(FactoryConfigurationElement factoryConfiguration) 143 | { 144 | var factory = ActivatorUtils.Instantiate(factoryConfiguration.Type); 145 | 146 | // If the factory is configurable, invoke its Init method 147 | if (factory is IConfigurableLoggerFactory cf) 148 | { 149 | cf.Init(factoryConfiguration.FactoryData); 150 | } 151 | else 152 | { 153 | if (!string.IsNullOrEmpty(factoryConfiguration.FactoryData)) 154 | { 155 | throw new ConfigurationErrorsException("Factory " + factoryConfiguration.Type + 156 | " does not implement IConfigurableLoggerFactory."); 157 | } 158 | } 159 | 160 | return factory; 161 | } 162 | 163 | #endregion 164 | } 165 | } -------------------------------------------------------------------------------- /src/slf4net/Resolvers/LegacyServiceProviderResolver.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | using System; 24 | using slf4net.Internal; 25 | 26 | namespace slf4net.Resolvers 27 | { 28 | internal class LegacyServiceProviderResolver : ISlf4netServiceProviderResolver 29 | { 30 | private readonly IFactoryResolver _factoryResolver; 31 | 32 | public LegacyServiceProviderResolver(IFactoryResolver factoryResolver) 33 | { 34 | _factoryResolver = factoryResolver ?? throw new ArgumentNullException(nameof(factoryResolver)); 35 | } 36 | 37 | public ISlf4netServiceProvider GetProvider() 38 | { 39 | var loggerFactory = _factoryResolver.GetFactory(); 40 | 41 | switch (loggerFactory) 42 | { 43 | case null: 44 | return NOPSlf4netServiceProvider.Instance; 45 | case ISlf4netServiceProvider provider: 46 | return provider; 47 | default: 48 | return new BasicSlf4netServiceProvider(loggerFactory, NOPMdcAdapter.Instance); 49 | } 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /src/slf4net/Resolvers/NOPLoggerFactoryResolver.cs: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright © 2020 EF Learning Labs 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the “Software”), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | using slf4net.Factories; 25 | using slf4net.Internal; 26 | 27 | namespace slf4net.Resolvers 28 | { 29 | /// 30 | /// A resolver that always returns a , 31 | /// which in turn creates a instance. 32 | /// 33 | public class NOPLoggerFactoryResolver : IFactoryResolver, ISlf4netServiceProviderResolver 34 | { 35 | #region Singleton 36 | 37 | /// 38 | /// Provides access to the singleton instance of 39 | /// the class. 40 | /// 41 | public static NOPLoggerFactoryResolver Instance { get; } = new NOPLoggerFactoryResolver(); 42 | 43 | /// 44 | /// Private constructor. A reference to the Singleton 45 | /// instance of this class is available through the 46 | /// static property. 47 | /// 48 | private NOPLoggerFactoryResolver() 49 | { 50 | } 51 | 52 | #endregion 53 | 54 | #region IFactoryResolver Implementation 55 | 56 | /// 57 | public ILoggerFactory GetFactory() 58 | { 59 | return GetProvider().GetLoggerFactory(); 60 | } 61 | 62 | /// 63 | public ISlf4netServiceProvider GetProvider() 64 | { 65 | return NOPSlf4netServiceProvider.Instance; 66 | } 67 | 68 | #endregion 69 | } 70 | } -------------------------------------------------------------------------------- /src/slf4net/slf4net.PublicKey: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/slf4net/slf4net.PublicKey -------------------------------------------------------------------------------- /src/slf4net/slf4net.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net40;netstandard2.0 5 | slf4net - The Simple Logging Façade for .NET. 6 | 7 | 8 | 9 | true 10 | bin\Release\slf4net.xml 11 | 12 | 13 | 14 | bin\Debug\slf4net.xml 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | <_Parameter1>slf4net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100890f523b76872be0dbd02b9e961bbbcdc64ebcf957cbbf1cf180f31aa6193ca0c06caf3c60c4fcb52022b7e4e031e1d67d9795564d8a3ffa21e01e49c23824d5bce0c026fc92cf1bc1da8b13c55644af09fc3a57458d515ffd3a0d8197f7291ae736338d790edd001f15419a91466ac9a66bd8c5e6e76b7e1c9c80ce2978c3a7 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/slf4net/slf4net.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ef-labs/slf4net/4ef958a97a2d3f71b65edac5ca98be13cb22d7b0/src/slf4net/slf4net.snk --------------------------------------------------------------------------------