File tree Expand file tree Collapse file tree 8 files changed +187
-0
lines changed
samples/SimpleWebAPISample Expand file tree Collapse file tree 8 files changed +187
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ ```
2+ dotnet new webapi
3+ dotnet add package Serilog.Extensions.Logging
4+ dotnet add package Serilog.Sinks.Literate
5+ ```
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ {
2+ "Logging" : {
3+ "IncludeScopes" : false ,
4+ "LogLevel" : {
5+ "Default" : " Debug" ,
6+ "System" : " Information" ,
7+ "Microsoft" : " Information"
8+ }
9+ }
10+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments