Troubleshooting by writing logs at the application start for .NET core app using Serilog in Azure App Service

Azure App service troubleshooting | App start up logs ASP.NET Core | Serilog in .NET Core

Sanjeevi Subramani's photo
Sanjeevi Subramani
·Sep 25, 2020·

2 min read

Troubleshooting by writing logs at the application start for .NET core app using Serilog in Azure App Service

Subscribe to our newsletter and never miss any upcoming articles

Play this article

.NET core 3.1 app may fail to start, and any logging integration implemented in startup using a middleware or anything wont 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.

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) 
** .UseSerilog()**
 .ConfigureAppConfiguration((hostContext, config) 
 var env = hostContext.HostingEnvironment;
 HostBuilderService hostBuilderService = new HostBuilderService(webHostBuilder);
 .UseDefaultServiceProvider((context, options)
 options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
 options.ValidateOnBuild = true;


Did you find this article valuable?

Support LKG for IT by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
Share this