Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Сергей Трегуб committed Feb 27, 2021
1 parent 68e246c commit cafec9d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
22 changes: 14 additions & 8 deletions ProjectTemplates/ReferenceProject/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
Expand All @@ -10,10 +9,14 @@

namespace ReferenceProject
{
public class Program
public class Program
{
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();

try
{
CreateHostBuilder(args).Build().Run();
Expand All @@ -22,14 +25,13 @@ public static int Main(string[] args)
}
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);
}
Log.Logger.Fatal(ex, "An unhandled exception occurred. The application will be closed");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Expand All @@ -43,6 +45,10 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
config.AddEnvironmentVariables();
})
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
)
.ConfigureLogging((context, logging) =>
{
logging.ClearProviders();
Expand Down
6 changes: 6 additions & 0 deletions ProjectTemplates/ReferenceProject/ReferenceProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Exceptions" Version="6.0.0" />
<PackageReference Include="Serilog.Expressions" Version="1.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
Expand Down
3 changes: 3 additions & 0 deletions ProjectTemplates/ReferenceProject/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using ReferenceProject.Configuration;
using ReferenceProject.Filters;
using ReferenceProject.Modules;
using Serilog;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -137,6 +138,8 @@ public void ConfigureProductionContainer(ContainerBuilder builder)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, ILogger<Startup> logger)
{
app.UseSerilogRequestLogging();

// Use an exception handler middleware before any other handlers
// See: https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#unhandled-exceptions-handling
app.UseExceptionHandler();
Expand Down
11 changes: 9 additions & 2 deletions ProjectTemplates/ReferenceProject/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting": "Information",
"System": "Warning"
"System": "Warning",
"Serilog.AspNetCore": "Warning"
}
},
"Filter": [],
Expand All @@ -29,7 +30,13 @@
}
],
"Enrich": [
"FromLogContext"
"FromLogContext",
"WithExceptionDetails",
"WithDefaultDestructurers",
"WithProcessId",
"WithThreadId",
"WithMachineName",
"WithEnvironmentUserName"
]
},
"Products": {
Expand Down

0 comments on commit cafec9d

Please sign in to comment.