Skip to content

Commit

Permalink
test: add a test case for AddSerilog
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 12, 2023
1 parent e88ec93 commit 10c4e3e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net4.8' ">
Expand All @@ -19,8 +20,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Serilog.Sinks.InMemory" Version="0.11.0" />
<PackageReference Include="Serilog.Sinks.InMemory.Assertions" Version="0.11.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Logging;
using Serilog.Sinks.InMemory;
using Serilog.Sinks.InMemory.Assertions;
using Xunit;

namespace Serilog.Extensions.Hosting.Tests;

public class SerilogLoggingBuilderExtensionsTests
{
[Fact]
public async Task LoggingBuilderExtensions_AddSerilog_SuccessAsync()
{
// Arrange
var builder = WebApplication.CreateBuilder();
var logger = new LoggerConfiguration()
.WriteTo.InMemory()
.CreateLogger();
builder.Logging.AddSerilog(logger);
builder.WebHost.UseTestServer();
var app = builder.Build();

// Act
var message = "Hello World!";
app.Logger.LogInformation("Hello World!");
await app.StartAsync();

// Assert
InMemorySink.Instance.Should().HaveMessage(message);
}
}

0 comments on commit 10c4e3e

Please sign in to comment.