Skip to content

Commit 4f6a463

Browse files
authored
Merge pull request #86 from sungam3r/files
File-scoped namespaces
2 parents 9aa37c9 + c51b78c commit 4f6a463

29 files changed

+2298
-2325
lines changed

samples/SimpleServiceSample/PrintTimeService.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44
using Microsoft.Extensions.Hosting;
55
using Microsoft.Extensions.Logging;
66

7-
namespace SimpleServiceSample
7+
namespace SimpleServiceSample;
8+
9+
public class PrintTimeService : BackgroundService
810
{
9-
public class PrintTimeService : BackgroundService
10-
{
11-
private readonly ILogger _logger;
11+
private readonly ILogger _logger;
1212

13-
public PrintTimeService(ILogger<PrintTimeService> logger)
14-
{
15-
_logger = logger;
16-
}
13+
public PrintTimeService(ILogger<PrintTimeService> logger)
14+
{
15+
_logger = logger;
16+
}
1717

18-
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
18+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
19+
{
20+
while (!stoppingToken.IsCancellationRequested)
1921
{
20-
while (!stoppingToken.IsCancellationRequested)
21-
{
22-
_logger.LogInformation("The current time is: {CurrentTime}", DateTimeOffset.UtcNow);
23-
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
24-
}
22+
_logger.LogInformation("The current time is: {CurrentTime}", DateTimeOffset.UtcNow);
23+
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
2524
}
2625
}
2726
}

samples/SimpleServiceSample/Program.cs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,40 @@
33
using Microsoft.Extensions.Hosting;
44
using Serilog;
55

6-
namespace SimpleServiceSample
6+
namespace SimpleServiceSample;
7+
8+
public static class Program
79
{
8-
public static class Program
10+
public static int Main(string[] args)
911
{
10-
public static int Main(string[] args)
11-
{
12-
Log.Logger = new LoggerConfiguration()
13-
.Enrich.FromLogContext()
14-
.WriteTo.Console()
15-
.CreateBootstrapLogger();
12+
Log.Logger = new LoggerConfiguration()
13+
.Enrich.FromLogContext()
14+
.WriteTo.Console()
15+
.CreateBootstrapLogger();
1616

17-
try
18-
{
19-
Log.Information("Getting the motors running...");
20-
CreateHostBuilder(args).Build().Run();
21-
return 0;
22-
}
23-
catch (Exception ex)
24-
{
25-
Log.Fatal(ex, "Host terminated unexpectedly");
26-
return 1;
27-
}
28-
finally
29-
{
30-
Log.CloseAndFlush();
31-
}
17+
try
18+
{
19+
Log.Information("Getting the motors running...");
20+
CreateHostBuilder(args).Build().Run();
21+
return 0;
22+
}
23+
catch (Exception ex)
24+
{
25+
Log.Fatal(ex, "Host terminated unexpectedly");
26+
return 1;
27+
}
28+
finally
29+
{
30+
Log.CloseAndFlush();
3231
}
33-
34-
public static IHostBuilder CreateHostBuilder(string[] args) =>
35-
Host.CreateDefaultBuilder(args)
36-
.ConfigureServices(services => services.AddHostedService<PrintTimeService>())
37-
.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
38-
.ReadFrom.Configuration(context.Configuration)
39-
.ReadFrom.Services(services)
40-
.Enrich.FromLogContext()
41-
.WriteTo.Console());
4232
}
33+
34+
public static IHostBuilder CreateHostBuilder(string[] args) =>
35+
Host.CreateDefaultBuilder(args)
36+
.ConfigureServices(services => services.AddHostedService<PrintTimeService>())
37+
.UseSerilog((context, services, loggerConfiguration) => loggerConfiguration
38+
.ReadFrom.Configuration(context.Configuration)
39+
.ReadFrom.Services(services)
40+
.Enrich.FromLogContext()
41+
.WriteTo.Console());
4342
}

samples/WebApplicationSample/Program.cs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,41 @@
33
using Microsoft.Extensions.Hosting;
44
using Serilog;
55

6-
namespace WebApplicationSample
6+
namespace WebApplicationSample;
7+
8+
public static class Program
79
{
8-
public static class Program
10+
public static int Main(string[] args)
911
{
10-
public static int Main(string[] args)
11-
{
12-
Log.Logger = new LoggerConfiguration()
13-
.WriteTo.Console()
14-
.CreateBootstrapLogger();
15-
16-
Log.Information("Starting up!");
12+
Log.Logger = new LoggerConfiguration()
13+
.WriteTo.Console()
14+
.CreateBootstrapLogger();
15+
16+
Log.Information("Starting up!");
1717

18-
try
19-
{
20-
CreateHostBuilder(args).Build().Run();
18+
try
19+
{
20+
CreateHostBuilder(args).Build().Run();
2121

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-
}
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();
3433
}
35-
36-
public static IHostBuilder CreateHostBuilder(string[] args) =>
37-
Host.CreateDefaultBuilder(args)
38-
.UseSerilog((context, services, configuration) => configuration
39-
.WriteTo.Console()
40-
.ReadFrom.Configuration(context.Configuration)
41-
.ReadFrom.Services(services))
42-
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
4334
}
35+
36+
public static IHostBuilder CreateHostBuilder(string[] args) =>
37+
Host.CreateDefaultBuilder(args)
38+
.UseSerilog((context, services, configuration) => configuration
39+
.WriteTo.Console()
40+
.ReadFrom.Configuration(context.Configuration)
41+
.ReadFrom.Services(services))
42+
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
4443
}

samples/WebApplicationSample/Startup.cs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,33 @@
99
using Microsoft.Extensions.Hosting;
1010
using Serilog;
1111

12-
namespace WebApplicationSample
12+
namespace WebApplicationSample;
13+
14+
public class Startup
1315
{
14-
public class Startup
16+
// This method gets called by the runtime. Use this method to add services to the container.
17+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
18+
public void ConfigureServices(IServiceCollection services)
1519
{
16-
// This method gets called by the runtime. Use this method to add services to the container.
17-
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
18-
public void ConfigureServices(IServiceCollection services)
19-
{
20-
}
20+
}
2121

22-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
23-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
22+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
23+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
24+
{
25+
if (env.IsDevelopment())
2426
{
25-
if (env.IsDevelopment())
26-
{
27-
app.UseDeveloperExceptionPage();
28-
}
27+
app.UseDeveloperExceptionPage();
28+
}
2929

30-
app.UseRouting();
30+
app.UseRouting();
3131

32-
app.UseEndpoints(endpoints =>
32+
app.UseEndpoints(endpoints =>
33+
{
34+
endpoints.MapGet("/", async context =>
3335
{
34-
endpoints.MapGet("/", async context =>
35-
{
36-
Log.Information("Saying hello");
37-
await context.Response.WriteAsync("Hello World!");
38-
});
36+
Log.Information("Saying hello");
37+
await context.Response.WriteAsync("Hello World!");
3938
});
40-
}
39+
});
4140
}
4241
}

src/Serilog.Extensions.Hosting/Extensions/Hosting/AmbientDiagnosticContextCollector.cs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,31 @@
1515
using System;
1616
using System.Threading;
1717

18-
namespace Serilog.Extensions.Hosting
18+
namespace Serilog.Extensions.Hosting;
19+
20+
class AmbientDiagnosticContextCollector : IDisposable
1921
{
20-
class AmbientDiagnosticContextCollector : IDisposable
21-
{
22-
static readonly AsyncLocal<AmbientDiagnosticContextCollector> AmbientCollector =
23-
new AsyncLocal<AmbientDiagnosticContextCollector>();
22+
static readonly AsyncLocal<AmbientDiagnosticContextCollector> AmbientCollector =
23+
new AsyncLocal<AmbientDiagnosticContextCollector>();
2424

25-
// The indirection here ensures that completing collection cleans up the collector in all
26-
// execution contexts. Via @benaadams' addition to `HttpContextAccessor` :-)
27-
DiagnosticContextCollector _collector;
25+
// The indirection here ensures that completing collection cleans up the collector in all
26+
// execution contexts. Via @benaadams' addition to `HttpContextAccessor` :-)
27+
DiagnosticContextCollector _collector;
2828

29-
public static DiagnosticContextCollector Current => AmbientCollector.Value?._collector;
29+
public static DiagnosticContextCollector Current => AmbientCollector.Value?._collector;
3030

31-
public static DiagnosticContextCollector Begin()
32-
{
33-
var value = new AmbientDiagnosticContextCollector();
34-
value._collector = new DiagnosticContextCollector(value);
35-
AmbientCollector.Value = value;
36-
return value._collector;
37-
}
31+
public static DiagnosticContextCollector Begin()
32+
{
33+
var value = new AmbientDiagnosticContextCollector();
34+
value._collector = new DiagnosticContextCollector(value);
35+
AmbientCollector.Value = value;
36+
return value._collector;
37+
}
3838

39-
public void Dispose()
40-
{
41-
_collector = null;
42-
if (AmbientCollector.Value == this)
43-
AmbientCollector.Value = null;
44-
}
39+
public void Dispose()
40+
{
41+
_collector = null;
42+
if (AmbientCollector.Value == this)
43+
AmbientCollector.Value = null;
4544
}
4645
}

0 commit comments

Comments
 (0)