From 090bf4aede5449782e89e0d7815f582902d8411e Mon Sep 17 00:00:00 2001 From: David Fowler <davidfowl@gmail.com> Date: Thu, 5 Mar 2015 02:01:48 -0800 Subject: [PATCH] Moar refactoring! - Moved TraceSourceLogger to Microsoft.Framework.Logging.TraceSource - Removed Logging dependency from all providers, they only need the .Interfaces reference - Moved WriteContext to the test project - Added an extension method AddTraceSource to add the TraceSource provider - Added AddLogging to the Logging project - Removed RootTraceName #142 #141 #140 --- Logging.sln | 17 +++++++++- .../project.json | 4 +-- .../project.json | 2 +- .../project.json | 2 +- .../Internal/TraceSourceScope.cs | 0 ...rosoft.Framework.Logging.TraceSource.kproj | 20 +++++++++++ .../TraceSourceFactoryExtensions.cs | 27 +++++++++++++++ .../TraceSourceLogger.cs | 4 +-- .../TraceSourceLoggerProvider.cs | 33 +++++++------------ .../project.json | 30 +++++++++++++++++ .../LoggingServiceCollectionExtensions.cs | 14 ++++++++ src/Microsoft.Framework.Logging/project.json | 1 + .../TraceSourceLoggerTest.cs | 7 ++-- .../Utils/WriteContext.cs | 0 .../project.json | 2 ++ 15 files changed, 130 insertions(+), 33 deletions(-) rename src/{Microsoft.Framework.Logging => Microsoft.Framework.Logging.TraceSource}/Internal/TraceSourceScope.cs (100%) create mode 100644 src/Microsoft.Framework.Logging.TraceSource/Microsoft.Framework.Logging.TraceSource.kproj create mode 100644 src/Microsoft.Framework.Logging.TraceSource/TraceSourceFactoryExtensions.cs rename src/{Microsoft.Framework.Logging => Microsoft.Framework.Logging.TraceSource}/TraceSourceLogger.cs (97%) rename src/{Microsoft.Framework.Logging => Microsoft.Framework.Logging.TraceSource}/TraceSourceLoggerProvider.cs (74%) create mode 100644 src/Microsoft.Framework.Logging.TraceSource/project.json create mode 100644 src/Microsoft.Framework.Logging/LoggingServiceCollectionExtensions.cs rename {src/Microsoft.Framework.Logging => test/Microsoft.Framework.Logging.Test}/Utils/WriteContext.cs (100%) diff --git a/Logging.sln b/Logging.sln index fa2c1f86..67f7160e 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.22530.0 +VisualStudioVersion = 14.0.22625.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 @@ -23,6 +23,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{699DB330-0095-4266-B7B0-3EAB3710CA49}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging.TraceSource", "src\Microsoft.Framework.Logging.TraceSource\Microsoft.Framework.Logging.TraceSource.kproj", "{1A3EB66F-9E64-4676-852F-24995549ED8A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -105,6 +107,18 @@ Global {00F5A9B3-6213-4526-8B2F-58D5B8427AA2}.Release|Mixed Platforms.Build.0 = Release|Any CPU {00F5A9B3-6213-4526-8B2F-58D5B8427AA2}.Release|x86.ActiveCfg = Release|Any CPU {00F5A9B3-6213-4526-8B2F-58D5B8427AA2}.Release|x86.Build.0 = Release|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|x86.ActiveCfg = Debug|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|x86.Build.0 = Debug|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Any CPU.Build.0 = Release|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|x86.ActiveCfg = Release|Any CPU + {1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -117,5 +131,6 @@ Global {550E0247-0BDD-4016-A29B-250F075686FD} = {8C1F5D80-88EA-4961-84DC-7AC6E13951F4} {75A4DE6D-BBAA-4D59-829D-94009E759A18} = {699DB330-0095-4266-B7B0-3EAB3710CA49} {00F5A9B3-6213-4526-8B2F-58D5B8427AA2} = {699DB330-0095-4266-B7B0-3EAB3710CA49} + {1A3EB66F-9E64-4676-852F-24995549ED8A} = {699DB330-0095-4266-B7B0-3EAB3710CA49} EndGlobalSection EndGlobal diff --git a/src/Microsoft.Framework.Logging.Console/project.json b/src/Microsoft.Framework.Logging.Console/project.json index bad4db2b..c8d40249 100644 --- a/src/Microsoft.Framework.Logging.Console/project.json +++ b/src/Microsoft.Framework.Logging.Console/project.json @@ -2,7 +2,6 @@ "version": "1.0.0-*", "description": "Console logger implementation.", "dependencies": { - "Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.Logging.Interfaces": "1.0.0-*" }, "frameworks": { @@ -12,7 +11,8 @@ "dependencies": { "System.Runtime": "4.0.20.0-beta-*", "System.Console": "4.0.0-beta-*", - "System.IO": "4.0.10-beta-*" + "System.IO": "4.0.10-beta-*", + "System.Threading": "4.0.10-beta-*" } } } diff --git a/src/Microsoft.Framework.Logging.NLog/project.json b/src/Microsoft.Framework.Logging.NLog/project.json index 507247fd..9137fccb 100644 --- a/src/Microsoft.Framework.Logging.NLog/project.json +++ b/src/Microsoft.Framework.Logging.NLog/project.json @@ -1,7 +1,7 @@ { "version": "1.0.0-*", "dependencies": { - "Microsoft.Framework.Logging": "1.0.0-*", + "Microsoft.Framework.Logging.Interfaces": "1.0.0-*", "NLog": "3.1.0" }, "frameworks": { diff --git a/src/Microsoft.Framework.Logging.Serilog/project.json b/src/Microsoft.Framework.Logging.Serilog/project.json index 84af8f00..c86371a7 100644 --- a/src/Microsoft.Framework.Logging.Serilog/project.json +++ b/src/Microsoft.Framework.Logging.Serilog/project.json @@ -1,7 +1,7 @@ { "version": "1.0.0-*", "dependencies": { - "Microsoft.Framework.Logging": "1.0.0-*", + "Microsoft.Framework.Logging.Interfaces": "1.0.0-*", "Serilog": "1.4.14" }, "frameworks": { diff --git a/src/Microsoft.Framework.Logging/Internal/TraceSourceScope.cs b/src/Microsoft.Framework.Logging.TraceSource/Internal/TraceSourceScope.cs similarity index 100% rename from src/Microsoft.Framework.Logging/Internal/TraceSourceScope.cs rename to src/Microsoft.Framework.Logging.TraceSource/Internal/TraceSourceScope.cs diff --git a/src/Microsoft.Framework.Logging.TraceSource/Microsoft.Framework.Logging.TraceSource.kproj b/src/Microsoft.Framework.Logging.TraceSource/Microsoft.Framework.Logging.TraceSource.kproj new file mode 100644 index 00000000..7ae336df --- /dev/null +++ b/src/Microsoft.Framework.Logging.TraceSource/Microsoft.Framework.Logging.TraceSource.kproj @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> + <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> + </PropertyGroup> + + <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" /> + <PropertyGroup Label="Globals"> + <ProjectGuid>1a3eb66f-9e64-4676-852f-24995549ed8a</ProjectGuid> + <RootNamespace>Microsoft.Framework.Logging.TraceSource</RootNamespace> + <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> + <OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> + </PropertyGroup> + + <PropertyGroup> + <SchemaVersion>2.0</SchemaVersion> + </PropertyGroup> + <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> +</Project> \ No newline at end of file diff --git a/src/Microsoft.Framework.Logging.TraceSource/TraceSourceFactoryExtensions.cs b/src/Microsoft.Framework.Logging.TraceSource/TraceSourceFactoryExtensions.cs new file mode 100644 index 00000000..6597cec4 --- /dev/null +++ b/src/Microsoft.Framework.Logging.TraceSource/TraceSourceFactoryExtensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Diagnostics; +using Microsoft.Framework.Internal; + +namespace Microsoft.Framework.Logging +{ + public static class TraceSourceFactoryExtensions + { + public static ILoggerFactory AddTraceSource( + [NotNull] this ILoggerFactory factory, + [NotNull] string switchName, + [NotNull] TraceListener listener) + { + return factory.AddTraceSource(new SourceSwitch(switchName), listener); + } + + public static ILoggerFactory AddTraceSource( + [NotNull] this ILoggerFactory factory, + [NotNull] SourceSwitch sourceSwitch, + [NotNull] TraceListener listener) + { + factory.AddProvider(new TraceSourceLoggerProvider(sourceSwitch, listener)); + + return factory; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.Framework.Logging/TraceSourceLogger.cs b/src/Microsoft.Framework.Logging.TraceSource/TraceSourceLogger.cs similarity index 97% rename from src/Microsoft.Framework.Logging/TraceSourceLogger.cs rename to src/Microsoft.Framework.Logging.TraceSource/TraceSourceLogger.cs index ed6b2884..0531820e 100644 --- a/src/Microsoft.Framework.Logging/TraceSourceLogger.cs +++ b/src/Microsoft.Framework.Logging.TraceSource/TraceSourceLogger.cs @@ -1,7 +1,6 @@ // 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. -#if NET45 || ASPNET50 || ASPNETCORE50 using System; using System.Diagnostics; using Microsoft.Framework.Logging.Internal; @@ -69,5 +68,4 @@ public IDisposable BeginScope(object state) return new TraceSourceScope(state); } } -} -#endif +} \ No newline at end of file diff --git a/src/Microsoft.Framework.Logging/TraceSourceLoggerProvider.cs b/src/Microsoft.Framework.Logging.TraceSource/TraceSourceLoggerProvider.cs similarity index 74% rename from src/Microsoft.Framework.Logging/TraceSourceLoggerProvider.cs rename to src/Microsoft.Framework.Logging.TraceSource/TraceSourceLoggerProvider.cs index 33b17f87..a4d7e507 100644 --- a/src/Microsoft.Framework.Logging/TraceSourceLoggerProvider.cs +++ b/src/Microsoft.Framework.Logging.TraceSource/TraceSourceLoggerProvider.cs @@ -1,10 +1,10 @@ // 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. -#if NET45 || ASPNET50 || ASPNETCORE50 using System; using System.Collections.Concurrent; using System.Diagnostics; +using Microsoft.Framework.Internal; namespace Microsoft.Framework.Logging { @@ -13,37 +13,24 @@ namespace Microsoft.Framework.Logging /// </summary> public class TraceSourceLoggerProvider : ILoggerProvider { - private const string RootTraceName = "Microsoft.AspNet"; private readonly SourceSwitch _rootSourceSwitch; private readonly TraceListener _rootTraceListener; private readonly ConcurrentDictionary<string, TraceSource> _sources = new ConcurrentDictionary<string, TraceSource>(StringComparer.OrdinalIgnoreCase); /// <summary> - /// Initializes a new instance of the <see cref="DiagnosticsLoggerProvider"/> class. - /// </summary> - /// <summary> - /// Creates a factory named "Microsoft.AspNet". - /// </summary> - public TraceSourceLoggerProvider() - { - _rootSourceSwitch = new SourceSwitch(RootTraceName); - _rootTraceListener = null; - } - - /// <summary> - /// Initializes a new instance of the <see cref="DiagnosticsLoggerProvider"/> class. + /// Initializes a new instance of the <see cref="TraceSourceLoggerProvider"/> class. /// </summary> /// <param name="rootSourceSwitch"></param> /// <param name="rootTraceListener"></param> - public TraceSourceLoggerProvider(SourceSwitch rootSourceSwitch, TraceListener rootTraceListener) + public TraceSourceLoggerProvider([NotNull]SourceSwitch rootSourceSwitch, [NotNull]TraceListener rootTraceListener) { - _rootSourceSwitch = rootSourceSwitch ?? new SourceSwitch(RootTraceName); + _rootSourceSwitch = rootSourceSwitch; _rootTraceListener = rootTraceListener; } /// <summary> - /// Creates a new DiagnosticsLogger for the given component name. + /// Creates a new <see cref="ILogger"/> for the given component name. /// </summary> /// <param name="name"></param> /// <returns></returns> @@ -60,12 +47,15 @@ private TraceSource GetOrAddTraceSource(string name) private TraceSource InitializeTraceSource(string traceSourceName) { var traceSource = new TraceSource(traceSourceName); - if (traceSourceName == RootTraceName) + string parentSourceName = ParentSourceName(traceSourceName); + + if (string.IsNullOrEmpty(parentSourceName)) { if (HasDefaultSwitch(traceSource)) { traceSource.Switch = _rootSourceSwitch; } + if (_rootTraceListener != null) { traceSource.Listeners.Add(_rootTraceListener); @@ -73,13 +63,13 @@ private TraceSource InitializeTraceSource(string traceSourceName) } else { - string parentSourceName = ParentSourceName(traceSourceName); if (HasDefaultListeners(traceSource)) { TraceSource parentTraceSource = GetOrAddTraceSource(parentSourceName); traceSource.Listeners.Clear(); traceSource.Listeners.AddRange(parentTraceSource.Listeners); } + if (HasDefaultSwitch(traceSource)) { TraceSource parentTraceSource = GetOrAddTraceSource(parentSourceName); @@ -93,7 +83,7 @@ private TraceSource InitializeTraceSource(string traceSourceName) private static string ParentSourceName(string traceSourceName) { int indexOfLastDot = traceSourceName.LastIndexOf('.'); - return indexOfLastDot == -1 ? RootTraceName : traceSourceName.Substring(0, indexOfLastDot); + return indexOfLastDot == -1 ? null : traceSourceName.Substring(0, indexOfLastDot); } private static bool HasDefaultListeners(TraceSource traceSource) @@ -108,4 +98,3 @@ private static bool HasDefaultSwitch(TraceSource traceSource) } } } -#endif diff --git a/src/Microsoft.Framework.Logging.TraceSource/project.json b/src/Microsoft.Framework.Logging.TraceSource/project.json new file mode 100644 index 00000000..427d3597 --- /dev/null +++ b/src/Microsoft.Framework.Logging.TraceSource/project.json @@ -0,0 +1,30 @@ +{ + "version": "1.0.0-*", + "dependencies": { + "Microsoft.Framework.Logging.Interfaces": "1.0.0-*", + "Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" } + }, + + "frameworks": { + "net45": { + "frameworkAssemblies": { + "System.Collections.Concurrent": "" + } + }, + "aspnet50": { + "frameworkAssemblies": { + "System.Collections.Concurrent": "" + } + }, + "aspnetcore50": { + "dependencies": { + "System.Collections.Concurrent": "4.0.10-beta-*", + "System.Collections": "4.0.10-beta-*", + "System.Diagnostics.TraceSource": "4.0.0-beta-*", + "System.Globalization": "4.0.10-beta-*", + "System.Linq": "4.0.0-beta-*", + "System.Threading": "4.0.10-beta-*" + } + } + } +} diff --git a/src/Microsoft.Framework.Logging/LoggingServiceCollectionExtensions.cs b/src/Microsoft.Framework.Logging/LoggingServiceCollectionExtensions.cs new file mode 100644 index 00000000..6a69286c --- /dev/null +++ b/src/Microsoft.Framework.Logging/LoggingServiceCollectionExtensions.cs @@ -0,0 +1,14 @@ +using Microsoft.Framework.Logging; + +namespace Microsoft.Framework.DependencyInjection +{ + public static class LoggingServiceCollectionExtensions + { + public static IServiceCollection AddLogging(this IServiceCollection services) + { + services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>()); + services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>))); + return services; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.Framework.Logging/project.json b/src/Microsoft.Framework.Logging/project.json index 139cae43..fc63ac45 100644 --- a/src/Microsoft.Framework.Logging/project.json +++ b/src/Microsoft.Framework.Logging/project.json @@ -2,6 +2,7 @@ "version": "1.0.0-*", "description": "Logging infrastructure.", "dependencies": { + "Microsoft.Framework.DependencyInjection.Interfaces": "1.0.0-*", "Microsoft.Framework.Logging.Interfaces": "1.0.0-*" }, "compilationOptions": { diff --git a/test/Microsoft.Framework.Logging.Test/TraceSourceLoggerTest.cs b/test/Microsoft.Framework.Logging.Test/TraceSourceLoggerTest.cs index 934e2d73..245d45b1 100644 --- a/test/Microsoft.Framework.Logging.Test/TraceSourceLoggerTest.cs +++ b/test/Microsoft.Framework.Logging.Test/TraceSourceLoggerTest.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.IO; using Xunit; namespace Microsoft.Framework.Logging.Test @@ -20,7 +21,7 @@ public static void IsEnabledReturnsCorrectValue() var logger = factory.CreateLogger("Test"); // Act - factory.AddProvider(new TraceSourceLoggerProvider(testSwitch, new ConsoleTraceListener())); + factory.AddTraceSource(testSwitch, new ConsoleTraceListener()); // Assert Assert.True(logger.IsEnabled(LogLevel.Critical)); @@ -48,8 +49,8 @@ public static void MultipleLoggers_IsEnabledReturnsCorrectValue(SourceLevels fir var logger = factory.CreateLogger("Test"); // Act - factory.AddProvider(new TraceSourceLoggerProvider(firstSwitch, new ConsoleTraceListener())); - factory.AddProvider(new TraceSourceLoggerProvider(secondSwitch, new ConsoleTraceListener())); + factory.AddTraceSource(firstSwitch, new ConsoleTraceListener()); + factory.AddTraceSource(secondSwitch, new ConsoleTraceListener()); // Assert Assert.Equal(expected, logger.IsEnabled(LogLevel.Information)); diff --git a/src/Microsoft.Framework.Logging/Utils/WriteContext.cs b/test/Microsoft.Framework.Logging.Test/Utils/WriteContext.cs similarity index 100% rename from src/Microsoft.Framework.Logging/Utils/WriteContext.cs rename to test/Microsoft.Framework.Logging.Test/Utils/WriteContext.cs diff --git a/test/Microsoft.Framework.Logging.Test/project.json b/test/Microsoft.Framework.Logging.Test/project.json index 3a97fd0a..488812d5 100644 --- a/test/Microsoft.Framework.Logging.Test/project.json +++ b/test/Microsoft.Framework.Logging.Test/project.json @@ -1,6 +1,8 @@ { "dependencies": { + "Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.Logging.Console": "1.0.0-*", + "Microsoft.Framework.Logging.TraceSource": "1.0.0-*", "Microsoft.Framework.Logging.Serilog": "1.0.0-*", "xunit.runner.kre": "1.0.0-*" },