serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core
Apache License 2.0
1.31k stars 205 forks source link

builder.Host.UseSerilog() Causing a container to hang using .net 8 #353

Closed mumby0168 closed 10 months ago

mumby0168 commented 10 months ago

Description My container just hangs, see the below log out from a simple docker run:

[+] Running 1/1
 ⠿ Container serilog-net8-issue-serilog_api-1  Recreated                                                                                 0.1s
Attaching to serilog-net8-issue-serilog_api-1
serilog-net8-issue-serilog_api-1  | [21:20:39 INF] Starting web application

Reproduction

I have put together a small repo here:

https://github.com/mumby0168/serilog-net8-repro

Just run docker-compose up in the root of the solution and notice you cannot hit the route endpoint on the application.

It's a single file:

using Serilog;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();

try
{
    Log.Information("Starting web application");

    var builder = WebApplication.CreateBuilder(args);

    builder.Host.UseSerilog((context, services, configuration) => configuration
            .ReadFrom
            .Configuration(context.Configuration));

    var app = builder.Build();

    app.MapGet("/", () => "Hello World!");

    app.Run();
}
catch (Exception ex)
{
    Log.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
    Log.CloseAndFlush();
}

Expected behavior The app can be setup with logging and serve requests.

Relevant package, tooling and runtime versions

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
  </ItemGroup>
</Project>
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

WORKDIR /app
COPY ./SerilogDockerIssue/SerilogDockerIssue.csproj ./

RUN dotnet restore
COPY ./ ./

WORKDIR ./SerilogDockerIssue

FROM build AS publish
RUN dotnet publish "SerilogDockerIssue.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SerilogDockerIssue.dll"]

Additional context This only occurs inside a container, seems to run fine locally outside of the container.

nblumhardt commented 10 months ago

Hi! There were two issues throwing you off, here.

First, in .NET 8 the default port for ASP.NET Core is 8080, not 80, so your service isn't actually listening on the exposed port.

Second, your call to UseSerilog() overrides the initial logger configuration, and doesn't add any sinks - so Serilog is configured and working, but the messages aren't going anywhere.

You'll need to

  1. Change the compose file to map 5555:8080
  2. Add some sinks to the JSON configuration file, and change CreateLogger() to CreateBootstrapLogger()

Even better than 2 would be to ditch the JSON configuration altogether, and just configure the sinks you need in the initial CreateLogger() call. In that case you don't need CreateBootstrapLogger(), and don't need to pass a callback to UseSerilog(). Simpler and pretty much bulletproof :-)

HTH!

mumby0168 commented 10 months ago

Hi! There were two issues throwing you off, here.

First, in .NET 8 the default port for ASP.NET Core is 8080, not 80, so your service isn't actually listening on the exposed port.

Second, your call to UseSerilog() overrides the initial logger configuration, and doesn't add any sinks - so Serilog is configured and working, but the messages aren't going anywhere.

You'll need to

  1. Change the compose file to map 5555:8080
  2. Add some sinks to the JSON configuration file, and change CreateLogger() to CreateBootstrapLogger()

Brilliant thank you very much @nblumhardt completely through me off that port change, it was a trial and error commented sections of my bootstrapping code out until it started to run some log information, so I've accidentally blamed serilog here.

Thanks for your help, all I needed was the port number change!