最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Serilog does not work using appsettings.json when deployed to App Service - Stack Overflow

programmeradmin1浏览0评论

I use Serilog for logging and want to write to Console and Application Insights.

Everything works fine on localhost. I see the logs in Console. But when I deploy it to Azure the App failed to start: "HTTP Error 500.30 - ASP.NET Core app failed to start".

This is my configuration in appsettings.Development.json:

"Serilog": {
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.ApplicationInsights"],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"System": "Warning",
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} ({SourceContext}){NewLine}{Exception}"
}
},
{
"Name": "ApplicationInsights",
"Args": {
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level} {Message}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}

And this is my configuration in program.cs:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateBootstrapLogger();
builder.Host
        .UseSerilog((context, loggerConfiguration) =>
            loggerConfiguration.ReadFrom.Configuration(context.Configuration));

When I do not use appsettings and configure it in program.cs like so everything works fine and I see log entries in traces table:

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information() 
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) 
.WriteTo.Console() 
.WriteTo.ApplicationInsights( builder.Configuration["ApplicationInsights.InstrumentationKey"], TelemetryConverter.Traces) 
.Enrich.FromLogContext() 
.CreateLogger(); 
builder.Host.UseSerilog();

I use Core 8 Web API.

Any idea? :-)

I use Serilog for logging and want to write to Console and Application Insights.

Everything works fine on localhost. I see the logs in Console. But when I deploy it to Azure the App failed to start: "HTTP Error 500.30 - ASP.NET Core app failed to start".

This is my configuration in appsettings.Development.json:

"Serilog": {
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.ApplicationInsights"],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"System": "Warning",
"Microsoft": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} ({SourceContext}){NewLine}{Exception}"
}
},
{
"Name": "ApplicationInsights",
"Args": {
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level} {Message}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}

And this is my configuration in program.cs:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateBootstrapLogger();
builder.Host
        .UseSerilog((context, loggerConfiguration) =>
            loggerConfiguration.ReadFrom.Configuration(context.Configuration));

When I do not use appsettings and configure it in program.cs like so everything works fine and I see log entries in traces table:

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information() 
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) 
.WriteTo.Console() 
.WriteTo.ApplicationInsights( builder.Configuration["ApplicationInsights.InstrumentationKey"], TelemetryConverter.Traces) 
.Enrich.FromLogContext() 
.CreateLogger(); 
builder.Host.UseSerilog();

I use Core 8 Web API.

Any idea? :-)

Share Improve this question asked Mar 3 at 8:52 Uwe BeckerUwe Becker 1237 bronze badges 4
  • Hi Uwe Becker, could you kindly check this thread first, then check what happened during the startup. – Jason Commented Mar 3 at 12:03
  • You seem to be assuming Serilog is the cause. But until you've actually debugged the reason for the failure, you probably shouldn't make that assumption. There's ways to diagnose app startup failures you'll need to look into. Remember, when you get generic error messages like "X failed" then you can often dig deeper to find the underlying reason for the failure. – mason Commented Mar 3 at 15:07
  • Hi mason. Not Serilog is the cause. As I described Serilog without appsettings.json is working fine. – Uwe Becker Commented Mar 3 at 21:54
  • Your appsetting.json configuration doesn't line up with the configuration you did via C#. In C#, you're not doing WithMachineName or WithThreadId enrichers. Other than checking the actual error for this, I'd start by modifying your configuration to match the C#, helping you find the issue via process of elimination. – mason Commented Mar 3 at 21:58
Add a comment  | 

1 Answer 1

Reset to default 0

OK got it! Console in AppService told me the truth. The NuGet package Serilog.Sinks.AzureApp was missing. Works like a charm now with appsettings. Thanks for your support!

发布评论

评论列表(0)

  1. 暂无评论