Skip to content

Commit ffb726b

Browse files
authored
A bootstrap logger (#28)
* ReadFrom.Services() * A have-your-cake-and-eat-it-too bootstrap logger * Delete accidentally-committed log file * Remove methods covered by default interface implementations
1 parent 00fb5e7 commit ffb726b

20 files changed

+1621
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,4 @@ __pycache__/
285285
*.btm.cs
286286
*.odx.cs
287287
*.xsd.cs
288+
/samples/WebApplicationSample/logs/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
using Serilog;
10+
11+
namespace WebApplicationSample
12+
{
13+
public static class Program
14+
{
15+
public static int Main(string[] args)
16+
{
17+
Log.Logger = new LoggerConfiguration()
18+
.WriteTo.Console()
19+
.CreateBootstrapLogger();
20+
21+
Log.Information("Starting up!");
22+
23+
try
24+
{
25+
CreateHostBuilder(args).Build().Run();
26+
27+
Log.Information("Stopped cleanly");
28+
return 0;
29+
}
30+
catch (Exception ex)
31+
{
32+
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
33+
return 1;
34+
}
35+
finally
36+
{
37+
Log.CloseAndFlush();
38+
}
39+
}
40+
41+
public static IHostBuilder CreateHostBuilder(string[] args) =>
42+
Host.CreateDefaultBuilder(args)
43+
.UseSerilog((context, services, configuration) => configuration
44+
.WriteTo.Console()
45+
.ReadFrom.Configuration(context.Configuration)
46+
.ReadFrom.Services(services))
47+
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:15670",
7+
"sslPort": 44322
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"WebApplicationSample": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.AspNetCore.Http;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
10+
using Serilog;
11+
12+
namespace WebApplicationSample
13+
{
14+
public class Startup
15+
{
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+
}
21+
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())
26+
{
27+
app.UseDeveloperExceptionPage();
28+
}
29+
30+
app.UseRouting();
31+
32+
app.UseEndpoints(endpoints =>
33+
{
34+
endpoints.MapGet("/", async context =>
35+
{
36+
Log.Information("Saying hello");
37+
await context.Response.WriteAsync("Hello World!");
38+
});
39+
});
40+
}
41+
}
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\Serilog.Extensions.Hosting\Serilog.Extensions.Hosting.csproj" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="serilog.settings.configuration" Version="3.1.0" />
13+
<PackageReference Include="serilog.sinks.console" Version="3.1.1" />
14+
<PackageReference Include="serilog.sinks.file" Version="4.1.0" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"Serilog": {
3+
"MinimumLevel": {
4+
"Default": "Information",
5+
"Override": {
6+
"Microsoft": "Warning",
7+
"Microsoft.Hosting.Lifetime": "Information"
8+
}
9+
},
10+
"WriteTo": [
11+
{
12+
"Name": "File",
13+
"Args": { "path": "./logs/log-.txt", "rollingInterval": "Day" }
14+
}
15+
]
16+
},
17+
"AllowedHosts": "*"
18+
}

serilog-extensions-hosting.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Hosting.
2424
EndProject
2525
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleServiceSample", "samples\SimpleServiceSample\SimpleServiceSample.csproj", "{E5A82756-4619-4E6B-8B26-6D83E00E99F0}"
2626
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplicationSample", "samples\WebApplicationSample\WebApplicationSample.csproj", "{1ACDCA67-F404-45AB-9348-98E55E03CB8C}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -42,6 +44,10 @@ Global
4244
{E5A82756-4619-4E6B-8B26-6D83E00E99F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
4345
{E5A82756-4619-4E6B-8B26-6D83E00E99F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
4446
{E5A82756-4619-4E6B-8B26-6D83E00E99F0}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{1ACDCA67-F404-45AB-9348-98E55E03CB8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{1ACDCA67-F404-45AB-9348-98E55E03CB8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{1ACDCA67-F404-45AB-9348-98E55E03CB8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{1ACDCA67-F404-45AB-9348-98E55E03CB8C}.Release|Any CPU.Build.0 = Release|Any CPU
4551
EndGlobalSection
4652
GlobalSection(SolutionProperties) = preSolution
4753
HideSolutionNode = FALSE
@@ -50,6 +56,7 @@ Global
5056
{0549D23F-986B-4FB2-BACE-16FD7A7BC9EF} = {A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}
5157
{AD51759B-CD58-473F-9620-0B0E56A123A1} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
5258
{E5A82756-4619-4E6B-8B26-6D83E00E99F0} = {F2407211-6043-439C-8E06-3641634332E7}
59+
{1ACDCA67-F404-45AB-9348-98E55E03CB8C} = {F2407211-6043-439C-8E06-3641634332E7}
5360
EndGlobalSection
5461
GlobalSection(ExtensibilityGlobals) = postSolution
5562
SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10}

0 commit comments

Comments
 (0)