Skip to content
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
4 changes: 4 additions & 0 deletions dotnet-logging/MessageTemplateForLoggingCA2254/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root = true

[*.cs]
dotnet_diagnostic.CA2254.severity = warning
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="11.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
using MessageTemplateForLoggingCA2254;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddEnvironmentVariables()
.Build();
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddSingleton<Application>();

var host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(config =>
{
config.AddConfiguration(configuration);
})
.ConfigureServices((context, services) =>
{
services.AddSingleton<Application>();
})
.Build();
using var host = builder.Build();

var app = host.Services.GetRequiredService<Application>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using MessageTemplateForLoggingCA2254;
using Microsoft.Extensions.Logging;
using NSubstitute;
using System.Globalization;
using System.Text.Json;
using Xunit;

Expand Down Expand Up @@ -71,10 +72,12 @@ public void WhenLogMessageWithoutFormatting_ThenShowUsernameAndTimeWithoutFormat
var app = GetApplication();
app.LogMessageWithoutFormatting(_userId, _loggedInTime);

var expected = string.Create(CultureInfo.InvariantCulture, $"User {_userId} logged in at {_loggedInTime}.");

_logger.Received(1).Log(
LogLevel.Information,
Arg.Any<EventId>(),
Arg.Is<object>(o => o.ToString()!.Contains($"User {_userId} logged in at {_loggedInTime:MM/dd/yyyy HH:mm:ss}.")),
Arg.Is<object>(o => o.ToString()!.Contains(expected)),
Arg.Any<Exception>(),
Arg.Any<Func<object, Exception?, string>>()
);
Expand All @@ -86,10 +89,12 @@ public void WhenLogMessageWithFormatting_ThenShowUsernameAndTimeWithFormattingTe
var app = GetApplication();
app.LogMessageWithFormatting(_userId, _loggedInTime);

var expected = string.Create(CultureInfo.InvariantCulture, $"User {_userId:N} logged in at {_loggedInTime:F}.");

_logger.Received(1).Log(
LogLevel.Information,
Arg.Any<EventId>(),
Arg.Is<object>(o => o.ToString()!.Contains($"User {_userId:N} logged in at {_loggedInTime:F}.")),
Arg.Is<object>(o => o.ToString()!.Contains(expected)),
Arg.Any<Exception>(),
Arg.Any<Func<object, Exception?, string>>()
);
Expand Down Expand Up @@ -132,11 +137,13 @@ public void WhenLogMessageToFixCA2254Warning_ThenShowUsernameAndTimeTest()
var app = GetApplication();
app.LogMessageToFixCA2254Warning(_userName, _loggedInTime);

var expected = string.Create(CultureInfo.InvariantCulture, $"User {_userName} logged on {_loggedInTime}");

_logger.Received(1).Log(
LogLevel.Information,
Arg.Any<EventId>(),
Arg.Is<object>(o =>
o.ToString()!.Contains($"User {_userName} logged on {_loggedInTime:MM/dd/yyyy HH:mm:ss}")),
o.ToString()!.Contains(expected)),
Arg.Any<Exception>(),
Arg.Any<Func<object, Exception?, string>>()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,15 +10,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="NSubstitute" Version="6.2.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading