serilog / serilog-aspnetcore

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

Migrations generating an error when serilog enabled. #288

Closed rcoady70 closed 2 years ago

rcoady70 commented 2 years ago

I get the following error when try to add a migration to a new core application. Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException: Exception of type 'Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException' was thrown. at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.OnNext(KeyValuePair`2 value) at System.Diagnostics.DiagnosticListener.Write(String name, Object value) at Microsoft.Extensions.Hosting.HostBuilder.Build() at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()

When I remove serilog configuration it works correctly. I had a few "WriteTo" enabled (File, Seq) once I remove these from the APPSETTINGS.JSON it works again.

So it seems to be directly related if there are any WriteTo elements it fails. Remove the writeto element and it works. "Serilog": { "Using": [ "Serilog.Sinks.Console"], "MinimumLevel": { "Default": "Information", "Override": { "RC": "Information", "Microsoft": "Information", "System": "Information" } }, // https://github.com/serilog/serilog/wiki/Provided-Sinks. "WriteTo": [ { "Name": "Console" } ] }

skomis-mm commented 2 years ago

Hi @rcoady70 , check workarounds here.

rcoady70 commented 2 years ago

Thanks for the response.

I had tried this already but did not work... Setup is simple so I do not think any other factors at play . New .net core 6 application with Identity added, the migration to update the identity tables just fails.

skomis-mm commented 2 years ago

@rcoady70 can you provide minimal example? I've just created .net 6 web app + individual accounts from template, with Serilog added. But could not reproduce the problem

rcoady70 commented 2 years ago

Thanks Sergey,

I created a git repo : https://github.com/rcoady70/Serilog.Mig.Issue.git

gh repo clone rcoady70/Serilog.Mig.Issue

Basically if you comment out the write to line in the appsettings file it will work.

[image: image.png]

On Fri, 4 Feb 2022 at 16:38, Sergey Komisarchik @.***> wrote:

@rcoady70 https://github.com/rcoady70 can you provide minimal example? I've just created .net 6 web app + individual accounts from template, with Serilog added. But could not reproduce the problem

— Reply to this email directly, view it on GitHub https://github.com/serilog/serilog-aspnetcore/issues/288#issuecomment-1030154815, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQGZOII45SHMGD7N4HZOKDDUZP6J5ANCNFSM5NPT6KVA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

skomis-mm commented 2 years ago

@rcoady70 , I cloned project and executed

dotnet ef migrations add Initial
dotnet ef database update

Migrations were generated and applied successfully. The message you see Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException doesn't affect the behavior. It comes from implementation details of how EF tools handle top-level statements.

You can suppress this message by adding additional catch block:

catch (Exception ex) when (ex.GetType().Name == "StopTheHostException")
{
}
rcoady70 commented 2 years ago

Thanks for taking the time to review.

I assume that that catch block is added to the migration code.

On Mon, 7 Feb 2022 at 18:39, Sergey Komisarchik @.***> wrote:

@rcoady70 https://github.com/rcoady70 , I cloned project and executed

dotnet ef migrations add Initialdotnet ef database update

Migrations were generated and applied successfully. The message you see Microsoft.Extensions.Hosting.HostFactoryResolver+HostingListener+StopTheHostException doesn't affect the behavior. It comes from implementation details of how EF tools handle top-level statements.

You can suppress this message by adding additional catch block:

catch (Exception ex) when (ex.GetType().Name == "StopTheHostException") { }

— Reply to this email directly, view it on GitHub https://github.com/serilog/serilog-aspnetcore/issues/288#issuecomment-1031793313, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQGZOIOVFATUBSCHQNKGVW3U2AGW7ANCNFSM5NPT6KVA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

nblumhardt commented 2 years ago

(Just a quick note, I think it'd be in your program/main entrypoint.)

ataccounts commented 2 years ago

Nice work guys. Thank you for the details here. Immediately helped with the catch.