Skip to content

Commit e143130

Browse files
committed
Added simple WebAPI with scopes from old sample.
1 parent 49b328b commit e143130

File tree

8 files changed

+187
-0
lines changed

8 files changed

+187
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Serilog;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace SimpleWebAPISample.Controllers
10+
{
11+
[Route("api/[controller]")]
12+
public class ScopesController : Controller
13+
{
14+
ILogger<ScopesController> _logger;
15+
16+
public ScopesController(ILogger<ScopesController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
// GET api/scopes
22+
[HttpGet]
23+
public IEnumerable<string> Get()
24+
{
25+
_logger.LogInformation("Before");
26+
27+
using (_logger.BeginScope("Some name"))
28+
using (_logger.BeginScope(42))
29+
using (_logger.BeginScope("Formatted {WithValue}", 12345))
30+
using (_logger.BeginScope(new Dictionary<string, object> { ["ViaDictionary"] = 100 }))
31+
{
32+
_logger.LogInformation("Hello from the Index!");
33+
}
34+
35+
_logger.LogInformation("After");
36+
37+
return new string[] { "value1", "value2" };
38+
}
39+
}
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Serilog;
7+
8+
namespace SimpleWebAPISample.Controllers
9+
{
10+
[Route("api/[controller]")]
11+
public class ValuesController : Controller
12+
{
13+
// GET api/values
14+
[HttpGet]
15+
public IEnumerable<string> Get()
16+
{
17+
// Directly through Serilog
18+
Log.Information("This is a handler for {Path}", Request.Path);
19+
return new string[] { "value1", "value2" };
20+
}
21+
}
22+
}

samples/SimpleWebAPISample/Program.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
using Serilog;
12+
13+
namespace SimpleWebAPISample
14+
{
15+
public class Program
16+
{
17+
public static void Main(string[] args)
18+
{
19+
Log.Logger = new LoggerConfiguration()
20+
.Enrich.FromLogContext()
21+
.WriteTo.LiterateConsole()
22+
.CreateLogger();
23+
24+
Log.Information("Getting the motors running...");
25+
26+
BuildWebHost(args).Run();
27+
}
28+
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();
38+
}
39+
}

samples/SimpleWebAPISample/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```
2+
dotnet new webapi
3+
dotnet add package Serilog.Extensions.Logging
4+
dotnet add package Serilog.Sinks.Literate
5+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Folder Include="wwwroot\" />
7+
</ItemGroup>
8+
<ItemGroup>
9+
<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" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
15+
</ItemGroup>
16+
</Project>

samples/SimpleWebAPISample/Startup.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.Options;
11+
12+
namespace SimpleWebAPISample
13+
{
14+
public class Startup
15+
{
16+
public Startup(IConfiguration configuration)
17+
{
18+
Configuration = configuration;
19+
}
20+
21+
public IConfiguration Configuration { get; }
22+
23+
// This method gets called by the runtime. Use this method to add services to the container.
24+
public void ConfigureServices(IServiceCollection services)
25+
{
26+
services.AddMvc();
27+
}
28+
29+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
30+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
31+
{
32+
if (env.IsDevelopment())
33+
{
34+
app.UseDeveloperExceptionPage();
35+
}
36+
37+
app.UseMvc();
38+
}
39+
}
40+
}
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"Debug": {
5+
"LogLevel": {
6+
"Default": "Warning"
7+
}
8+
},
9+
"Console": {
10+
"LogLevel": {
11+
"Default": "Warning"
12+
}
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)