Removed test code related to logging and instead used Testing repo's logging helpers.

This commit is contained in:
Kiran Challa 2015-04-16 10:35:08 -07:00
parent 65bd8c448a
commit 7576116969
20 changed files with 11 additions and 363 deletions

View File

@ -5,9 +5,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.Mvc.Logging;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
@ -27,7 +25,6 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
/// <param name="assemblyProvider"><see cref="IAssemblyProvider"/> that provides assemblies to look for
/// controllers in.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public DefaultControllerTypeProvider(IAssemblyProvider assemblyProvider)
{
_assemblyProvider = assemblyProvider;

View File

@ -1,21 +0,0 @@
// 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 Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc.Logging
{
internal static class LoggerExtensions
{
public static void WriteValues([NotNull] this ILogger logger, object values)
{
logger.Log(
logLevel: LogLevel.Verbose,
eventId: 0,
state: values,
exception: null,
formatter: LogFormatter.Formatter);
}
}
}

View File

@ -1,55 +0,0 @@
// 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.Collections.Generic;
using System.Text;
namespace Microsoft.AspNet.Mvc.Logging
{
internal static class StringBuilderHelpers
{
public static void Append<T>(
StringBuilder builder,
IEnumerable<T> items,
Func<T, string> itemFormatter = null)
{
if (items == null)
{
return;
}
foreach (var item in items)
{
builder.Append(Environment.NewLine);
builder.Append("\t\t");
if (itemFormatter == null)
{
builder.Append(item != null ? item.ToString() : "null");
}
else
{
builder.Append(item != null ? itemFormatter(item) : "null");
}
}
}
public static void Append<K, V>(StringBuilder builder, IDictionary<K, V> dict)
{
if (dict == null)
{
return;
}
foreach (var kvp in dict)
{
builder.Append(Environment.NewLine);
builder.Append("\t\t");
builder.Append(kvp.Key != null ? kvp.Key.ToString() : "null");
builder.Append(" : ");
builder.Append(kvp.Value != null ? kvp.Value.ToString() : "null");
}
}
}
}

View File

@ -17,6 +17,7 @@ using Microsoft.AspNet.Mvc.Routing;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Testing;
using Moq;
using Xunit;
@ -29,7 +30,7 @@ namespace Microsoft.AspNet.Mvc
{
// Arrange
var sink = new TestSink();
var loggerFactory = new TestLoggerFactory(sink);
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
var actions = new ActionDescriptor[]
{

View File

@ -1,12 +0,0 @@
// 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.
namespace Microsoft.AspNet.Mvc
{
public class BeginScopeContext
{
public object Scope { get; set; }
public string LoggerName { get; set; }
}
}

View File

@ -1,17 +0,0 @@
// 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;
namespace Microsoft.AspNet.Mvc
{
public class NullDisposable : IDisposable
{
public static NullDisposable Instance = new NullDisposable();
public void Dispose()
{
// intentionally does nothing
}
}
}

View File

@ -1,27 +0,0 @@
// 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 Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
public class NullLogger : ILogger
{
public static NullLogger Instance = new NullLogger();
public IDisposable BeginScopeImpl(object state)
{
return NullDisposable.Instance;
}
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
}
public bool IsEnabled(LogLevel logLevel)
{
return false;
}
}
}

View File

@ -1,24 +0,0 @@
// 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 Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
public class NullLoggerFactory : ILoggerFactory
{
public static NullLoggerFactory Instance = new NullLoggerFactory();
public LogLevel MinimumLevel { get; set; }
public ILogger CreateLogger(string name)
{
return NullLogger.Instance;
}
public void AddProvider(ILoggerProvider provider)
{
}
}
}

View File

@ -1,33 +0,0 @@
using System;
using System.Linq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Logging
{
public static class PropertiesAssert
{
/// <summary>
/// Given two types, compares their properties and asserts true if they have the same property names.
/// </summary>
/// <param name="original">The original type to compare against.</param>
/// <param name="shadow">The shadow type whose properties will be compared against the original.</param>
/// <param name="exclude">Properties that exist in the original type but not the shadow.</param>
/// <param name="include">Properties that are in the shadow type but not in the original.</param>
public static void PropertiesAreTheSame(Type original, Type shadow, string[] exclude = null, string[] include = null)
{
var originalProperties = original.GetProperties().Where(p => !exclude?.Contains(p.Name) ?? true)
.Select(p => p.Name);
if (include != null)
{
originalProperties = originalProperties.Concat(include.ToList());
}
originalProperties = originalProperties.OrderBy(n => n);
// Message is a property on all ILoggerStructures
var shadowProperties = shadow.GetProperties().Where(p => !string.Equals("Message", p.Name))
.Select(p => p.Name).OrderBy(n => n);
Assert.True(originalProperties.SequenceEqual(shadowProperties));
}
}
}

View File

@ -1,55 +0,0 @@
// 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 Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
public class TestLogger : ILogger
{
private object _scope;
private TestSink _sink;
private string _name;
public TestLogger(string name, TestSink sink)
{
_sink = sink;
_name = name;
}
public string Name { get; set; }
public IDisposable BeginScopeImpl(object state)
{
_scope = state;
_sink.Begin(new BeginScopeContext()
{
LoggerName = _name,
Scope = state,
});
return NullDisposable.Instance;
}
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
_sink.Write(new WriteContext()
{
LogLevel = logLevel,
EventId = eventId,
State = state,
Exception = exception,
Formatter = formatter,
LoggerName = _name,
Scope = _scope
});
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
}
}

View File

@ -1,29 +0,0 @@
// 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 Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
public class TestLoggerFactory : ILoggerFactory
{
private TestSink _sink;
public TestLoggerFactory(TestSink sink)
{
_sink = sink;
}
public LogLevel MinimumLevel { get; set; }
public ILogger CreateLogger(string name)
{
return new TestLogger(name, _sink);
}
public void AddProvider(ILoggerProvider provider)
{
}
}
}

View File

@ -1,56 +0,0 @@
// 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.Collections.Generic;
namespace Microsoft.AspNet.Mvc
{
public class TestSink
{
public TestSink(
Func<WriteContext, bool> writeEnabled = null,
Func<BeginScopeContext, bool> beginEnabled = null)
{
WriteEnabled = writeEnabled;
BeginEnabled = beginEnabled;
Scopes = new List<BeginScopeContext>();
Writes = new List<WriteContext>();
}
public Func<WriteContext, bool> WriteEnabled { get; set; }
public Func<BeginScopeContext, bool> BeginEnabled { get; set; }
public List<BeginScopeContext> Scopes { get; set; }
public List<WriteContext> Writes { get; set; }
public void Write(WriteContext context)
{
if (WriteEnabled == null || WriteEnabled(context))
{
Writes.Add(context);
}
}
public void Begin(BeginScopeContext context)
{
if (BeginEnabled == null || BeginEnabled(context))
{
Scopes.Add(context);
}
}
public static bool EnableWithTypeName<T>(WriteContext context)
{
return context.LoggerName.Equals(typeof(T).FullName);
}
public static bool EnableWithTypeName<T>(BeginScopeContext context)
{
return context.LoggerName.Equals(typeof(T).FullName);
}
}
}

View File

@ -1,25 +0,0 @@
// 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 Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Mvc
{
public class WriteContext
{
public LogLevel LogLevel { get; set; }
public int EventId { get; set; }
public object State { get; set; }
public Exception Exception { get; set; }
public Func<object, Exception, string> Formatter { get; set; }
public object Scope { get; set; }
public string LoggerName { get; set; }
}
}

View File

@ -5,10 +5,9 @@ using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.AspNet.Mvc.Logging;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Testing;
using Microsoft.Framework.OptionsModel;
using Moq;
using Xunit;
@ -22,7 +21,7 @@ namespace Microsoft.AspNet.Mvc
{
// Arrange
var sink = new TestSink();
var loggerFactory = new TestLoggerFactory(sink);
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
var displayName = "A.B.C";
var actionDescriptor = new Mock<ActionDescriptor>();
actionDescriptor.SetupGet(ad => ad.DisplayName)
@ -46,7 +45,7 @@ namespace Microsoft.AspNet.Mvc
{
// Arrange
var sink = new TestSink();
var loggerFactory = new TestLoggerFactory(sink);
var loggerFactory = new TestLoggerFactory(sink, enabled: true);
var mockActionSelector = new Mock<IActionSelector>();
mockActionSelector.Setup(a => a.SelectAsync(It.IsAny<RouteContext>()))

View File

@ -9,6 +9,7 @@ using Microsoft.AspNet.Mvc.ModelBinding.Validation;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Testing;
using Microsoft.Framework.OptionsModel;
using Moq;
using Xunit;

View File

@ -9,6 +9,7 @@ using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Core;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Testing;
using Moq;
using Xunit;

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http.Core;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Testing;
using Microsoft.Framework.OptionsModel;
using Moq;
using Xunit;

View File

@ -6,10 +6,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Logging;
using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Routing.Template;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Testing;
using Microsoft.Framework.OptionsModel;
using Moq;
using Xunit;

View File

@ -10,6 +10,7 @@ using Microsoft.AspNet.Mvc.Internal;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Logging.Testing;
using Microsoft.Framework.OptionsModel;
using Moq;
using Xunit;

View File

@ -7,6 +7,7 @@
"Microsoft.AspNet.Mvc.TestCommon": { "version": "6.0.0-*", "type": "build" },
"Microsoft.AspNet.Mvc.Xml": "6.0.0-*",
"Microsoft.AspNet.Testing": "1.0.0-*",
"Microsoft.Framework.Logging.Testing": "1.0.0-*",
"Moq": "4.2.1312.1622",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},