Skip to content

Commit 798905d

Browse files
LoggingAPI: move to .NET 10, dispose the logger factory, use message templates (#2346)
Retarget both projects to net10.0 with Microsoft.Extensions.Logging 10.0.14. Program.cs uses top-level statements and declares the ILoggerFactory with using so the console provider flushes before exit. Student and Department log through message templates. Adds InMemoryLoggerProvider and replaces the NSubstitute tests with tests that assert on its messages, plus a minimum-level test and a host test (MSTest 4.5.2, Microsoft.Extensions.Hosting 10.0.14).
1 parent 5b72a1f commit 798905d

7 files changed

Lines changed: 136 additions & 89 deletions

File tree

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
using Microsoft.Extensions.Logging;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using Microsoft.Extensions.Logging;
72

8-
namespace LoggingAPI
3+
namespace LoggingAPI;
4+
5+
public class Department
96
{
10-
public class Department
11-
{
12-
public string Name { get; set; }
13-
public string Description { get; set; }
7+
public string Name { get; set; }
8+
public string Description { get; set; }
149

15-
private readonly ILogger _logger;
16-
17-
public Department(string name, string description, ILoggerFactory factory)
18-
{
19-
Name = name;
20-
Description = description;
10+
private readonly ILogger _logger;
11+
12+
public Department(string name, string description, ILoggerFactory factory)
13+
{
14+
Name = name;
15+
Description = description;
2116

22-
_logger = factory.CreateLogger<Department>();
23-
_logger.LogInformation("Department is " + Name + " " + Description);
24-
}
17+
_logger = factory.CreateLogger<Department>();
18+
_logger.LogInformation("Department is {Name} {Description}", Name, Description);
2519
}
2620
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace LoggingAPI;
4+
5+
public sealed class InMemoryLoggerProvider : ILoggerProvider
6+
{
7+
public List<string> Messages { get; } = [];
8+
9+
public ILogger CreateLogger(string categoryName) => new InMemoryLogger(categoryName, Messages);
10+
11+
public void Dispose() { }
12+
}
13+
14+
public sealed class InMemoryLogger(string categoryName, List<string> messages) : ILogger
15+
{
16+
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
17+
18+
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;
19+
20+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
21+
Exception? exception, Func<TState, Exception?, string> formatter)
22+
{
23+
if (!IsEnabled(logLevel))
24+
return;
25+
26+
messages.Add($"{logLevel}: {categoryName}: {formatter(state, exception)}");
27+
}
28+
}

‎dotnet-logging/LoggingAPI/LoggingAPI/LoggingAPI.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
13-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.14" />
12+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.14" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="10.0.14" />
14+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.14" />
1515
</ItemGroup>
1616

1717
</Project>
Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
using Microsoft.Extensions.Logging;
1+
using LoggingAPI;
2+
using Microsoft.Extensions.Logging;
23

3-
namespace LoggingAPI
4+
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
45
{
5-
public class Program
6-
{
7-
static void Main(string[] args)
8-
{
9-
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
10-
{
11-
builder.AddConsole();
12-
builder.AddDebug();
13-
});
6+
builder.AddConsole();
7+
builder.AddDebug();
8+
});
149

15-
ILogger<Student> logger = loggerFactory.CreateLogger<Student>();
16-
var student = new Student("John", "IT", logger);
10+
ILogger<Student> logger = loggerFactory.CreateLogger<Student>();
11+
var student = new Student("John", "IT", logger);
1712

18-
var department = new Department("IT", "Information Technology", loggerFactory);
19-
20-
}
21-
}
22-
}
13+
var department = new Department("IT", "Information Technology", loggerFactory);
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
using Microsoft.Extensions.Logging;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using Microsoft.Extensions.Logging;
72

8-
namespace LoggingAPI
3+
namespace LoggingAPI;
4+
5+
public class Student
96
{
10-
public class Student
11-
{
12-
public string Name { get; set; }
13-
public string Department { get; set; }
7+
public string Name { get; set; }
8+
public string Department { get; set; }
149

15-
private readonly ILogger _logger;
10+
private readonly ILogger _logger;
1611

17-
public Student(string name, string department, ILogger<Student> logger)
18-
{
19-
Name = name;
20-
Department = department;
21-
_logger = logger;
12+
public Student(string name, string department, ILogger<Student> logger)
13+
{
14+
Name = name;
15+
Department = department;
16+
_logger = logger;
2217

23-
_logger.LogInformation("Name of student is " + Name +" and his department is " + Department);
24-
}
18+
_logger.LogInformation("Name of student is {Name} and his department is {Department}", Name, Department);
2519
}
2620
}
Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,71 @@
11
using LoggingAPI;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
24
using Microsoft.Extensions.Logging;
35
using Microsoft.VisualStudio.TestTools.UnitTesting;
4-
using NSubstitute;
5-
using System;
6-
using System.IO;
76

8-
namespace Tests
7+
namespace Tests;
8+
9+
[TestClass]
10+
public class LoggingAPITests
911
{
10-
[TestClass]
11-
public class LoggingAPITests
12+
[TestMethod]
13+
public void GivenLogger_ShouldLogInformation_WhenStudentIsCreated()
1214
{
13-
[TestMethod]
14-
public void GivenLogger_ShouldLogInformation_WhenStudentIsCreated()
15-
{
16-
ILogger<Student> logger = Substitute.For<ILogger<Student>>();
17-
var student = new Student("John", "IT", logger);
15+
var provider = new InMemoryLoggerProvider();
16+
using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddProvider(provider));
17+
18+
_ = new Student("John", "IT", factory.CreateLogger<Student>());
19+
20+
Assert.AreEqual(
21+
"Information: LoggingAPI.Student: Name of student is John and his department is IT",
22+
provider.Messages.Single());
23+
}
1824

19-
logger.Received(1).LogInformation("Name of student is John and his department is IT");
20-
}
25+
[TestMethod]
26+
public void GivenLoggerFactory_ShouldCreateLogger_WhenDepartmentIsCreated()
27+
{
28+
var provider = new InMemoryLoggerProvider();
29+
using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddProvider(provider));
30+
31+
_ = new Department("IT", "Information Technology", factory);
32+
33+
Assert.AreEqual(
34+
"Information: LoggingAPI.Department: Department is IT Information Technology",
35+
provider.Messages.Single());
36+
}
2137

22-
[TestMethod]
23-
public void GivenLoggerFactory_ShouldCreateLogger_WhenDepartmentIsCreated()
38+
[TestMethod]
39+
public void GivenMinimumLevelWarning_ShouldSkipInformation_WhenStudentIsCreated()
40+
{
41+
var provider = new InMemoryLoggerProvider();
42+
using ILoggerFactory factory = LoggerFactory.Create(builder =>
2443
{
25-
ILoggerFactory loggerFactory = Substitute.For<ILoggerFactory>();
26-
var department = new Department("IT", "Information Technology", loggerFactory);
44+
builder.SetMinimumLevel(LogLevel.Warning);
45+
builder.AddProvider(provider);
46+
});
47+
48+
_ = new Student("John", "IT", factory.CreateLogger<Student>());
49+
50+
Assert.AreEqual(0, provider.Messages.Count);
51+
}
52+
53+
[TestMethod]
54+
public void GivenHost_ShouldResolveTypedLogger_WhenStudentIsCreated()
55+
{
56+
var provider = new InMemoryLoggerProvider();
57+
58+
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
59+
builder.Logging.ClearProviders();
60+
builder.Logging.AddProvider(provider);
61+
62+
using IHost host = builder.Build();
63+
64+
ILogger<Student> logger = host.Services.GetRequiredService<ILogger<Student>>();
65+
_ = new Student("John", "IT", logger);
2766

28-
loggerFactory.Received(1).CreateLogger<Department>();
29-
}
67+
Assert.AreEqual(
68+
"Information: LoggingAPI.Student: Name of student is John and his department is IT",
69+
provider.Messages.Single());
3070
}
31-
}
71+
}

‎dotnet-logging/LoggingAPI/Tests/Tests.csproj‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
56
<Nullable>enable</Nullable>
6-
77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13-
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
14-
<PackageReference Include="coverlet.collector" Version="3.1.0" />
15-
<PackageReference Include="NSubstitute" Version="4.2.2" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="4.5.2" />
13+
<PackageReference Include="MSTest.TestFramework" Version="4.5.2" />
14+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
15+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.14" />
1616
</ItemGroup>
1717

1818
<ItemGroup>

0 commit comments

Comments
 (0)