Skip to content

Commit 4c10236

Browse files
authored
fix: create 1 db/db_errors file logger per context type (AscensionGameDev#2061)
1 parent 133b102 commit 4c10236

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

Intersect.Server.Core/Database/DatabaseTypeMigrationService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ PropertyInfo dbSetInfo
186186
await using var fromContext = IntersectDbContext<TContext>.Create(fromOptions with
187187
{
188188
DisableAutoInclude = true,
189-
LoggerFactory = new IntersectLoggerFactory(),
189+
LoggerFactory = new IntersectLoggerFactory(typeof(TContext).Name),
190190
});
191191
await using var toContext = IntersectDbContext<TContext>.Create(toOptions with
192192
{
193193
DisableAutoInclude = true,
194194
EnableDetailedErrors = true,
195195
EnableSensitiveDataLogging = true,
196-
LoggerFactory = new IntersectLoggerFactory(),
196+
LoggerFactory = new IntersectLoggerFactory(typeof(TContext).Name),
197197
});
198198

199199
if (dbSetInfo.GetValue(fromContext) is not DbSet<T> fromDbSet)

Intersect.Server.Core/Database/DbInterface.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static GameContext CreateGameContext(
101101
ExplicitLoad = explicitLoad,
102102
LazyLoading = lazyLoading,
103103
#if DEBUG
104-
LoggerFactory = new IntersectLoggerFactory(),
104+
LoggerFactory = new IntersectLoggerFactory(nameof(GameContext)),
105105
#endif
106106
QueryTrackingBehavior = queryTrackingBehavior,
107107
ReadOnly = readOnly,
@@ -129,7 +129,7 @@ public static PlayerContext CreatePlayerContext(
129129
ExplicitLoad = explicitLoad,
130130
LazyLoading = lazyLoading,
131131
#if DEBUG
132-
LoggerFactory = new IntersectLoggerFactory(),
132+
LoggerFactory = new IntersectLoggerFactory(nameof(PlayerContext)),
133133
#endif
134134
QueryTrackingBehavior = queryTrackingBehavior,
135135
ReadOnly = readOnly,
@@ -152,7 +152,7 @@ internal static LoggingContext CreateLoggingContext(
152152
ExplicitLoad = explicitLoad,
153153
LazyLoading = lazyLoading,
154154
#if DEBUG
155-
LoggerFactory = new IntersectLoggerFactory(),
155+
LoggerFactory = new IntersectLoggerFactory(nameof(LoggingContext)),
156156
#endif
157157
QueryTrackingBehavior = queryTrackingBehavior,
158158
ReadOnly = readOnly,
@@ -284,7 +284,7 @@ private static bool EnsureUpdated(IServerContext serverContext)
284284
DatabaseType = Options.Instance.GameDatabase.Type,
285285
EnableDetailedErrors = true,
286286
EnableSensitiveDataLogging = true,
287-
LoggerFactory = new IntersectLoggerFactory(),
287+
LoggerFactory = new IntersectLoggerFactory(nameof(GameContext)),
288288
});
289289

290290
Log.Verbose("Creating player context...");
@@ -297,7 +297,7 @@ private static bool EnsureUpdated(IServerContext serverContext)
297297
DatabaseType = Options.Instance.PlayerDatabase.Type,
298298
EnableDetailedErrors = true,
299299
EnableSensitiveDataLogging = true,
300-
LoggerFactory = new IntersectLoggerFactory(),
300+
LoggerFactory = new IntersectLoggerFactory(nameof(PlayerContext)),
301301
});
302302

303303
Log.Verbose("Creating logging context...");
@@ -310,7 +310,7 @@ private static bool EnsureUpdated(IServerContext serverContext)
310310
DatabaseType = Options.Instance.LoggingDatabase.Type,
311311
EnableDetailedErrors = true,
312312
EnableSensitiveDataLogging = true,
313-
LoggerFactory = new IntersectLoggerFactory(),
313+
LoggerFactory = new IntersectLoggerFactory(nameof(LoggingContext)),
314314
});
315315

316316
// We don't want anyone running the old migration tool accidentally

Intersect.Server.Core/Database/IntersectLoggerFactory.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ namespace Intersect.Server.Database;
1313

1414
internal sealed class IntersectLoggerFactory : ILoggerFactory
1515
{
16-
private static string ExecutableName => Path.GetFileNameWithoutExtension(
17-
Process.GetCurrentProcess().MainModule?.FileName ?? Assembly.GetExecutingAssembly().GetName().Name
18-
);
16+
private static readonly Dictionary<string, ImmutableArray<ILogOutput>> _cachedOutputs = new();
1917

2018
private readonly DbLoggerProvider _loggerProvider;
2119

22-
internal IntersectLoggerFactory()
20+
internal IntersectLoggerFactory(string name)
2321
{
24-
var outputs = ImmutableList.Create<ILogOutput>(
25-
new FileOutput($"db-{ExecutableName}.log"),
26-
new FileOutput($"errors-{ExecutableName}.log", IntersectLogLevel.Error),
27-
new ConciseConsoleOutput(Debugger.IsAttached ? IntersectLogLevel.Warn : IntersectLogLevel.Error)
28-
);
22+
if (!_cachedOutputs.TryGetValue(name, out var outputs))
23+
{
24+
outputs = ImmutableArray.Create<ILogOutput>(
25+
new FileOutput(Log.SuggestFilename(prefix: $"db-{name}-")),
26+
new FileOutput(Log.SuggestFilename(prefix: $"db_errors-{name}-"), IntersectLogLevel.Error),
27+
new ConciseConsoleOutput(Debugger.IsAttached ? IntersectLogLevel.Warn : IntersectLogLevel.Error)
28+
);
29+
_cachedOutputs[name] = outputs;
30+
}
2931

3032
_loggerProvider = new DbLoggerProvider(
3133
new Logger(

0 commit comments

Comments
 (0)