Skip to content

Commit 47a6eb8

Browse files
Merge pull request #179 from AntonioFalcao/feature/graphql-4
Adding Serilog
2 parents ef752c9 + 8088b11 commit 47a6eb8

10 files changed

Lines changed: 136 additions & 34 deletions

File tree

Directory.Build.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<AutoMapper_DependencyInjection_Version>8.1.1</AutoMapper_DependencyInjection_Version>
2525
<AutoMapper_Version>10.1.1</AutoMapper_Version>
2626

27+
<!--Serilog-->
28+
<Serilog_Version>4.1.0</Serilog_Version>
29+
2730
<!--HealthChecks-->
2831
<HealthChecks_Version>5.0.1</HealthChecks_Version>
2932

src/Dotnet6.GraphQL4.Store.WebAPI/Dotnet6.GraphQL4.Store.WebAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="$(HealthChecks_Version)" />
99
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="$(Microsoft_HealthChecks_EntityFrameworkCore_Version)" />
10+
<PackageReference Include="Serilog.AspNetCore" Version="$(Serilog_Version)" />
1011
</ItemGroup>
1112

1213
<ItemGroup>

src/Dotnet6.GraphQL4.Store.WebAPI/Graphs/Extensions/DependencyInjection/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ public static IApplicationBuilder UseApplicationGraphQL<TSchema>(this IApplicati
1313
.UseGraphQLWebSockets<TSchema>()
1414
.UseGraphQL<TSchema>()
1515
.UseGraphQLPlayground(
16-
new()
16+
options: new()
1717
{
1818
BetaUpdates = true,
1919
RequestCredentials = RequestCredentials.Omit,
2020
HideTracingResponse = false,
21-
2221
EditorCursorShape = EditorCursorShape.Line,
2322
EditorTheme = EditorTheme.Dark,
2423
EditorFontSize = 14,

src/Dotnet6.GraphQL4.Store.WebAPI/Program.cs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,67 @@
1+
using System;
2+
using System.IO;
13
using System.Threading.Tasks;
24
using Microsoft.AspNetCore.Hosting;
35
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.Extensions.Configuration;
47
using Microsoft.Extensions.DependencyInjection;
58
using Microsoft.Extensions.Hosting;
9+
using Serilog;
610

711
namespace Dotnet6.GraphQL4.Store.WebAPI
812
{
913
public static class Program
1014
{
1115
private static IHostBuilder CreateHostBuilder(string[] args)
1216
=> Host.CreateDefaultBuilder(args)
17+
.UseSerilog(
18+
(context, services, configuration)
19+
=> configuration
20+
.ReadFrom.Configuration(context.Configuration)
21+
.ReadFrom.Services(services)
22+
.Enrich.FromLogContext()
23+
.WriteTo.Console())
1324
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
14-
.UseDefaultServiceProvider((context, options) =>
15-
{
16-
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
17-
options.ValidateOnBuild = true;
18-
});
19-
25+
.UseDefaultServiceProvider(
26+
(context, options) =>
27+
{
28+
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
29+
options.ValidateOnBuild = true;
30+
});
31+
2032
public static async Task Main(string[] args)
2133
{
22-
var host = CreateHostBuilder(args).Build();
23-
await host.MigrateDatabaseAsync();
24-
await host.RunAsync();
34+
var configuration = new ConfigurationBuilder()
35+
.SetBasePath(Directory.GetCurrentDirectory())
36+
.AddJsonFile(
37+
path: "appsettings.json",
38+
optional: false,
39+
reloadOnChange: true)
40+
.AddJsonFile(
41+
path: $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
42+
optional: true,
43+
reloadOnChange: true)
44+
.Build();
45+
46+
Log.Logger = new LoggerConfiguration()
47+
.ReadFrom.Configuration(configuration)
48+
.CreateLogger();
49+
50+
try
51+
{
52+
var host = CreateHostBuilder(args).Build();
53+
await host.MigrateDatabaseAsync();
54+
await host.RunAsync();
55+
Log.Information("Stopped cleanly");
56+
}
57+
catch (Exception ex)
58+
{
59+
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
60+
}
61+
finally
62+
{
63+
Log.CloseAndFlush();
64+
}
2565
}
2666

2767
private static async Task MigrateDatabaseAsync(this IHost host)

src/Dotnet6.GraphQL4.Store.WebAPI/appsettings.Development.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
"Readiness": "/health/ready",
1111
"UI": "/healthz"
1212
},
13-
"Logging": {
14-
"LogLevel": {
15-
"Default": "Information",
16-
"Microsoft": "Information",
17-
"Microsoft.Hosting.Lifetime": "Information"
13+
"Serilog": {
14+
"MinimumLevel": {
15+
"Default": "Debug",
16+
"Override": {
17+
"Microsoft": "Information",
18+
"Microsoft.Hosting.Lifetime": "Information"
19+
}
1820
}
1921
}
2022
}

src/Dotnet6.GraphQL4.Store.WebAPI/appsettings.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
"Readiness": "/health/ready",
1414
"UI": "/healthz"
1515
},
16-
"Logging": {
17-
"LogLevel": {
16+
"Serilog": {
17+
"MinimumLevel": {
1818
"Default": "Information",
19-
"Microsoft": "Warning",
20-
"Microsoft.Hosting.Lifetime": "Information"
19+
"Override": {
20+
"Microsoft": "Information",
21+
"Microsoft.Hosting.Lifetime": "Information"
22+
}
2123
}
2224
},
2325
"AllowedHosts": "*"

src/Dotnet6.GraphQL4.Store.WebMVC/Dotnet6.GraphQL4.Store.WebMVC.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="$(HealthChecks_Version)" />
99
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="$(HealthChecks_Version)" />
10+
<PackageReference Include="Serilog.AspNetCore" Version="$(Serilog_Version)" />
1011
</ItemGroup>
1112

1213
<ItemGroup>
Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,66 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
14
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Configuration;
26
using Microsoft.Extensions.Hosting;
7+
using Serilog;
38

49
namespace Dotnet6.GraphQL4.Store.WebMVC
510
{
611
public static class Program
712
{
813
private static IHostBuilder CreateHostBuilder(string[] args)
914
=> Host.CreateDefaultBuilder(args)
10-
.ConfigureWebHostDefaults(webBuilder
11-
=> webBuilder.UseStartup<Startup>());
15+
.UseSerilog(
16+
(context, services, configuration)
17+
=> configuration
18+
.ReadFrom.Configuration(context.Configuration)
19+
.ReadFrom.Services(services)
20+
.Enrich.FromLogContext()
21+
.WriteTo.Console())
22+
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
23+
.UseDefaultServiceProvider(
24+
(context, options) =>
25+
{
26+
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
27+
options.ValidateOnBuild = true;
28+
});
1229

13-
public static void Main(string[] args)
14-
=> CreateHostBuilder(args).Build().Run();
30+
public static async Task Main(string[] args)
31+
{
32+
var configuration = new ConfigurationBuilder()
33+
.SetBasePath(Directory.GetCurrentDirectory())
34+
.AddJsonFile(
35+
path: "appsettings.json",
36+
optional: false,
37+
reloadOnChange: true)
38+
.AddJsonFile(
39+
path: $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
40+
optional: true,
41+
reloadOnChange: true)
42+
.Build();
43+
44+
Log.Logger = new LoggerConfiguration()
45+
.ReadFrom.Configuration(configuration)
46+
.CreateLogger();
47+
48+
try
49+
{
50+
await CreateHostBuilder(args)
51+
.Build()
52+
.RunAsync();
53+
54+
Log.Information("Stopped cleanly");
55+
}
56+
catch (Exception ex)
57+
{
58+
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
59+
}
60+
finally
61+
{
62+
Log.CloseAndFlush();
63+
}
64+
}
1565
}
1666
}

src/Dotnet6.GraphQL4.Store.WebMVC/appsettings.Development.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
"Readiness": "/health/ready",
66
"UI": "/healthz"
77
},
8-
"Logging": {
9-
"LogLevel": {
10-
"Default": "Information",
11-
"Microsoft": "Warning",
12-
"Microsoft.Hosting.Lifetime": "Information"
8+
"Serilog": {
9+
"MinimumLevel": {
10+
"Default": "Debug",
11+
"Override": {
12+
"Microsoft": "Information",
13+
"Microsoft.Hosting.Lifetime": "Information"
14+
}
1315
}
1416
}
15-
}
17+
}

src/Dotnet6.GraphQL4.Store.WebMVC/appsettings.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"Readiness": "/health/ready",
99
"UI": "/healthz"
1010
},
11-
"Logging": {
12-
"LogLevel": {
11+
"Serilog": {
12+
"MinimumLevel": {
1313
"Default": "Information",
14-
"Microsoft": "Warning",
15-
"Microsoft.Hosting.Lifetime": "Information"
14+
"Override": {
15+
"Microsoft": "Information",
16+
"Microsoft.Hosting.Lifetime": "Information"
17+
}
1618
}
1719
},
1820
"AllowedHosts": "*"

0 commit comments

Comments
 (0)