serilog / serilog-aspnetcore

Serilog integration for ASP.NET Core
Apache License 2.0
1.32k stars 209 forks source link

Serilog.AspNetCore not working with F# #369

Closed catamounttechnology closed 7 months ago

catamounttechnology commented 7 months ago

Description I have an F# asp.net 8 api. The application has been developed in a Windows 10 machine initially, and log was working as expected. Now I have a new machine with Windows 11 and in there nothing is logged to the console. So I made some simple tests and it looks like the issue is occurring only for F#

Reproduction code in https://github.com/catamounttechnology/WebApplication1 In visual studio 2022:

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

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

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

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

namespace WebApplication3

nowarn "20"

open Microsoft.AspNetCore.Builder open Microsoft.Extensions.DependencyInjection open Microsoft.Extensions.Hosting open Serilog

module Program = [] let main args =

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

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

            let builder = WebApplication.CreateBuilder(args)

            builder.Services.AddControllers()

            let app = builder.Build()

            app.UseHttpsRedirection()

            app.UseAuthorization()
            app.MapControllers()

            app.Run()
            0
        with | ex -> 
            Log.Fatal(ex, "Application terminated unexpectedly")
            -1
    finally Log.CloseAndFlush()

run the C# app -> The console shows the log message as expected
![image](https://github.com/serilog/serilog-aspnetcore/assets/164097140/55070c13-0320-4d0c-b42f-605941342ff7)

run the F# app. No log message is printed to the console
![image](https://github.com/serilog/serilog-aspnetcore/assets/164097140/09cbaae8-2245-4f11-aa40-dcb872a25cb7)

**Expected behavior**
The F# application log should be the same as the C# one

**Relevant package, tooling and runtime versions**

Processor   13th Gen Intel(R) Core(TM) i7-13700H   2.40 GHz
Installed RAM   48.0 GB (47.7 GB usable)
System type 64-bit operating system, x64-based processor

Edition Windows 11 Pro
Version 23H2
Installed on    ‎20/‎03/‎2024
OS build    22631.3374
Experience  Windows Feature Experience Pack 1000.22688.1000.0

C# project
net8.0 enable enable

F# project
net8.0

appsettings.json (both)

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" }


appsettings.Development.json (both)

{ "DetailedErrors": true, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } } }

nblumhardt commented 7 months ago

Hi! I can't spot anything at first glance; does SelfLog.Enable() produce anything useful?

No known issues using F# with Serilog that I'm aware of.

catamounttechnology commented 7 months ago

I tried SelfLog.Enable with a delegate, the delegate is never called

nblumhardt commented 7 months ago

Thanks for the follow-up. Will need some investigation. It would also be useful to try reproducing this on another machine, since it's uncommon but not unheard of for tooling install problems to cause this kind of thing.

catamounttechnology commented 7 months ago

Unfortunately I don't have a second machine with the same configuration.

catamounttechnology commented 7 months ago

As a workaround I moved all my F# code in a library. The application starter is now written in C#, while the full business logic is in the F# library. With this, logging works. Feel free to close this issue, even if not solved.

nblumhardt commented 7 months ago

Glad you were able to work around it. I'll close this, and reopen if anyone else reports something similar 👍