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

Commit 81016a2

Browse files
author
Ben Brown
committed
Added LoggerFactoryExtensions
Provides Create<T> to create type named loggers.
1 parent 5863188 commit 81016a2

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.Framework.Logging
7+
{
8+
/// <summary>
9+
/// ILoggerFactory extension methods for common scenarios.
10+
/// </summary>
11+
public static class LoggerFactoryExtensions
12+
{
13+
/// <summary>
14+
/// Creates a new ILogger instance using the full name of the given type.
15+
/// </summary>
16+
/// <typeparam name="T">The type.</typeparam>
17+
/// <param name="factory">The factory.</param>
18+
public static ILogger Create<T>(this ILoggerFactory factory)
19+
{
20+
if (factory == null)
21+
{
22+
throw new ArgumentNullException("factory");
23+
}
24+
25+
return factory.Create(typeof(T).FullName);
26+
}
27+
}
28+
}

test/Microsoft.Framework.Logging.Test/DiagnosticsScopeTests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
24
using System.Diagnostics;
35
using Xunit;
46
#if NET45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#if NET45
5+
using Moq;
6+
#endif
7+
using Xunit;
8+
9+
namespace Microsoft.Framework.Logging.Test
10+
{
11+
public class LoggerFactoryExtensionsTest
12+
{
13+
#if NET45
14+
[Fact]
15+
public void LoggerFactoryCreateOfT_CallsCreateWithCorrectName()
16+
{
17+
// Arrange
18+
var expected = typeof(TestType).FullName;
19+
20+
var factory = new Mock<ILoggerFactory>();
21+
factory.Setup(f => f.Create(
22+
It.IsAny<string>()))
23+
.Returns(new Mock<ILogger>().Object);
24+
25+
// Act
26+
factory.Object.Create<TestType>();
27+
28+
// Assert
29+
factory.Verify(f => f.Create(expected));
30+
}
31+
#endif
32+
33+
private class TestType
34+
{
35+
// intentionally holds nothing
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)