Skip to content

Commit a95aed3

Browse files
committed
Fixes #98 - UseSerilog() goodness
1 parent bff2a88 commit a95aed3

16 files changed

+307
-72
lines changed

samples/SimpleWebAPISample/Program.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
63
using Microsoft.AspNetCore;
74
using Microsoft.AspNetCore.Hosting;
85
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.Logging;
10-
116
using Serilog;
127

138
namespace SimpleWebAPISample
149
{
1510
public class Program
1611
{
17-
public static void Main(string[] args)
12+
public static int Main(string[] args)
1813
{
14+
var configuration = new ConfigurationBuilder()
15+
.SetBasePath(Directory.GetCurrentDirectory())
16+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
17+
.Build();
18+
1919
Log.Logger = new LoggerConfiguration()
20+
.ReadFrom.Configuration(configuration)
2021
.Enrich.FromLogContext()
21-
.WriteTo.LiterateConsole()
22+
.WriteTo.Console()
2223
.CreateLogger();
23-
24-
Log.Information("Getting the motors running...");
2524

26-
BuildWebHost(args).Run();
27-
}
25+
try
26+
{
27+
Log.Information("Getting the motors running...");
2828

29-
public static IWebHost BuildWebHost(string[] args) =>
30-
WebHost.CreateDefaultBuilder(args)
31-
.UseStartup<Startup>()
32-
.ConfigureLogging(log =>
33-
{
34-
log.SetMinimumLevel(LogLevel.Information);
35-
log.AddSerilog(logger: Log.Logger, dispose: true);
36-
})
37-
.Build();
29+
var host = WebHost.CreateDefaultBuilder(args)
30+
.UseStartup<Startup>()
31+
.UseConfiguration(configuration)
32+
.UseSerilog()
33+
.Build();
34+
35+
host.Run();
36+
37+
return 0;
38+
}
39+
catch (Exception ex)
40+
{
41+
Log.Fatal(ex, "Unhandled exception");
42+
return 1;
43+
}
44+
finally
45+
{
46+
Log.CloseAndFlush();
47+
}
48+
}
3849
}
3950
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:51968/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"SimpleWebAPISample": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:51972/"
25+
}
26+
}
27+
}

samples/SimpleWebAPISample/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The project was created with the following steps.
66
```
77
dotnet new webapi
88
dotnet add package Serilog.Extensions.Logging
9-
dotnet add package Serilog.Sinks.Literate
9+
dotnet add package Serilog.Sinks.Console
1010
```
1111

1212
* Extend the logging to include Serilog. See `Program.cs`
@@ -17,4 +17,4 @@ dotnet add package Serilog.Sinks.Literate
1717
})
1818
```
1919

20-
* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline.
20+
* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline.

samples/SimpleWebAPISample/SimpleWebAPISample.csproj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.0</TargetFramework>
44
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
8+
</ItemGroup>
9+
510
<ItemGroup>
611
<Folder Include="wwwroot\" />
712
</ItemGroup>
13+
814
<ItemGroup>
915
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
10-
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.0" />
11-
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.0" />
16+
<PackageReference Include="Serilog.Sinks.Console" Version="3.0.1" />
17+
<PackageReference Include="Serilog" Version="2.5.0" />
18+
<PackageReference Include="Serilog.Settings.Configuration" Version="2.4.0" />
1219
</ItemGroup>
20+
1321
<ItemGroup>
1422
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
1523
</ItemGroup>
16-
</Project>
24+
25+
</Project>

samples/SimpleWebAPISample/appsettings.json

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
{
2-
"Logging": {
3-
"IncludeScopes": false,
4-
"Debug": {
5-
"LogLevel": {
6-
"Default": "Warning"
7-
}
8-
},
9-
"Console": {
10-
"LogLevel": {
11-
"Default": "Warning"
2+
"Serilog": {
3+
"MinimumLevel": {
4+
"Default": "Debug",
5+
"Override": {
6+
"Microsoft": "Information",
7+
"System": "Warning"
128
}
139
}
1410
}

samples/SimpleWebSample/Program.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
4-
using System.Linq;
5-
using System.Threading.Tasks;
63
using Microsoft.AspNetCore;
74
using Microsoft.AspNetCore.Hosting;
85
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.Logging;
10-
116
using Serilog;
12-
137
namespace SimpleWebSample
148
{
159
public class Program
1610
{
17-
public static void Main(string[] args)
11+
public static int Main(string[] args)
1812
{
13+
var configuration = new ConfigurationBuilder()
14+
.SetBasePath(Directory.GetCurrentDirectory())
15+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
16+
.Build();
17+
1918
Log.Logger = new LoggerConfiguration()
19+
.ReadFrom.Configuration(configuration)
2020
.Enrich.FromLogContext()
21-
.WriteTo.LiterateConsole()
21+
.WriteTo.Console()
2222
.CreateLogger();
23-
24-
Log.Information("Getting the motors running...");
2523

26-
BuildWebHost(args).Run();
27-
}
24+
try
25+
{
26+
Log.Information("Getting the motors running...");
2827

29-
public static IWebHost BuildWebHost(string[] args) =>
30-
WebHost.CreateDefaultBuilder(args)
31-
.UseStartup<Startup>()
32-
.ConfigureLogging(log =>
33-
{
34-
log.SetMinimumLevel(LogLevel.Information);
35-
log.AddSerilog(logger: Log.Logger, dispose: true);
36-
})
37-
.Build();
28+
var host = WebHost.CreateDefaultBuilder(args)
29+
.UseStartup<Startup>()
30+
.UseConfiguration(configuration)
31+
.UseSerilog()
32+
.Build();
33+
34+
host.Run();
35+
36+
return 0;
37+
}
38+
catch (Exception ex)
39+
{
40+
Log.Fatal(ex, "Unhandled exception");
41+
return 1;
42+
}
43+
finally
44+
{
45+
Log.CloseAndFlush();
46+
}
47+
}
3848
}
3949
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:51965/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"SimpleWebSample": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:51966/"
25+
}
26+
}
27+
}

samples/SimpleWebSample/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The project was created with the following steps.
66
```
77
dotnet new web
88
dotnet add package Serilog.Extensions.Logging
9-
dotnet add package Serilog.Sinks.Literate
9+
dotnet add package Serilog.Sinks.Console
1010
```
1111

1212
* Extend the logging to include Serilog. See `Program.cs`
@@ -17,4 +17,4 @@ dotnet add package Serilog.Sinks.Literate
1717
})
1818
```
1919

20-
* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline.
20+
* Logging can then used directly to Serilog or via the `Microsoft.Extensions.Logging` pipeline.

samples/SimpleWebSample/SimpleWebSample.csproj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.0</TargetFramework>
44
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
8+
</ItemGroup>
9+
510
<ItemGroup>
611
<Folder Include="wwwroot\" />
712
</ItemGroup>
13+
814
<ItemGroup>
915
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
10-
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.0" />
11-
<PackageReference Include="Serilog.Sinks.Literate" Version="3.0.0" />
16+
<PackageReference Include="Serilog.Sinks.Console" Version="3.0.1" />
17+
<PackageReference Include="Serilog" Version="2.5.0" />
18+
<PackageReference Include="Serilog.Settings.Configuration" Version="2.4.0" />
19+
1220
</ItemGroup>
21+
1322
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"LogLevel": {
5+
"Default": "Debug",
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)