Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Moar refactoring! #143

Merged
merged 1 commit into from
Mar 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Logging.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions src/Microsoft.Framework.Logging.Console/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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-*"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Framework.Logging.NLog/project.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Framework.Logging.Serilog/project.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headerz

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a script for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No but I'll go ahead and do it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #145

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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -69,5 +68,4 @@ public IDisposable BeginScope(object state)
return new TraceSourceScope(state);
}
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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>
Expand All @@ -60,26 +47,29 @@ 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);
}
}
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);
Expand All @@ -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)
Expand All @@ -108,4 +98,3 @@ private static bool HasDefaultSwitch(TraceSource traceSource)
}
}
}
#endif
30 changes: 30 additions & 0 deletions src/Microsoft.Framework.Logging.TraceSource/project.json
Original file line number Diff line number Diff line change
@@ -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-*"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.Framework.Logging/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.IO;
using Xunit;

namespace Microsoft.Framework.Logging.Test
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions test/Microsoft.Framework.Logging.Test/project.json
Original file line number Diff line number Diff line change
@@ -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-*"
},
Expand Down