Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseSerilog support for .net 7 CreateApplicationBuilder and HostApplicationBuilder #1855

Closed
kris-g opened this issue Feb 8, 2023 · 9 comments

Comments

@kris-g
Copy link

kris-g commented Feb 8, 2023

Are there any plans to implement another UseSerilog extension method for the new application bootstrapper method from .net 7?

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.host.createapplicationbuilder

dotnet/runtime#61634

@dezren39
Copy link

an interesting aspect is the global namespace program.cs file. i'd like if we could just builder.Logger.UseSerilog and then add configuration from there, all within builder.Logger. No more redefining Log constant at top except for logging before builder.build() and no need for Host. In most cases I'd guess it'd be just changing from 'Host' to 'Logging' in user code, but I think there is more going on in the backend?

@dezren39
Copy link

it's also unclear if/when a new console app will come out to match the new webapplication. this may be something to plan for as well.

also i think it's .net6 and up support this

@SF-Simon
Copy link

SF-Simon commented Mar 6, 2023

At the same time, I hope to consider the part of MAUI, which comes from: Microsoft.Maui.Hosting.MauiAppBuilder.

thx.

At present, I have finished my work in this way. Code:

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseSerilog(loggerConfiguration =>
            {
                var newLoggerConfiguration = loggerConfiguration.WriteTo.Console();
                return newLoggerConfiguration;
            })
...
        return builder.Build();
    }
}

public static class MauiAppBuilderExtensions
{

    public static MauiAppBuilder UseSerilog(this MauiAppBuilder builder, Func<LoggerConfiguration, LoggerConfiguration> options)
    {
        var config = options(new LoggerConfiguration());
        Log.Logger = config.CreateLogger();
        return builder;
    }

}

@nblumhardt
Copy link
Member

I think this went into Serilog.Extensions.Hosting last week: serilog/serilog-extensions-hosting#70 - should now be on NuGet in the lastest -dev version of that package.

Please let me know if it's not a match. Thanks!

@scottmwyant
Copy link

Can we revisit this issue? It's not been fully addressed.

We are expecting to see an extension method on the HostApplicationBuilder class, which comes from Host.CreateApplicationBuilder() but right now it's only available on HostBuilder.

Reference: SerilogHostBuilderExtensions.cs.

@silkfire
Copy link

@nblumhardt Any plans to add the above discussed extension method?

@nblumhardt
Copy link
Member

@silkfire I think builder.Services.AddSerilog(...) should cover every current application/builder model; does that one not work in your scenario?

@silkfire
Copy link

Sure, as long as it's made clear in the documentation that UseSerilog cannot be used anymore with this scenario. Otherwise it's confusing why there's no equivalent for it when using minimal APIs.

@nblumhardt
Copy link
Member

👍 raised serilog/serilog-aspnetcore#368 - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants