diff --git a/Logging.sln b/Logging.sln index 13e00407..3449a017 100644 --- a/Logging.sln +++ b/Logging.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.21901.1 +VisualStudioVersion = 14.0.22115.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging", "src\Microsoft.Framework.Logging\Microsoft.Framework.Logging.kproj", "{19D1B6C5-8A62-4387-8816-C54874D1DF5F}" EndProject @@ -17,8 +17,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8C1F EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SampleApp", "samples\SampleApp\SampleApp.kproj", "{550E0247-0BDD-4016-A29B-250F075686FD}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging.Console", "src\Microsoft.Framework.Logging.Console\Microsoft.Framework.Logging.Console.kproj", "{75A4DE6D-BBAA-4D59-829D-94009E759A18}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + aspnet50|Any CPU = aspnet50|Any CPU + aspnet50|Mixed Platforms = aspnet50|Mixed Platforms + aspnet50|x86 = aspnet50|x86 + aspnetcore50|Any CPU = aspnetcore50|Any CPU + aspnetcore50|Mixed Platforms = aspnetcore50|Mixed Platforms + aspnetcore50|x86 = aspnetcore50|x86 Debug|Any CPU = Debug|Any CPU Debug|Mixed Platforms = Debug|Mixed Platforms Debug|x86 = Debug|x86 @@ -27,6 +35,16 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Any CPU.ActiveCfg = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Any CPU.Build.0 = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Mixed Platforms.ActiveCfg = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Mixed Platforms.Build.0 = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|x86.ActiveCfg = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Any CPU.ActiveCfg = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Any CPU.Build.0 = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Mixed Platforms.ActiveCfg = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Mixed Platforms.Build.0 = Release|Any CPU + {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|x86.ActiveCfg = Release|Any CPU {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Debug|Any CPU.Build.0 = Debug|Any CPU {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -37,6 +55,16 @@ Global {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Release|Mixed Platforms.Build.0 = Release|Any CPU {19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Release|x86.ActiveCfg = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Any CPU.ActiveCfg = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Any CPU.Build.0 = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Mixed Platforms.ActiveCfg = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Mixed Platforms.Build.0 = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|x86.ActiveCfg = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Any CPU.ActiveCfg = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Any CPU.Build.0 = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Mixed Platforms.ActiveCfg = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Mixed Platforms.Build.0 = Release|Any CPU + {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|x86.ActiveCfg = Release|Any CPU {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Debug|Any CPU.Build.0 = Debug|Any CPU {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -47,6 +75,16 @@ Global {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Release|Mixed Platforms.Build.0 = Release|Any CPU {96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Release|x86.ActiveCfg = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Any CPU.ActiveCfg = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Any CPU.Build.0 = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Mixed Platforms.ActiveCfg = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Mixed Platforms.Build.0 = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|x86.ActiveCfg = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Any CPU.ActiveCfg = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Any CPU.Build.0 = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Mixed Platforms.ActiveCfg = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Mixed Platforms.Build.0 = Release|Any CPU + {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|x86.ActiveCfg = Release|Any CPU {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Debug|Any CPU.Build.0 = Debug|Any CPU {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -57,6 +95,16 @@ Global {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Release|Mixed Platforms.Build.0 = Release|Any CPU {8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Release|x86.ActiveCfg = Release|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Any CPU.ActiveCfg = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Any CPU.Build.0 = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Mixed Platforms.ActiveCfg = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Mixed Platforms.Build.0 = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|x86.ActiveCfg = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Any CPU.ActiveCfg = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Any CPU.Build.0 = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Mixed Platforms.ActiveCfg = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Mixed Platforms.Build.0 = aspnet50|Any CPU + {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|x86.ActiveCfg = aspnet50|Any CPU {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Debug|Any CPU.Build.0 = Debug|Any CPU {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -67,6 +115,16 @@ Global {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Release|Mixed Platforms.Build.0 = Release|Any CPU {718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Release|x86.ActiveCfg = Release|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Any CPU.ActiveCfg = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Any CPU.Build.0 = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Mixed Platforms.ActiveCfg = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Mixed Platforms.Build.0 = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|x86.ActiveCfg = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Any CPU.ActiveCfg = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Any CPU.Build.0 = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Mixed Platforms.ActiveCfg = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Mixed Platforms.Build.0 = aspnetcore50|Any CPU + {550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|x86.ActiveCfg = aspnetcore50|Any CPU {550E0247-0BDD-4016-A29B-250F075686FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {550E0247-0BDD-4016-A29B-250F075686FD}.Debug|Any CPU.Build.0 = Debug|Any CPU {550E0247-0BDD-4016-A29B-250F075686FD}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -77,6 +135,26 @@ Global {550E0247-0BDD-4016-A29B-250F075686FD}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {550E0247-0BDD-4016-A29B-250F075686FD}.Release|Mixed Platforms.Build.0 = Release|Any CPU {550E0247-0BDD-4016-A29B-250F075686FD}.Release|x86.ActiveCfg = Release|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Any CPU.ActiveCfg = aspnet50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Any CPU.Build.0 = aspnet50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Mixed Platforms.ActiveCfg = aspnet50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Mixed Platforms.Build.0 = aspnet50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|x86.ActiveCfg = aspnet50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Any CPU.ActiveCfg = aspnetcore50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Any CPU.Build.0 = aspnetcore50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Mixed Platforms.ActiveCfg = aspnetcore50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Mixed Platforms.Build.0 = aspnetcore50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|x86.ActiveCfg = aspnetcore50|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Any CPU.Build.0 = Debug|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|x86.ActiveCfg = Debug|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Any CPU.ActiveCfg = Release|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Any CPU.Build.0 = Release|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/samples/SampleApp/Program.cs b/samples/SampleApp/Program.cs index 3e1d7a18..460293b5 100644 --- a/samples/SampleApp/Program.cs +++ b/samples/SampleApp/Program.cs @@ -30,6 +30,8 @@ public void Main(string[] args) catch (Exception ex) { _logger.WriteError("Unexpected error starting application", ex); + _logger.Write(TraceType.Critical, 0, "unexpected error", ex, null); + _logger.Write(TraceType.Critical, 0, null, null, null); } using (_logger.BeginScope("Main")) diff --git a/src/Microsoft.Framework.Logging.Interfaces/ILogger.cs b/src/Microsoft.Framework.Logging.Interfaces/ILogger.cs index 0b103acc..318d6bb6 100644 --- a/src/Microsoft.Framework.Logging.Interfaces/ILogger.cs +++ b/src/Microsoft.Framework.Logging.Interfaces/ILogger.cs @@ -15,8 +15,6 @@ public interface ILogger { /// /// Aggregates most logging patterns to a single method. This must be compatible with the Func representation in the OWIN environment. - /// - /// To check IsEnabled call WriteCore with only TraceEventType and check the return value, no event will be written. /// /// /// @@ -24,8 +22,14 @@ public interface ILogger /// /// /// - bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func formatter); + void Write(TraceType eventType, int eventId, object state, Exception exception, Func formatter); + /// + /// Checks if the given TraceEventType is enabled. + /// + /// + /// + bool IsEnabled(TraceType eventType); /// /// Begins a logical operation scope. diff --git a/src/Microsoft.Framework.Logging.NLog/NLogLoggerProvider.cs b/src/Microsoft.Framework.Logging.NLog/NLogLoggerProvider.cs index 019fb23a..14aedb13 100644 --- a/src/Microsoft.Framework.Logging.NLog/NLogLoggerProvider.cs +++ b/src/Microsoft.Framework.Logging.NLog/NLogLoggerProvider.cs @@ -29,7 +29,7 @@ public Logger(global::NLog.Logger logger) _logger = logger; } - public bool WriteCore( + public void Write( TraceType eventType, int eventId, object state, @@ -37,19 +37,33 @@ public bool WriteCore( Func formatter) { var logLevel = GetLogLevel(eventType); - if (!_logger.IsEnabled(logLevel)) + var message = string.Empty; + if (formatter != null) { - return false; + message = formatter(state, exception); } - if (formatter == null) + else { - return true; + if (state != null) + { + message += state; + } + if (exception != null) + { + message += Environment.NewLine + exception; + } } - var message = formatter(state, exception); - var eventInfo = LogEventInfo.Create(logLevel, _logger.Name, message, exception); - eventInfo.Properties["EventId"] = eventId; - _logger.Log(eventInfo); - return true; + if (!string.IsNullOrEmpty(message)) + { + var eventInfo = LogEventInfo.Create(logLevel, _logger.Name, message, exception); + eventInfo.Properties["EventId"] = eventId; + _logger.Log(eventInfo); + } + } + + public bool IsEnabled(TraceType eventType) + { + return _logger.IsEnabled(GetLogLevel(eventType)); } private LogLevel GetLogLevel(TraceType eventType) diff --git a/src/Microsoft.Framework.Logging/DiagnosticsLogger.cs b/src/Microsoft.Framework.Logging/DiagnosticsLogger.cs index ec803015..7229c89c 100644 --- a/src/Microsoft.Framework.Logging/DiagnosticsLogger.cs +++ b/src/Microsoft.Framework.Logging/DiagnosticsLogger.cs @@ -16,19 +16,38 @@ public DiagnosticsLogger(TraceSource traceSource) _traceSource = traceSource; } - public bool WriteCore(TraceType traceType, int eventId, object state, Exception exception, Func formatter) + public void Write(TraceType traceType, int eventId, object state, Exception exception, Func formatter) { - var eventType = GetEventType(traceType); - - if (!_traceSource.Switch.ShouldTrace(eventType)) + if (!IsEnabled(traceType)) + { + return; + } + var message = string.Empty; + if (formatter != null) + { + message = formatter(state, exception); + } + else { - return false; + if (state != null) + { + message += state; + } + if (exception != null) + { + message += Environment.NewLine + exception; + } } - else if (formatter != null) + if (!string.IsNullOrEmpty(message)) { - _traceSource.TraceEvent(eventType, eventId, formatter(state, exception)); + _traceSource.TraceEvent(GetEventType(traceType), eventId, message); } - return true; + } + + public bool IsEnabled(TraceType traceType) + { + var eventType = GetEventType(traceType); + return _traceSource.Switch.ShouldTrace(eventType); } private static TraceEventType GetEventType(TraceType traceType) diff --git a/src/Microsoft.Framework.Logging/Logger.cs b/src/Microsoft.Framework.Logging/Logger.cs index b3524188..ddb7ef58 100644 --- a/src/Microsoft.Framework.Logging/Logger.cs +++ b/src/Microsoft.Framework.Logging/Logger.cs @@ -25,22 +25,24 @@ public Logger(LoggerFactory loggerFactory, string name) } } - public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func formatter) + public void Write(TraceType eventType, int eventId, object state, Exception exception, Func formatter) { - var result = false; - var count = _loggers.Length; - for (var index = 0; index != count; index++) + foreach (var logger in _loggers) { - result |= _loggers[index].WriteCore(eventType, eventId, state, exception, formatter); + logger.Write(eventType, eventId, state, exception, formatter); } - return result; + } + + public bool IsEnabled(TraceType eventType) + { + return _loggers.Any(l => l.IsEnabled(eventType)); } public IDisposable BeginScope(object state) { var count = _loggers.Length; var scope = new Scope(count); - for (var index = 0; index != count; index++) + for (var index = 0; index < count; index++) { scope.SetDisposable(index, _loggers[index].BeginScope(state)); } diff --git a/src/Microsoft.Framework.Logging/LoggerExtensions.cs b/src/Microsoft.Framework.Logging/LoggerExtensions.cs index d67bb8cf..473cdfc5 100644 --- a/src/Microsoft.Framework.Logging/LoggerExtensions.cs +++ b/src/Microsoft.Framework.Logging/LoggerExtensions.cs @@ -13,23 +13,8 @@ namespace Microsoft.Framework.Logging public static class LoggerExtensions { private static readonly Func TheMessage = (message, error) => (string)message; - private static readonly Func TheMessageAndError = (message, error) => string.Format(CultureInfo.CurrentCulture, "{0}\r\n{1}", message, error); - - /// - /// Checks if the given TraceEventType is enabled. - /// - /// - /// - /// - public static bool IsEnabled(this ILogger logger, TraceType eventType) - { - if (logger == null) - { - throw new ArgumentNullException("logger"); - } - - return logger.WriteCore(eventType, 0, null, null, null); - } + private static readonly Func TheMessageAndError = (message, error) + => string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", message, Environment.NewLine, error); /// /// Writes a verbose log message. @@ -44,7 +29,7 @@ public static void WriteVerbose(this ILogger logger, string data) throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Verbose, 0, data, null, TheMessage); + logger.Write(TraceType.Verbose, 0, data, null, TheMessage); } /// @@ -59,7 +44,7 @@ public static void WriteInformation(this ILogger logger, string message) throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Information, 0, message, null, TheMessage); + logger.Write(TraceType.Information, 0, message, null, TheMessage); } /// @@ -75,7 +60,7 @@ public static void WriteWarning(this ILogger logger, string message, params stri throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Warning, 0, + logger.Write(TraceType.Warning, 0, string.Format(CultureInfo.InvariantCulture, message, args), null, TheMessage); } @@ -92,7 +77,7 @@ public static void WriteWarning(this ILogger logger, string message, Exception e throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Warning, 0, message, error, TheMessageAndError); + logger.Write(TraceType.Warning, 0, message, error, TheMessageAndError); } /// @@ -107,7 +92,7 @@ public static void WriteError(this ILogger logger, string message) throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Error, 0, message, null, TheMessage); + logger.Write(TraceType.Error, 0, message, null, TheMessage); } /// @@ -123,7 +108,7 @@ public static void WriteError(this ILogger logger, string message, Exception err throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Error, 0, message, error, TheMessageAndError); + logger.Write(TraceType.Error, 0, message, error, TheMessageAndError); } /// @@ -138,7 +123,7 @@ public static void WriteCritical(this ILogger logger, string message) throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Critical, 0, message, null, TheMessage); + logger.Write(TraceType.Critical, 0, message, null, TheMessage); } /// @@ -154,7 +139,7 @@ public static void WriteCritical(this ILogger logger, string message, Exception throw new ArgumentNullException("logger"); } - logger.WriteCore(TraceType.Critical, 0, message, error, TheMessageAndError); + logger.Write(TraceType.Critical, 0, message, error, TheMessageAndError); } } } diff --git a/test/Microsoft.Framework.Logging.Test/DiagnosticsLoggerTest.cs b/test/Microsoft.Framework.Logging.Test/DiagnosticsLoggerTest.cs new file mode 100644 index 00000000..13c0b101 --- /dev/null +++ b/test/Microsoft.Framework.Logging.Test/DiagnosticsLoggerTest.cs @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Diagnostics; +using Xunit; + +namespace Microsoft.Framework.Logging.Test +{ + public class DiagnosticsLoggerTest + { + [Fact] + public static void IsEnabledReturnsCorrectValue() + { + // Arrange + var testSwitch = new SourceSwitch("TestSwitch", "Level will be set to warning for this test"); + testSwitch.Level = SourceLevels.Warning; + + var factory = new LoggerFactory(); + var logger = factory.Create("Test"); + + // Act + factory.AddProvider(new DiagnosticsLoggerProvider(testSwitch, new ConsoleTraceListener())); + + // Assert + Assert.True(logger.IsEnabled(TraceType.Critical)); + Assert.True(logger.IsEnabled(TraceType.Error)); + Assert.True(logger.IsEnabled(TraceType.Warning)); + Assert.False(logger.IsEnabled(TraceType.Information)); + Assert.False(logger.IsEnabled(TraceType.Verbose)); + } + + [Theory] + [InlineData(SourceLevels.Warning, SourceLevels.Information, true)] + [InlineData(SourceLevels.Information, SourceLevels.Information, true)] + [InlineData(SourceLevels.Information, SourceLevels.Warning, true)] + [InlineData(SourceLevels.Warning, SourceLevels.Warning, false)] + public static void MultipleLoggers_IsEnabledReturnsCorrectValue(SourceLevels first, SourceLevels second, bool expected) + { + // Arrange + var firstSwitch = new SourceSwitch("FirstSwitch", "First Test Switch"); + firstSwitch.Level = first; + + var secondSwitch = new SourceSwitch("SecondSwitch", "Second Test Switch"); + secondSwitch.Level = second; + + var factory = new LoggerFactory(); + var logger = factory.Create("Test"); + + // Act + factory.AddProvider(new DiagnosticsLoggerProvider(firstSwitch, new ConsoleTraceListener())); + factory.AddProvider(new DiagnosticsLoggerProvider(secondSwitch, new ConsoleTraceListener())); + + // Assert + Assert.Equal(expected, logger.IsEnabled(TraceType.Information)); + } + } +} \ No newline at end of file