Troubleshooting by writing logs at the application start for .NET core app using Serilog in Azure App Service | Azure App Service troubleshooting | Debugging Azure App Service | Dotnet app in Azure App service

{tocify} $title={Table of Contents}

.NET core 3.1 app may fail to start, and any logging integration implemented in startup using middleware or anything won't log the errors for the exception occurring at the app start.

To troubleshoot this, we will write log at the Program.cs and startup.cs using Serilog.

Install Serilog related items from Nuget:

<PackageReference Include=”Serilog” Version=”2.10.0" />
 <PackageReference Include=”Serilog.AspNetCore” Version=”3.4.0"  />
 <PackageReference Include=”Serilog.Sinks.File” Version=”4.1.0"  />

Initialize the Serilog using below hightlighted code in Main method in Program.cs.

And call UseSerilog() method when setting up the HostBuilder.

Thats all we must do for setting up the Serilog for logging at app start up logs. Now you can call the Log.Error or Log.Information from the application it will log it in the file and in console if application called from console. If deployed in Azure App Service this log will come in the LogStream option also.

For App Service log at console feature in Azure App Service refer this article.

For dotnet 3.1

namespace SampleStartupLog
 using System;
 using Serilog;
public class Program
 public static void Main(string[] args)

Log.Logger = new LoggerConfiguration()
 fileSizeLimitBytes: 1_000_000,
 rollOnFileSizeLimit: true,
 shared: true,
 flushToDiskInterval: TimeSpan.FromSeconds(1))
 catch (Exception ex)
 Log.Error(ex.Message + ex.StackTrace);

public static IHostBuilder CreateHostBuilder(string[] args)
 .ConfigureAppConfiguration((hostContext, config)
 var env = hostContext.HostingEnvironment;
 HostBuilderService hostBuilderService = new HostBuilderService(webHostBuilder);
 .UseDefaultServiceProvider((context, options)
 options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
 options.ValidateOnBuild = true;

For Dotnet 6

Similarly, for dotnet 6 do following in Program.cs

using Serilog;

var builder = WebApplication.CreateBuilder(args);


var app = builder.Build();

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



Here we can also do following to write to console.

builder.Host.UseSerilog((hostContext, services, configuration) => {



Thanks 👍👌😄


Previous Post Next Post


post ad 2