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

Commit 090bf4a

Browse files
committed
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
1 parent 760e4e0 commit 090bf4a

File tree

15 files changed

+130
-33
lines changed

15 files changed

+130
-33
lines changed

Logging.sln

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.22530.0
4+
VisualStudioVersion = 14.0.22625.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging", "src\Microsoft.Framework.Logging\Microsoft.Framework.Logging.kproj", "{19D1B6C5-8A62-4387-8816-C54874D1DF5F}"
77
EndProject
@@ -23,6 +23,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging
2323
EndProject
2424
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{699DB330-0095-4266-B7B0-3EAB3710CA49}"
2525
EndProject
26+
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}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -105,6 +107,18 @@ Global
105107
{00F5A9B3-6213-4526-8B2F-58D5B8427AA2}.Release|Mixed Platforms.Build.0 = Release|Any CPU
106108
{00F5A9B3-6213-4526-8B2F-58D5B8427AA2}.Release|x86.ActiveCfg = Release|Any CPU
107109
{00F5A9B3-6213-4526-8B2F-58D5B8427AA2}.Release|x86.Build.0 = Release|Any CPU
110+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
112+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
113+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
114+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|x86.ActiveCfg = Debug|Any CPU
115+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Debug|x86.Build.0 = Debug|Any CPU
116+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
117+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Any CPU.Build.0 = Release|Any CPU
118+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
119+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
120+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|x86.ActiveCfg = Release|Any CPU
121+
{1A3EB66F-9E64-4676-852F-24995549ED8A}.Release|x86.Build.0 = Release|Any CPU
108122
EndGlobalSection
109123
GlobalSection(SolutionProperties) = preSolution
110124
HideSolutionNode = FALSE
@@ -117,5 +131,6 @@ Global
117131
{550E0247-0BDD-4016-A29B-250F075686FD} = {8C1F5D80-88EA-4961-84DC-7AC6E13951F4}
118132
{75A4DE6D-BBAA-4D59-829D-94009E759A18} = {699DB330-0095-4266-B7B0-3EAB3710CA49}
119133
{00F5A9B3-6213-4526-8B2F-58D5B8427AA2} = {699DB330-0095-4266-B7B0-3EAB3710CA49}
134+
{1A3EB66F-9E64-4676-852F-24995549ED8A} = {699DB330-0095-4266-B7B0-3EAB3710CA49}
120135
EndGlobalSection
121136
EndGlobal

src/Microsoft.Framework.Logging.Console/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"version": "1.0.0-*",
33
"description": "Console logger implementation.",
44
"dependencies": {
5-
"Microsoft.Framework.Logging": "1.0.0-*",
65
"Microsoft.Framework.Logging.Interfaces": "1.0.0-*"
76
},
87
"frameworks": {
@@ -12,7 +11,8 @@
1211
"dependencies": {
1312
"System.Runtime": "4.0.20.0-beta-*",
1413
"System.Console": "4.0.0-beta-*",
15-
"System.IO": "4.0.10-beta-*"
14+
"System.IO": "4.0.10-beta-*",
15+
"System.Threading": "4.0.10-beta-*"
1616
}
1717
}
1818
}

src/Microsoft.Framework.Logging.NLog/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": "1.0.0-*",
33
"dependencies": {
4-
"Microsoft.Framework.Logging": "1.0.0-*",
4+
"Microsoft.Framework.Logging.Interfaces": "1.0.0-*",
55
"NLog": "3.1.0"
66
},
77
"frameworks": {

src/Microsoft.Framework.Logging.Serilog/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": "1.0.0-*",
33
"dependencies": {
4-
"Microsoft.Framework.Logging": "1.0.0-*",
4+
"Microsoft.Framework.Logging.Interfaces": "1.0.0-*",
55
"Serilog": "1.4.14"
66
},
77
"frameworks": {
File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>1a3eb66f-9e64-4676-852f-24995549ed8a</ProjectGuid>
11+
<RootNamespace>Microsoft.Framework.Logging.TraceSource</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
14+
</PropertyGroup>
15+
16+
<PropertyGroup>
17+
<SchemaVersion>2.0</SchemaVersion>
18+
</PropertyGroup>
19+
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
20+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Diagnostics;
3+
using Microsoft.Framework.Internal;
4+
5+
namespace Microsoft.Framework.Logging
6+
{
7+
public static class TraceSourceFactoryExtensions
8+
{
9+
public static ILoggerFactory AddTraceSource(
10+
[NotNull] this ILoggerFactory factory,
11+
[NotNull] string switchName,
12+
[NotNull] TraceListener listener)
13+
{
14+
return factory.AddTraceSource(new SourceSwitch(switchName), listener);
15+
}
16+
17+
public static ILoggerFactory AddTraceSource(
18+
[NotNull] this ILoggerFactory factory,
19+
[NotNull] SourceSwitch sourceSwitch,
20+
[NotNull] TraceListener listener)
21+
{
22+
factory.AddProvider(new TraceSourceLoggerProvider(sourceSwitch, listener));
23+
24+
return factory;
25+
}
26+
}
27+
}

src/Microsoft.Framework.Logging/TraceSourceLogger.cs renamed to src/Microsoft.Framework.Logging.TraceSource/TraceSourceLogger.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#if NET45 || ASPNET50 || ASPNETCORE50
54
using System;
65
using System.Diagnostics;
76
using Microsoft.Framework.Logging.Internal;
@@ -69,5 +68,4 @@ public IDisposable BeginScope(object state)
6968
return new TraceSourceScope(state);
7069
}
7170
}
72-
}
73-
#endif
71+
}

src/Microsoft.Framework.Logging/TraceSourceLoggerProvider.cs renamed to src/Microsoft.Framework.Logging.TraceSource/TraceSourceLoggerProvider.cs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#if NET45 || ASPNET50 || ASPNETCORE50
54
using System;
65
using System.Collections.Concurrent;
76
using System.Diagnostics;
7+
using Microsoft.Framework.Internal;
88

99
namespace Microsoft.Framework.Logging
1010
{
@@ -13,37 +13,24 @@ namespace Microsoft.Framework.Logging
1313
/// </summary>
1414
public class TraceSourceLoggerProvider : ILoggerProvider
1515
{
16-
private const string RootTraceName = "Microsoft.AspNet";
1716
private readonly SourceSwitch _rootSourceSwitch;
1817
private readonly TraceListener _rootTraceListener;
1918

2019
private readonly ConcurrentDictionary<string, TraceSource> _sources = new ConcurrentDictionary<string, TraceSource>(StringComparer.OrdinalIgnoreCase);
2120

2221
/// <summary>
23-
/// Initializes a new instance of the <see cref="DiagnosticsLoggerProvider"/> class.
24-
/// </summary>
25-
/// <summary>
26-
/// Creates a factory named "Microsoft.AspNet".
27-
/// </summary>
28-
public TraceSourceLoggerProvider()
29-
{
30-
_rootSourceSwitch = new SourceSwitch(RootTraceName);
31-
_rootTraceListener = null;
32-
}
33-
34-
/// <summary>
35-
/// Initializes a new instance of the <see cref="DiagnosticsLoggerProvider"/> class.
22+
/// Initializes a new instance of the <see cref="TraceSourceLoggerProvider"/> class.
3623
/// </summary>
3724
/// <param name="rootSourceSwitch"></param>
3825
/// <param name="rootTraceListener"></param>
39-
public TraceSourceLoggerProvider(SourceSwitch rootSourceSwitch, TraceListener rootTraceListener)
26+
public TraceSourceLoggerProvider([NotNull]SourceSwitch rootSourceSwitch, [NotNull]TraceListener rootTraceListener)
4027
{
41-
_rootSourceSwitch = rootSourceSwitch ?? new SourceSwitch(RootTraceName);
28+
_rootSourceSwitch = rootSourceSwitch;
4229
_rootTraceListener = rootTraceListener;
4330
}
4431

4532
/// <summary>
46-
/// Creates a new DiagnosticsLogger for the given component name.
33+
/// Creates a new <see cref="ILogger"/> for the given component name.
4734
/// </summary>
4835
/// <param name="name"></param>
4936
/// <returns></returns>
@@ -60,26 +47,29 @@ private TraceSource GetOrAddTraceSource(string name)
6047
private TraceSource InitializeTraceSource(string traceSourceName)
6148
{
6249
var traceSource = new TraceSource(traceSourceName);
63-
if (traceSourceName == RootTraceName)
50+
string parentSourceName = ParentSourceName(traceSourceName);
51+
52+
if (string.IsNullOrEmpty(parentSourceName))
6453
{
6554
if (HasDefaultSwitch(traceSource))
6655
{
6756
traceSource.Switch = _rootSourceSwitch;
6857
}
58+
6959
if (_rootTraceListener != null)
7060
{
7161
traceSource.Listeners.Add(_rootTraceListener);
7262
}
7363
}
7464
else
7565
{
76-
string parentSourceName = ParentSourceName(traceSourceName);
7766
if (HasDefaultListeners(traceSource))
7867
{
7968
TraceSource parentTraceSource = GetOrAddTraceSource(parentSourceName);
8069
traceSource.Listeners.Clear();
8170
traceSource.Listeners.AddRange(parentTraceSource.Listeners);
8271
}
72+
8373
if (HasDefaultSwitch(traceSource))
8474
{
8575
TraceSource parentTraceSource = GetOrAddTraceSource(parentSourceName);
@@ -93,7 +83,7 @@ private TraceSource InitializeTraceSource(string traceSourceName)
9383
private static string ParentSourceName(string traceSourceName)
9484
{
9585
int indexOfLastDot = traceSourceName.LastIndexOf('.');
96-
return indexOfLastDot == -1 ? RootTraceName : traceSourceName.Substring(0, indexOfLastDot);
86+
return indexOfLastDot == -1 ? null : traceSourceName.Substring(0, indexOfLastDot);
9787
}
9888

9989
private static bool HasDefaultListeners(TraceSource traceSource)
@@ -108,4 +98,3 @@ private static bool HasDefaultSwitch(TraceSource traceSource)
10898
}
10999
}
110100
}
111-
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"version": "1.0.0-*",
3+
"dependencies": {
4+
"Microsoft.Framework.Logging.Interfaces": "1.0.0-*",
5+
"Microsoft.Framework.NotNullAttribute.Internal": { "type": "build", "version": "1.0.0-*" }
6+
},
7+
8+
"frameworks": {
9+
"net45": {
10+
"frameworkAssemblies": {
11+
"System.Collections.Concurrent": ""
12+
}
13+
},
14+
"aspnet50": {
15+
"frameworkAssemblies": {
16+
"System.Collections.Concurrent": ""
17+
}
18+
},
19+
"aspnetcore50": {
20+
"dependencies": {
21+
"System.Collections.Concurrent": "4.0.10-beta-*",
22+
"System.Collections": "4.0.10-beta-*",
23+
"System.Diagnostics.TraceSource": "4.0.0-beta-*",
24+
"System.Globalization": "4.0.10-beta-*",
25+
"System.Linq": "4.0.0-beta-*",
26+
"System.Threading": "4.0.10-beta-*"
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)