Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Intersect.Server.Core/Core/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ private static bool PreContextSetup(params string[] args)
}
}

DbInterface.InitializeDbLoggers();
DbInterface.CheckDirectories();

PrintIntroduction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,12 @@ PropertyInfo dbSetInfo
await using var fromContext = IntersectDbContext<TContext>.Create(fromOptions with
{
DisableAutoInclude = true,
LoggerFactory = new IntersectLoggerFactory(typeof(TContext).Name),
});
await using var toContext = IntersectDbContext<TContext>.Create(toOptions with
{
DisableAutoInclude = true,
EnableDetailedErrors = true,
EnableSensitiveDataLogging = true,
LoggerFactory = new IntersectLoggerFactory(typeof(TContext).Name),
});

if (dbSetInfo.GetValue(fromContext) is not DbSet<T> fromDbSet)
Expand Down
75 changes: 40 additions & 35 deletions Intersect.Server.Core/Database/DbInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using Serilog;
using Serilog.Events;
using Serilog.Extensions.Logging;

namespace Intersect.Server.Database;

Expand Down Expand Up @@ -60,10 +63,6 @@ public static partial class DbInterface

private static string PlayersDbFilename => Path.Combine(ServerContext.ResourceDirectory, "playerdata.db");

private static ILogger<GameContext> _gameDatabaseLogger { get; set; }

private static ILogger<PlayerContext> _playerDatabaseLogger { get; set; }

public static Dictionary<string, ServerVariableDescriptor> ServerVariableEventTextLookup = new();

public static Dictionary<string, PlayerVariableDescriptor> PlayerVariableEventTextLookup = new();
Expand Down Expand Up @@ -93,9 +92,7 @@ public static GameContext CreateGameContext(
ExplicitLoad = explicitLoad,
KillServerOnConcurrencyException = Options.Instance.GameDatabase.KillServerOnConcurrencyException,
LazyLoading = lazyLoading,
#if DEBUG
LoggerFactory = new IntersectLoggerFactory(nameof(GameContext)),
#endif
LoggerFactory = CreateLoggerFactory<GameContext>(Options.Instance.GameDatabase),
QueryTrackingBehavior = queryTrackingBehavior,
ReadOnly = readOnly,
});
Expand All @@ -117,9 +114,7 @@ internal static LoggingContext CreateLoggingContext(
ExplicitLoad = explicitLoad,
KillServerOnConcurrencyException = Options.Instance.LoggingDatabase.KillServerOnConcurrencyException,
LazyLoading = lazyLoading,
#if DEBUG
LoggerFactory = new IntersectLoggerFactory(nameof(LoggingContext)),
#endif
LoggerFactory = CreateLoggerFactory<LoggingContext>(Options.Instance.LoggingDatabase),
QueryTrackingBehavior = queryTrackingBehavior,
ReadOnly = readOnly,
});
Expand All @@ -146,26 +141,11 @@ public static PlayerContext CreatePlayerContext(
ExplicitLoad = explicitLoad,
KillServerOnConcurrencyException = Options.Instance.PlayerDatabase.KillServerOnConcurrencyException,
LazyLoading = lazyLoading,
#if DEBUG
LoggerFactory = new IntersectLoggerFactory(nameof(PlayerContext)),
#endif
LoggerFactory = CreateLoggerFactory<PlayerContext>(Options.Instance.PlayerDatabase),
QueryTrackingBehavior = queryTrackingBehavior,
ReadOnly = readOnly,
});

public static void InitializeDbLoggers()
{
if (Options.Instance.GameDatabase.LogLevel > LogLevel.None)
{
_gameDatabaseLogger = new IntersectLoggerFactory(nameof(GameContext)).CreateLogger<GameContext>();
}

if (Options.Instance.PlayerDatabase.LogLevel > LogLevel.None)
{
_playerDatabaseLogger = new IntersectLoggerFactory(nameof(PlayerContext)).CreateLogger<PlayerContext>();
}
}

//Check Directories
public static void CheckDirectories()
{
Expand Down Expand Up @@ -249,6 +229,24 @@ private static void ProcessMigrations<TContext>(TContext context)
context.OnSchemaMigrationsProcessed(processedSchemaMigrations.ToArray());
}

internal static ILoggerFactory CreateLoggerFactory<TDBContext>(DatabaseOptions databaseOptions)
where TDBContext : IntersectDbContext<TDBContext>
{
var contextName = typeof(TDBContext).Name;
var configuration = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Is(LevelConvert.ToSerilogLevel(databaseOptions.LogLevel))
.WriteTo.Console(restrictedToMinimumLevel: Debugger.IsAttached ? LogEventLevel.Warning : LogEventLevel.Error)
.WriteTo.File(path: $"logs/db-{contextName}.log").WriteTo.File(
path: $"logs/db-errors-{contextName}.log",
restrictedToMinimumLevel: LogEventLevel.Error,
rollOnFileSizeLimit: true,
retainedFileTimeLimit: TimeSpan.FromDays(30)
);

return new SerilogLoggerFactory(configuration.CreateLogger());
}

private static bool EnsureUpdated(IServerContext serverContext)
{
var gameDatabaseOptions = Options.Instance.GameDatabase;
Expand All @@ -262,7 +260,7 @@ private static bool EnsureUpdated(IServerContext serverContext)
DatabaseType = gameDatabaseOptions.Type,
EnableDetailedErrors = true,
EnableSensitiveDataLogging = true,
LoggerFactory = new IntersectLoggerFactory(nameof(GameContext)),
LoggerFactory = CreateLoggerFactory<GameContext>(gameDatabaseOptions),
});

var playerDatabaseOptions = Options.Instance.PlayerDatabase;
Expand All @@ -276,7 +274,7 @@ private static bool EnsureUpdated(IServerContext serverContext)
DatabaseType = playerDatabaseOptions.Type,
EnableDetailedErrors = true,
EnableSensitiveDataLogging = true,
LoggerFactory = new IntersectLoggerFactory(nameof(PlayerContext)),
LoggerFactory = CreateLoggerFactory<PlayerContext>(playerDatabaseOptions),
});

var loggingDatabaseOptions = Options.Instance.LoggingDatabase;
Expand All @@ -290,7 +288,7 @@ private static bool EnsureUpdated(IServerContext serverContext)
DatabaseType = loggingDatabaseOptions.Type,
EnableDetailedErrors = true,
EnableSensitiveDataLogging = true,
LoggerFactory = new IntersectLoggerFactory(nameof(LoggingContext)),
LoggerFactory = CreateLoggerFactory<LoggingContext>(loggingDatabaseOptions),
});

// We don't want anyone running the old migration tool accidentally
Expand Down Expand Up @@ -1957,9 +1955,9 @@ public static async Task Migrate<TContext>(DatabaseOptions fromDatabaseOptions,
DatabaseType = fromDatabaseOptions.Type,
ExplicitLoad = false,
LazyLoading = false,
LoggerFactory = default,
LoggerFactory = CreateLoggerFactory<TContext>(fromDatabaseOptions),
QueryTrackingBehavior = default,
ReadOnly = false
ReadOnly = false,
};

DatabaseOptions toDatabaseOptions;
Expand Down Expand Up @@ -2017,15 +2015,17 @@ public static async Task Migrate<TContext>(DatabaseOptions fromDatabaseOptions,
Port = port,
Database = database,
Username = username,
Password = password
Password = password,
LogLevel = fromDatabaseOptions.LogLevel,
};
toContextOptions = new()
{
ConnectionStringBuilder = toDatabaseType.CreateConnectionStringBuilder(
toDatabaseOptions,
default
),
DatabaseType = toDatabaseType
DatabaseType = toDatabaseType,
LoggerFactory = CreateLoggerFactory<TContext>(toDatabaseOptions),
};

try
Expand Down Expand Up @@ -2099,14 +2099,19 @@ public static async Task Migrate<TContext>(DatabaseOptions fromDatabaseOptions,
File.Delete(dbFileName);
}

toDatabaseOptions = new() { Type = toDatabaseType };
toDatabaseOptions = new()
{
LogLevel = fromDatabaseOptions.LogLevel,
Type = toDatabaseType,
};
toContextOptions = new()
{
ConnectionStringBuilder = toDatabaseType.CreateConnectionStringBuilder(
toDatabaseOptions,
dbFileName
),
DatabaseType = toDatabaseType
DatabaseType = toDatabaseType,
LoggerFactory = CreateLoggerFactory<TContext>(toDatabaseOptions),
};

break;
Expand Down
17 changes: 4 additions & 13 deletions Intersect.Server.Core/Database/IntersectDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
using Intersect.Core;
using Intersect.Framework.Reflection;
using Intersect.Server.Core;
#if DIAGNOSTIC
using Intersect.Server.Database.PlayerData;
#endif
using Intersect.Server.Database.PlayerData.Players;
using Intersect.Server.Entities;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -91,21 +88,15 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#endif

var loggerFactory = ContextOptions.LoggerFactory;
#if DIAGNOSTIC
if (this is PlayerContext)
{
loggerFactory ??= new IntersectLoggerFactory(GetType().GetName(qualified: true));
}
#endif

var enableSensitiveDataLogging = ContextOptions.EnableSensitiveDataLogging;
#if DIAGNOSTIC
enableSensitiveDataLogging = this is PlayerContext;
#if DEBUG
enableSensitiveDataLogging = Debugger.IsAttached;
#endif

var enableDetailedErrors = ContextOptions.EnableDetailedErrors;
#if DIAGNOSTIC
enableDetailedErrors = this is PlayerContext;
#if DEBUG
enableDetailedErrors = Debugger.IsAttached;
#endif

_ = optionsBuilder
Expand Down
49 changes: 0 additions & 49 deletions Intersect.Server.Core/Database/IntersectLoggerFactory.cs

This file was deleted.

5 changes: 5 additions & 0 deletions Intersect.Server.Core/Database/PlayerData/Players/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,18 @@ public Guild()
return null;
}


using var context = DbInterface.CreatePlayerContext(readOnly: false);

creator.Save(context);

var guild = new Guild
{
Name = name,
FoundingDate = DateTime.UtcNow,
GuildInstanceId = Guid.NewGuid(),
};
context.Guilds.Add(guild);

SlotHelper.ValidateSlotList(guild.Bank, Options.Instance.Guild.InitialBankSlots);

Expand Down
Loading