Skip to content

Commit 1332279

Browse files
authored
Merge pull request #44 from nblumhardt/f-example
Fixes #40 - web sample
2 parents 61aed76 + 9b92790 commit 1332279

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

samples/WebSample/Controllers/HomeController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Mvc;
6+
using Serilog;
67

78
namespace WebSample.Controllers
89
{
910
public class HomeController : Controller
1011
{
1112
public IActionResult Index()
1213
{
14+
Log.Information("Hello from the Index!");
15+
1316
return View();
1417
}
1518

1619
public IActionResult About()
1720
{
1821
ViewData["Message"] = "Your application description page.";
1922

23+
Log.Information("This is a handler for {Path}", Request.Path);
24+
2025
return View();
2126
}
2227

samples/WebSample/Startup.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging;
10+
using Serilog;
1011

1112
namespace WebSample
1213
{
1314
public class Startup
1415
{
1516
public Startup(IHostingEnvironment env)
1617
{
17-
var builder = new ConfigurationBuilder()
18+
Configuration = new ConfigurationBuilder()
1819
.SetBasePath(env.ContentRootPath)
1920
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
2021
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
21-
.AddEnvironmentVariables();
22-
Configuration = builder.Build();
22+
.AddEnvironmentVariables()
23+
.Build();
24+
25+
Log.Logger = new LoggerConfiguration()
26+
.ReadFrom.Configuration(Configuration)
27+
.CreateLogger();
2328
}
2429

2530
public IConfigurationRoot Configuration { get; }
@@ -34,8 +39,8 @@ public void ConfigureServices(IServiceCollection services)
3439
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3540
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
3641
{
37-
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
38-
loggerFactory.AddDebug();
42+
// Specifying dispose: true closes and flushes the Serilog `Log` class when the app shuts down.
43+
loggerFactory.AddSerilog(dispose: true);
3944

4045
if (env.IsDevelopment())
4146
{

samples/WebSample/appsettings.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{
2-
"Logging": {
3-
"IncludeScopes": false,
4-
"LogLevel": {
2+
"Serilog": {
3+
"MinimumLevel": {
54
"Default": "Debug",
6-
"System": "Information",
7-
"Microsoft": "Information"
8-
}
5+
"Override": {
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
},
10+
"Enrich": [
11+
"FromLogContext"
12+
],
13+
"WriteTo": [
14+
"Trace",
15+
"LiterateConsole"
16+
]
917
}
1018
}

samples/WebSample/project.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"Microsoft.Extensions.Logging.Console": "1.0.0",
2020
"Microsoft.Extensions.Logging.Debug": "1.0.0",
2121
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
22-
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
22+
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
23+
"Serilog.Extensions.Logging": { "target": "project" },
24+
"Serilog.Settings.Configuration": "2.1.0-dev-00028",
25+
"Serilog.Sinks.Trace": "2.0.0",
26+
"Serilog.Sinks.Literate": "2.0.0"
2327
},
2428

2529
"tools": {

0 commit comments

Comments
 (0)