Reputation: 25
We are using Serilog connected to Graylog to manage our logging and that works great for runtime issues, but the problem we have is that the service gets initialized in Startup.cs
and is not available in Program.cs
, where we might be getting an error (IIS shows 500 returned from Kestrel, but nothing in our logs).
What's the recommended way to handle this - there seems to be an option for stdout in the ASP Core module's web.config
, but not sure if that will provide the information I need. The other option seems to be adding a separate logging code in Program.cs
, but that feels like an unnecessary workaround, as in there is probably a better default way.
Upvotes: 1
Views: 1654
Reputation: 31832
Setting up all logging in Program.cs
is possible, and recommended with Serilog. From the Serilog.AspNetCore README:
public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting web host");
BuildWebHost(args).Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog()
.Build();
}
Upvotes: 4