Skip to content

Commit bf4ae07

Browse files
authored
Merge pull request #306 from serilog/cleanup
enable nullable
2 parents f56c26c + 55d2469 commit bf4ae07

20 files changed

+980
-1025
lines changed

Directory.Build.props

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project>
2+
<PropertyGroup>
3+
<LangVersion>latest</LangVersion>
4+
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
5+
<SignAssembly>true</SignAssembly>
6+
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
7+
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
8+
<CheckEolTargetFramework>false</CheckEolTargetFramework>
9+
<Nullable>enable</Nullable>
10+
<ImplicitUsings>enable</ImplicitUsings>
11+
</PropertyGroup>
12+
</Project>

global.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"sdk": {
33
"allowPrerelease": false,
4-
"version": "5.0.201",
4+
"version": "6.0.401",
55
"rollForward": "latestFeature"
66
}
77
}
+33-40
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Threading;
6-
using System.Threading.Tasks;
1+
using System.Diagnostics;
72
using Microsoft.AspNetCore.Mvc;
8-
using Microsoft.Extensions.Logging;
93
using Sample.Models;
104
using Serilog;
115

12-
namespace Sample.Controllers
6+
namespace Sample.Controllers;
7+
8+
public class HomeController : Controller
139
{
14-
public class HomeController : Controller
10+
static int _callCount;
11+
12+
readonly ILogger<HomeController> _logger;
13+
readonly IDiagnosticContext _diagnosticContext;
14+
15+
public HomeController(ILogger<HomeController> logger, IDiagnosticContext diagnosticContext)
16+
{
17+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
18+
_diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
19+
}
20+
21+
public IActionResult Index()
22+
{
23+
_logger.LogInformation("Hello, world!");
24+
25+
_diagnosticContext.Set("IndexCallCount", Interlocked.Increment(ref _callCount));
26+
27+
return View();
28+
}
29+
30+
public IActionResult Privacy()
31+
{
32+
throw new InvalidOperationException("Something went wrong.");
33+
}
34+
35+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
36+
public IActionResult Error()
1537
{
16-
static int _callCount;
17-
18-
readonly ILogger<HomeController> _logger;
19-
readonly IDiagnosticContext _diagnosticContext;
20-
21-
public HomeController(ILogger<HomeController> logger, IDiagnosticContext diagnosticContext)
22-
{
23-
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
24-
_diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
25-
}
26-
27-
public IActionResult Index()
28-
{
29-
_logger.LogInformation("Hello, world!");
30-
31-
_diagnosticContext.Set("IndexCallCount", Interlocked.Increment(ref _callCount));
32-
33-
return View();
34-
}
35-
36-
public IActionResult Privacy()
37-
{
38-
throw new InvalidOperationException("Something went wrong.");
39-
}
40-
41-
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
42-
public IActionResult Error()
43-
{
44-
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
45-
}
38+
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
4639
}
47-
}
40+
}
+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System;
1+
namespace Sample.Models;
22

3-
namespace Sample.Models
3+
public class ErrorViewModel
44
{
5-
public class ErrorViewModel
6-
{
7-
public string RequestId { get; set; }
5+
public string? RequestId { get; set; }
86

9-
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10-
}
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
118
}

samples/Sample/Program.cs

+36-45
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Hosting;
6-
using Microsoft.Extensions.Configuration;
7-
using Microsoft.Extensions.Hosting;
8-
using Microsoft.Extensions.Logging;
91
using Serilog;
102

11-
namespace Sample
3+
namespace Sample;
4+
5+
public static class Program
126
{
13-
public static class Program
7+
public static int Main(string[] args)
148
{
15-
public static int Main(string[] args)
16-
{
17-
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the
18-
// logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been
19-
// set up successfully.
20-
Log.Logger = new LoggerConfiguration()
21-
.WriteTo.Console()
22-
.CreateBootstrapLogger();
23-
24-
Log.Information("Starting up!");
9+
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the
10+
// logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been
11+
// set up successfully.
12+
Log.Logger = new LoggerConfiguration()
13+
.WriteTo.Console()
14+
.CreateBootstrapLogger();
2515

26-
try
27-
{
28-
CreateHostBuilder(args).Build().Run();
16+
Log.Information("Starting up!");
2917

30-
Log.Information("Stopped cleanly");
31-
return 0;
32-
}
33-
catch (Exception ex)
34-
{
35-
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
36-
return 1;
37-
}
38-
finally
39-
{
40-
Log.CloseAndFlush();
41-
}
42-
}
18+
try
19+
{
20+
CreateHostBuilder(args).Build().Run();
4321

44-
public static IHostBuilder CreateHostBuilder(string[] args) =>
45-
Host.CreateDefaultBuilder(args)
46-
.UseSerilog((context, services, configuration) => configuration
47-
.ReadFrom.Configuration(context.Configuration)
48-
.ReadFrom.Services(services)
49-
.Enrich.FromLogContext()
50-
.WriteTo.Console())
51-
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
22+
Log.Information("Stopped cleanly");
23+
return 0;
24+
}
25+
catch (Exception ex)
26+
{
27+
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
28+
return 1;
29+
}
30+
finally
31+
{
32+
Log.CloseAndFlush();
33+
}
5234
}
53-
}
35+
36+
public static IHostBuilder CreateHostBuilder(string[] args) =>
37+
Host.CreateDefaultBuilder(args)
38+
.UseSerilog((context, services, configuration) => configuration
39+
.ReadFrom.Configuration(context.Configuration)
40+
.ReadFrom.Services(services)
41+
.Enrich.FromLogContext()
42+
.WriteTo.Console())
43+
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
44+
}

samples/Sample/Sample.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

samples/Sample/Startup.cs

+38-49
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,53 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Builder;
6-
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.HttpsPolicy;
8-
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.DependencyInjection;
10-
using Microsoft.Extensions.Hosting;
111
using Serilog;
122

13-
namespace Sample
3+
namespace Sample;
4+
5+
public class Startup
146
{
15-
public class Startup
7+
public Startup(IConfiguration configuration)
168
{
17-
public Startup(IConfiguration configuration)
18-
{
19-
Configuration = configuration;
20-
}
9+
Configuration = configuration;
10+
}
2111

22-
public IConfiguration Configuration { get; }
12+
public IConfiguration Configuration { get; }
13+
14+
// This method gets called by the runtime. Use this method to add services to the container.
15+
public void ConfigureServices(IServiceCollection services)
16+
{
17+
services.AddControllersWithViews();
18+
}
2319

24-
// This method gets called by the runtime. Use this method to add services to the container.
25-
public void ConfigureServices(IServiceCollection services)
20+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
21+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
22+
{
23+
if (env.IsDevelopment())
2624
{
27-
services.AddControllersWithViews();
25+
app.UseDeveloperExceptionPage();
2826
}
29-
30-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
31-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
27+
else
3228
{
33-
if (env.IsDevelopment())
34-
{
35-
app.UseDeveloperExceptionPage();
36-
}
37-
else
38-
{
39-
app.UseExceptionHandler("/Home/Error");
40-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
41-
app.UseHsts();
42-
}
29+
app.UseExceptionHandler("/Home/Error");
30+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
31+
app.UseHsts();
32+
}
4333

44-
app.UseHttpsRedirection();
45-
app.UseStaticFiles();
46-
47-
// Write streamlined request completion events, instead of the more verbose ones from the framework.
48-
// To use the default framework request logging instead, remove this line and set the "Microsoft"
49-
// level in appsettings.json to "Information".
50-
app.UseSerilogRequestLogging();
34+
app.UseHttpsRedirection();
35+
app.UseStaticFiles();
5136

52-
app.UseRouting();
37+
// Write streamlined request completion events, instead of the more verbose ones from the framework.
38+
// To use the default framework request logging instead, remove this line and set the "Microsoft"
39+
// level in appsettings.json to "Information".
40+
app.UseSerilogRequestLogging();
5341

54-
app.UseAuthorization();
42+
app.UseRouting();
5543

56-
app.UseEndpoints(endpoints =>
57-
{
58-
endpoints.MapControllerRoute(
59-
name: "default",
60-
pattern: "{controller=Home}/{action=Index}/{id?}");
61-
});
62-
}
44+
app.UseAuthorization();
45+
46+
app.UseEndpoints(endpoints =>
47+
{
48+
endpoints.MapControllerRoute(
49+
name: "default",
50+
pattern: "{controller=Home}/{action=Index}/{id?}");
51+
});
6352
}
6453
}

serilog-aspnetcore.sln

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9
1717
README.md = README.md
1818
assets\Serilog.snk = assets\Serilog.snk
1919
Setup.ps1 = Setup.ps1
20+
Directory.Build.props = Directory.Build.props
2021
EndProjectSection
2122
EndProject
2223
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.AspNetCore", "src\Serilog.AspNetCore\Serilog.AspNetCore.csproj", "{0549D23F-986B-4FB2-BACE-16FD7A7BC9EF}"

0 commit comments

Comments
 (0)