Skip to content

Commit cafec9d

Browse files
author
Сергей Трегуб
committed
Improve logging
1 parent 68e246c commit cafec9d

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

ProjectTemplates/ReferenceProject/Program.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Autofac.Extensions.DependencyInjection;
2-
using Microsoft.AspNetCore;
32
using Microsoft.AspNetCore.Hosting;
43
using Microsoft.Extensions.Logging;
54
using Microsoft.Extensions.Configuration;
@@ -10,10 +9,14 @@
109

1110
namespace ReferenceProject
1211
{
13-
public class Program
12+
public class Program
1413
{
1514
public static int Main(string[] args)
1615
{
16+
Log.Logger = new LoggerConfiguration()
17+
.WriteTo.Console()
18+
.CreateBootstrapLogger();
19+
1720
try
1821
{
1922
CreateHostBuilder(args).Build().Run();
@@ -22,14 +25,13 @@ public static int Main(string[] args)
2225
}
2326
catch(Exception ex)
2427
{
25-
var msg = "An unhandled exception occurred. The application will be closed";
26-
Log.Logger?.Fatal(ex, msg);
27-
if( Log.Logger == null )
28-
{
29-
Console.WriteLine(msg + Environment.NewLine + ex);
30-
}
28+
Log.Logger.Fatal(ex, "An unhandled exception occurred. The application will be closed");
3129
return 1;
3230
}
31+
finally
32+
{
33+
Log.CloseAndFlush();
34+
}
3335
}
3436

3537
public static IHostBuilder CreateHostBuilder(string[] args) =>
@@ -43,6 +45,10 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
4345

4446
config.AddEnvironmentVariables();
4547
})
48+
.UseSerilog((context, services, configuration) => configuration
49+
.ReadFrom.Configuration(context.Configuration)
50+
.ReadFrom.Services(services)
51+
)
4652
.ConfigureLogging((context, logging) =>
4753
{
4854
logging.ClearProviders();

ProjectTemplates/ReferenceProject/ReferenceProject.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@
3939
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
4040
<PackageReference Include="Serilog" Version="2.10.0" />
4141
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
42+
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
43+
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1" />
44+
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
45+
<PackageReference Include="Serilog.Exceptions" Version="6.0.0" />
4246
<PackageReference Include="Serilog.Expressions" Version="1.0.0" />
47+
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.1.0" />
48+
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
4349
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
4450
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
4551
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />

ProjectTemplates/ReferenceProject/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using ReferenceProject.Configuration;
2020
using ReferenceProject.Filters;
2121
using ReferenceProject.Modules;
22+
using Serilog;
2223
using System.IO;
2324
using System.Text.Json;
2425
using System.Text.Json.Serialization;
@@ -137,6 +138,8 @@ public void ConfigureProductionContainer(ContainerBuilder builder)
137138
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
138139
public void Configure(IApplicationBuilder app, ILogger<Startup> logger)
139140
{
141+
app.UseSerilogRequestLogging();
142+
140143
// Use an exception handler middleware before any other handlers
141144
// See: https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#unhandled-exceptions-handling
142145
app.UseExceptionHandler();

ProjectTemplates/ReferenceProject/appsettings.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"Override": {
66
"Microsoft": "Warning",
77
"Microsoft.Hosting": "Information",
8-
"System": "Warning"
8+
"System": "Warning",
9+
"Serilog.AspNetCore": "Warning"
910
}
1011
},
1112
"Filter": [],
@@ -29,7 +30,13 @@
2930
}
3031
],
3132
"Enrich": [
32-
"FromLogContext"
33+
"FromLogContext",
34+
"WithExceptionDetails",
35+
"WithDefaultDestructurers",
36+
"WithProcessId",
37+
"WithThreadId",
38+
"WithMachineName",
39+
"WithEnvironmentUserName"
3340
]
3441
},
3542
"Products": {

0 commit comments

Comments
 (0)