-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f56c26c
commit 55d2469
Showing
20 changed files
with
980 additions
and
1,025 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<LangVersion>latest</LangVersion> | ||
<TreatWarningsAsErrors>True</TreatWarningsAsErrors> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile> | ||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> | ||
<CheckEolTargetFramework>false</CheckEolTargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"sdk": { | ||
"allowPrerelease": false, | ||
"version": "5.0.201", | ||
"version": "6.0.401", | ||
"rollForward": "latestFeature" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using Sample.Models; | ||
using Serilog; | ||
|
||
namespace Sample.Controllers | ||
namespace Sample.Controllers; | ||
|
||
public class HomeController : Controller | ||
{ | ||
public class HomeController : Controller | ||
static int _callCount; | ||
|
||
readonly ILogger<HomeController> _logger; | ||
readonly IDiagnosticContext _diagnosticContext; | ||
|
||
public HomeController(ILogger<HomeController> logger, IDiagnosticContext diagnosticContext) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext)); | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
_logger.LogInformation("Hello, world!"); | ||
|
||
_diagnosticContext.Set("IndexCallCount", Interlocked.Increment(ref _callCount)); | ||
|
||
return View(); | ||
} | ||
|
||
public IActionResult Privacy() | ||
{ | ||
throw new InvalidOperationException("Something went wrong."); | ||
} | ||
|
||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
public IActionResult Error() | ||
{ | ||
static int _callCount; | ||
|
||
readonly ILogger<HomeController> _logger; | ||
readonly IDiagnosticContext _diagnosticContext; | ||
|
||
public HomeController(ILogger<HomeController> logger, IDiagnosticContext diagnosticContext) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext)); | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
_logger.LogInformation("Hello, world!"); | ||
|
||
_diagnosticContext.Set("IndexCallCount", Interlocked.Increment(ref _callCount)); | ||
|
||
return View(); | ||
} | ||
|
||
public IActionResult Privacy() | ||
{ | ||
throw new InvalidOperationException("Something went wrong."); | ||
} | ||
|
||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
public IActionResult Error() | ||
{ | ||
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier}); | ||
} | ||
return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
using System; | ||
namespace Sample.Models; | ||
|
||
namespace Sample.Models | ||
public class ErrorViewModel | ||
{ | ||
public class ErrorViewModel | ||
{ | ||
public string RequestId { get; set; } | ||
public string? RequestId { get; set; } | ||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
} | ||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
|
||
namespace Sample | ||
namespace Sample; | ||
|
||
public static class Program | ||
{ | ||
public static class Program | ||
public static int Main(string[] args) | ||
{ | ||
public static int Main(string[] args) | ||
{ | ||
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the | ||
// logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been | ||
// set up successfully. | ||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.CreateBootstrapLogger(); | ||
|
||
Log.Information("Starting up!"); | ||
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the | ||
// logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been | ||
// set up successfully. | ||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.CreateBootstrapLogger(); | ||
|
||
try | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
Log.Information("Starting up!"); | ||
|
||
Log.Information("Stopped cleanly"); | ||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "An unhandled exception occured during bootstrapping"); | ||
return 1; | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} | ||
} | ||
try | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.UseSerilog((context, services, configuration) => configuration | ||
.ReadFrom.Configuration(context.Configuration) | ||
.ReadFrom.Services(services) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console()) | ||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); | ||
Log.Information("Stopped cleanly"); | ||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Fatal(ex, "An unhandled exception occured during bootstrapping"); | ||
return 1; | ||
} | ||
finally | ||
{ | ||
Log.CloseAndFlush(); | ||
} | ||
} | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.UseSerilog((context, services, configuration) => configuration | ||
.ReadFrom.Configuration(context.Configuration) | ||
.ReadFrom.Services(services) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console()) | ||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.HttpsPolicy; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
|
||
namespace Sample | ||
namespace Sample; | ||
|
||
public class Startup | ||
{ | ||
public class Startup | ||
public Startup(IConfiguration configuration) | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
public IConfiguration Configuration { get; } | ||
|
||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddControllersWithViews(); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
services.AddControllersWithViews(); | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
else | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
app.UseExceptionHandler("/Home/Error"); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
app.UseExceptionHandler("/Home/Error"); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
// Write streamlined request completion events, instead of the more verbose ones from the framework. | ||
// To use the default framework request logging instead, remove this line and set the "Microsoft" | ||
// level in appsettings.json to "Information". | ||
app.UseSerilogRequestLogging(); | ||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
// Write streamlined request completion events, instead of the more verbose ones from the framework. | ||
// To use the default framework request logging instead, remove this line and set the "Microsoft" | ||
// level in appsettings.json to "Information". | ||
app.UseSerilogRequestLogging(); | ||
|
||
app.UseAuthorization(); | ||
app.UseRouting(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllerRoute( | ||
name: "default", | ||
pattern: "{controller=Home}/{action=Index}/{id?}"); | ||
}); | ||
} | ||
app.UseAuthorization(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapControllerRoute( | ||
name: "default", | ||
pattern: "{controller=Home}/{action=Index}/{id?}"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.