From da33c930efcd3cd1afd091f9890c2a72eb99ea93 Mon Sep 17 00:00:00 2001 From: James Connor Date: Fri, 12 Jan 2018 23:50:34 +0000 Subject: [PATCH 1/2] .NET Standard 2.0 support NLog stable still doesn't support it. --- Obvs.Logging.Log4Net/Log4NetLogFactory.cs | 5 +++-- Obvs.Logging.Log4Net/Obvs.Logging.Log4Net.csproj | 6 +++--- Obvs.Logging.NLog/Obvs.Logging.NLog.csproj | 6 +++--- Obvs.Logging.Tests/Obvs.Logging.Tests.csproj | 12 +++++++----- Obvs.Logging.Tests/TestLoggerWrapper.cs | 9 ++++----- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Obvs.Logging.Log4Net/Log4NetLogFactory.cs b/Obvs.Logging.Log4Net/Log4NetLogFactory.cs index 3ff8bff..5387933 100644 --- a/Obvs.Logging.Log4Net/Log4NetLogFactory.cs +++ b/Obvs.Logging.Log4Net/Log4NetLogFactory.cs @@ -1,4 +1,5 @@ -using log4net; +using System.Reflection; +using log4net; namespace Obvs.Logging.Log4Net { @@ -6,7 +7,7 @@ public class Log4NetLogFactory : ILoggerFactory { public ILogger Create(string name) { - return new Log4NetLogWrapper(LogManager.GetLogger(name)); + return new Log4NetLogWrapper(LogManager.GetLogger(Assembly.GetEntryAssembly() ,name)); } public ILogger Create() diff --git a/Obvs.Logging.Log4Net/Obvs.Logging.Log4Net.csproj b/Obvs.Logging.Log4Net/Obvs.Logging.Log4Net.csproj index 4a490b2..cbfb0b6 100644 --- a/Obvs.Logging.Log4Net/Obvs.Logging.Log4Net.csproj +++ b/Obvs.Logging.Log4Net/Obvs.Logging.Log4Net.csproj @@ -1,6 +1,6 @@  - net45 + netstandard2.0;net45 Copyright © Christopher Read 2017 1.0.0 @@ -11,7 +11,7 @@ log4net logging extension for Obvs - - + + \ No newline at end of file diff --git a/Obvs.Logging.NLog/Obvs.Logging.NLog.csproj b/Obvs.Logging.NLog/Obvs.Logging.NLog.csproj index a500228..ad910cf 100644 --- a/Obvs.Logging.NLog/Obvs.Logging.NLog.csproj +++ b/Obvs.Logging.NLog/Obvs.Logging.NLog.csproj @@ -1,6 +1,6 @@  - net45 + net461 Copyright © Christopher Read 2017 1.0.0 @@ -11,7 +11,7 @@ NLog logging extension for Obvs - - + + \ No newline at end of file diff --git a/Obvs.Logging.Tests/Obvs.Logging.Tests.csproj b/Obvs.Logging.Tests/Obvs.Logging.Tests.csproj index efd9e12..50d79a7 100644 --- a/Obvs.Logging.Tests/Obvs.Logging.Tests.csproj +++ b/Obvs.Logging.Tests/Obvs.Logging.Tests.csproj @@ -1,12 +1,14 @@  - net45 + net461 - - - - + + + + + + diff --git a/Obvs.Logging.Tests/TestLoggerWrapper.cs b/Obvs.Logging.Tests/TestLoggerWrapper.cs index 0ff8cee..4efeb0b 100644 --- a/Obvs.Logging.Tests/TestLoggerWrapper.cs +++ b/Obvs.Logging.Tests/TestLoggerWrapper.cs @@ -1,20 +1,19 @@ -using NUnit.Framework; -using Obvs.Logging.NLog; +using Obvs.Logging.NLog; using Obvs.Logging.Log4Net; +using Xunit; namespace Obvs.Logging.Tests { - [TestFixture] public class TestLoggerWrapper { - [Test] + [Fact] public void ShouldUsesCorrectNameForNLog() { MyClass myClass = new MyClass(new NLogLoggerFactory()); myClass.LogSomething(); } - [Test] + [Fact] public void ShouldUsesCorrectNameForLog4Net() { MyClass myClass = new MyClass(new Log4NetLogFactory()); From aa538ac9785521386ec9a841e2b49752db244367 Mon Sep 17 00:00:00 2001 From: James Connor Date: Sat, 13 Jan 2018 00:00:25 +0000 Subject: [PATCH 2/2] Adding Serilog support --- .../Configuration/FluentConfigExtensions.cs | 21 +++++ .../Obvs.Logging.Serilog.csproj | 12 +++ Obvs.Logging.Serilog/SerilogLoggerFactory.cs | 17 ++++ Obvs.Logging.Serilog/SerilogLoggerWrapper.cs | 84 +++++++++++++++++++ Obvs.Logging.sln | 17 +++- 5 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 Obvs.Logging.Serilog/Configuration/FluentConfigExtensions.cs create mode 100644 Obvs.Logging.Serilog/Obvs.Logging.Serilog.csproj create mode 100644 Obvs.Logging.Serilog/SerilogLoggerFactory.cs create mode 100644 Obvs.Logging.Serilog/SerilogLoggerWrapper.cs diff --git a/Obvs.Logging.Serilog/Configuration/FluentConfigExtensions.cs b/Obvs.Logging.Serilog/Configuration/FluentConfigExtensions.cs new file mode 100644 index 0000000..8751596 --- /dev/null +++ b/Obvs.Logging.Serilog/Configuration/FluentConfigExtensions.cs @@ -0,0 +1,21 @@ +using System; +using Obvs.Configuration; + +namespace Obvs.Logging.Serilog.Configuration +{ + public static class FluentConfigExtensions + { + public static ICanCreate UsingNLog(this ICanAddEndpointOrLoggingOrCorrelationOrCreate configurator, + Func, bool> enableLogging = null, + Func logLevelSend = null, + Func logLevelReceive = null) + where TMessage : class + where TCommand : class, TMessage + where TEvent : class, TMessage + where TRequest : class, TMessage + where TResponse : class, TMessage + { + return configurator.UsingLogging(new SerilogLoggerFactory(), enableLogging, logLevelSend, logLevelReceive); + } + } +} \ No newline at end of file diff --git a/Obvs.Logging.Serilog/Obvs.Logging.Serilog.csproj b/Obvs.Logging.Serilog/Obvs.Logging.Serilog.csproj new file mode 100644 index 0000000..5ed9956 --- /dev/null +++ b/Obvs.Logging.Serilog/Obvs.Logging.Serilog.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.0;net45 + + + + + + + + diff --git a/Obvs.Logging.Serilog/SerilogLoggerFactory.cs b/Obvs.Logging.Serilog/SerilogLoggerFactory.cs new file mode 100644 index 0000000..021854a --- /dev/null +++ b/Obvs.Logging.Serilog/SerilogLoggerFactory.cs @@ -0,0 +1,17 @@ +using Serilog.Core; + +namespace Obvs.Logging.Serilog +{ + public class SerilogLoggerFactory : ILoggerFactory + { + public ILogger Create(string name) + { + return new SerilogLoggerWrapper(global::Serilog.Log.ForContext("SourceContext", name)); + } + + public ILogger Create() + { + return new SerilogLoggerWrapper(global::Serilog.Log.ForContext()); + } + } +} \ No newline at end of file diff --git a/Obvs.Logging.Serilog/SerilogLoggerWrapper.cs b/Obvs.Logging.Serilog/SerilogLoggerWrapper.cs new file mode 100644 index 0000000..e45bc4e --- /dev/null +++ b/Obvs.Logging.Serilog/SerilogLoggerWrapper.cs @@ -0,0 +1,84 @@ +using System; +using Serilog.Core; + +namespace Obvs.Logging.Serilog +{ + public class SerilogLoggerWrapper : ILogger + { + private readonly global::Serilog.ILogger _logger; + + public SerilogLoggerWrapper(global::Serilog.ILogger logger) + { + _logger = logger; + } + + public void Debug(string message, Exception exception = null) + { + if (exception == null) + { + _logger.Debug(message); + } + else + { + _logger.Debug(exception, message); + } + } + + public void Info(string message, Exception exception = null) + { + if (exception == null) + { + _logger.Information(message); + } + else + { + _logger.Information(exception, message); + } + } + + public void Warn(string message, Exception exception = null) + { + if (exception == null) + { + _logger.Warning(message); + } + else + { + _logger.Warning(exception, message); + } + } + + public void Error(string message, Exception exception = null) + { + if (exception == null) + { + _logger.Error(message); + } + else + { + _logger.Error(exception, message); + } + } + + public void Log(LogLevel level, string message, Exception exception = null) + { + switch (level) + { + case LogLevel.Debug: + Debug(message, exception); + break; + case LogLevel.Info: + Info(message, exception); + break; + case LogLevel.Warn: + Warn(message, exception); + break; + case LogLevel.Error: + Error(message, exception); + break; + default: + throw new ArgumentOutOfRangeException("level", level, null); + } + } + } +} diff --git a/Obvs.Logging.sln b/Obvs.Logging.sln index b527558..27917a3 100644 --- a/Obvs.Logging.sln +++ b/Obvs.Logging.sln @@ -1,16 +1,18 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27004.2005 +VisualStudioVersion = 15.0.27130.2010 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Obvs.Logging.NLog", "Obvs.Logging.NLog\Obvs.Logging.NLog.csproj", "{66CAB382-FF32-49DB-A103-3F6837B0FB0E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Obvs.Logging.NLog", "Obvs.Logging.NLog\Obvs.Logging.NLog.csproj", "{66CAB382-FF32-49DB-A103-3F6837B0FB0E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Obvs.Logging.Log4Net", "Obvs.Logging.Log4Net\Obvs.Logging.Log4Net.csproj", "{1DCAA7CD-7A3D-435C-84FE-B648A3DD9CAC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Obvs.Logging.Log4Net", "Obvs.Logging.Log4Net\Obvs.Logging.Log4Net.csproj", "{1DCAA7CD-7A3D-435C-84FE-B648A3DD9CAC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Obvs.Logging.Tests", "Obvs.Logging.Tests\Obvs.Logging.Tests.csproj", "{BC611C7A-C186-438D-92EB-488B607065B8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Obvs.Logging.Tests", "Obvs.Logging.Tests\Obvs.Logging.Tests.csproj", "{BC611C7A-C186-438D-92EB-488B607065B8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{CA58ABB1-E39F-433F-A5AE-DF61554A346A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Obvs.Logging.Serilog", "Obvs.Logging.Serilog\Obvs.Logging.Serilog.csproj", "{2CD63F4A-6398-47DB-8025-AFE9040E7B8E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -29,6 +31,10 @@ Global {BC611C7A-C186-438D-92EB-488B607065B8}.Debug|Any CPU.Build.0 = Debug|Any CPU {BC611C7A-C186-438D-92EB-488B607065B8}.Release|Any CPU.ActiveCfg = Release|Any CPU {BC611C7A-C186-438D-92EB-488B607065B8}.Release|Any CPU.Build.0 = Release|Any CPU + {2CD63F4A-6398-47DB-8025-AFE9040E7B8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2CD63F4A-6398-47DB-8025-AFE9040E7B8E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2CD63F4A-6398-47DB-8025-AFE9040E7B8E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2CD63F4A-6398-47DB-8025-AFE9040E7B8E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -36,4 +42,7 @@ Global GlobalSection(NestedProjects) = preSolution {BC611C7A-C186-438D-92EB-488B607065B8} = {CA58ABB1-E39F-433F-A5AE-DF61554A346A} EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D2638AC0-8D73-44E1-A591-1EE2ECFE49F3} + EndGlobalSection EndGlobal