Skip to content

Commit

Permalink
Log unhandled exceptions
Browse files Browse the repository at this point in the history
Issue #8
  • Loading branch information
Сергей Трегуб committed Feb 23, 2021
1 parent 99dc1c6 commit fda0c1b
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions ProjectTemplates/ReferenceProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@
using Serilog;
using Microsoft.Extensions.Hosting;
using System.IO;
using System;

namespace ReferenceProject
{
public class Program
{
public static void Main(string[] args)
public static int Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
try
{
CreateHostBuilder(args).Build().Run();

return 0;
}
catch(Exception ex)
{
var msg = "An unhandled exception occurred. The application will be closed";
Log.Logger?.Fatal(ex, msg);
if( Log.Logger == null )
{
Console.WriteLine(msg + Environment.NewLine + ex);
}
return 1;
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Expand All @@ -31,17 +47,12 @@ public static void Main(string[] args)
{
logging.ClearProviders();

/*
* You can use a global logger as this, but I don't recommend this way
* More information: https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#logging
// https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#logging
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(context.Configuration)
.CreateLogger();
*/

logging.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(context.Configuration)
.CreateLogger());
logging.AddSerilog(Log.Logger);
})
.ConfigureWebHostDefaults(webBuilder =>
{
Expand Down

0 comments on commit fda0c1b

Please sign in to comment.