Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit 3dcbf30

Browse files
make sure everything has a log context
1 parent c893ccb commit 3dcbf30

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

src/SqlStreamStore.Server/Browser/SqlStreamStoreBrowserMiddleware.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace SqlStreamStore.Server.Browser
1111
{
1212
internal static class SqlStreamStoreBrowserMiddleware
1313
{
14+
private static readonly ILogger s_Log = Log.ForContext(typeof(SqlStreamStoreBrowserMiddleware));
1415
public static IApplicationBuilder UseSqlStreamStoreBrowser(
1516
this IApplicationBuilder builder,
1617
Type rootType = default)
@@ -23,7 +24,7 @@ public static IApplicationBuilder UseSqlStreamStoreBrowser(
2324
var staticFiles = rootType.Assembly.GetManifestResourceNames()
2425
.Where(name => name.StartsWith(rootType.Namespace));
2526

26-
Log.Debug(
27+
s_Log.Debug(
2728
"The following embedded resources were found and will be served as static content: {staticFiles}",
2829
string.Join(", ", staticFiles));
2930

src/SqlStreamStore.Server/DatabaseInitializer.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
using Serilog;
88
using SqlStreamStore.Infrastructure;
99
using static SqlStreamStore.Server.Constants;
10+
1011
namespace SqlStreamStore.Server
1112
{
1213
internal class DatabaseInitializer
1314
{
15+
private static readonly ILogger s_Log = Log.ForContext<DatabaseInitializer>();
16+
1417
private readonly SqlStreamStoreServerConfiguration _configuration;
1518

1619
public DatabaseInitializer(SqlStreamStoreServerConfiguration configuration)
@@ -34,7 +37,7 @@ public Task Initialize(CancellationToken cancellationToken = default)
3437
case postgres:
3538
return InitializePostgres(cancellationToken);
3639
default:
37-
Log.Warning("Provider {provider} has no database initializer.", _configuration.Provider);
40+
s_Log.Warning("Provider {provider} has no database initializer.", _configuration.Provider);
3841
return Task.CompletedTask;
3942
}
4043
}
@@ -45,7 +48,7 @@ private async Task InitializeMySql(CancellationToken cancellationToken)
4548

4649
var cmdText = $"CREATE DATABASE IF NOT EXISTS `{connectionStringBuilder.Database}`";
4750

48-
Log.Information(
51+
s_Log.Information(
4952
"Creating database '{database}' at server '{server}' with the statement: {cmdText}",
5053
connectionStringBuilder.Database,
5154
connectionStringBuilder.Server,
@@ -78,7 +81,7 @@ IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'{connectionStringB
7881
CREATE DATABASE [{connectionStringBuilder.InitialCatalog}]
7982
END;
8083
";
81-
Log.Information(
84+
s_Log.Information(
8285
"Creating database '{database}' at server '{server}' with the statement: {cmdText}",
8386
connectionStringBuilder.InitialCatalog,
8487
connectionStringBuilder.DataSource,
@@ -107,7 +110,7 @@ private async Task InitializePostgres(CancellationToken cancellationToken)
107110

108111
var cmdText = $"CREATE DATABASE {connectionStringBuilder.Database}";
109112

110-
Log.Information(
113+
s_Log.Information(
111114
"Creating database '{database}' at server '{server}' with the statement: {cmdText}",
112115
connectionStringBuilder.Database,
113116
connectionStringBuilder.Host,

src/SqlStreamStore.Server/SqlStreamStoreFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace SqlStreamStore.Server
66
{
77
internal class SqlStreamStoreFactory
88
{
9+
private static readonly ILogger s_Log = Log.ForContext<SqlStreamStoreFactory>();
10+
911
private readonly SqlStreamStoreServerConfiguration _configuration;
1012

1113
public SqlStreamStoreFactory(SqlStreamStoreServerConfiguration configuration)
@@ -22,7 +24,7 @@ public IStreamStore Create()
2224
{
2325
var provider = _configuration.Provider;
2426

25-
Log.Information("Creating stream store for provider {provider}.", provider);
27+
s_Log.Information("Creating stream store for provider {provider}.", provider);
2628

2729
switch (provider)
2830
{

src/SqlStreamStore.Server/SqlStreamStoreInitializer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace SqlStreamStore.Server
1212
{
1313
internal class SqlStreamStoreInitializer
1414
{
15+
private static readonly ILogger s_Log = Log.ForContext<SqlStreamStoreInitializer>();
16+
1517
private readonly SqlStreamStoreServerConfiguration _configuration;
1618
private readonly SqlStreamStoreFactory _streamStoreFactory;
1719

@@ -37,7 +39,7 @@ public Task Initialize(CancellationToken cancellationToken = default)
3739
case postgres:
3840
return InitializePostgresStreamStore(cancellationToken);
3941
default:
40-
Log.Warning("Provider {provider} has no initialization.", _configuration.Provider);
42+
s_Log.Warning("Provider {provider} has no initialization.", _configuration.Provider);
4143
return Task.CompletedTask;
4244
}
4345
}
@@ -89,7 +91,7 @@ private async Task InitializePostgresStreamStore(CancellationToken cancellationT
8991

9092
private static void SchemaCreationFailed(Func<string> getSchemaCreationScript, Exception ex)
9193
{
92-
Log.Error(
94+
s_Log.Error(
9395
new StringBuilder()
9496
.Append("Could not create schema: {ex}")
9597
.AppendLine()

src/SqlStreamStore.Server/Program.cs renamed to src/SqlStreamStore.Server/SqlStreamStoreServer.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
namespace SqlStreamStore.Server
1111
{
12-
internal class Program : IDisposable
12+
internal class SqlStreamStoreServer : IDisposable
1313
{
14+
private static readonly ILogger s_Log = Log.ForContext<SqlStreamStoreServer>();
15+
1416
private readonly CancellationTokenSource _cts;
1517
private readonly SqlStreamStoreServerConfiguration _configuration;
1618
private readonly SqlStreamStoreFactory _factory;
@@ -21,22 +23,24 @@ public static async Task<int> Main(string[] args)
2123
Environment.GetEnvironmentVariables(),
2224
args);
2325

24-
using (var program = new Program(configuration))
26+
using (var server = new SqlStreamStoreServer(configuration))
2527
{
26-
return await program.Run();
28+
return await server.Run();
2729
}
2830
}
2931

30-
private Program(SqlStreamStoreServerConfiguration configuration)
32+
private SqlStreamStoreServer(SqlStreamStoreServerConfiguration configuration)
3133
{
3234
Log.Logger = new LoggerConfiguration()
3335
.MinimumLevel.Is(configuration.LogLevel)
3436
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
3537
.Enrich.FromLogContext()
36-
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}")
38+
.WriteTo.Console(
39+
outputTemplate:
40+
"[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}")
3741
.CreateLogger();
3842

39-
Log.Information(configuration.ToString());
43+
s_Log.Information(configuration.ToString());
4044

4145
_configuration = configuration;
4246
_cts = new CancellationTokenSource();
@@ -64,7 +68,7 @@ private async Task<int> Run()
6468
}
6569
catch (Exception ex)
6670
{
67-
Log.Fatal(ex, "Host terminated unexpectedly.");
71+
s_Log.Fatal(ex, "Host terminated unexpectedly.");
6872
return 1;
6973
}
7074
finally
@@ -84,7 +88,7 @@ private async Task RunServer()
8488
new SqlStreamStoreMiddlewareOptions
8589
{
8690
UseCanonicalUrls = _configuration.UseCanonicalUris,
87-
ServerAssembly = typeof(Program).Assembly
91+
ServerAssembly = typeof(SqlStreamStoreServer).Assembly
8892
}))
8993
.UseSerilog()
9094
.Build())

0 commit comments

Comments
 (0)