public static async Task ExecuteCommandAsync(this DbConnection connection, string commandText) { commandText = commandText.Trim(); try { var stopwatch = Stopwatch.StartNew(); #if (NET5_0) await using (var command = connection.CreateCommand()) #else using (var command = connection.CreateCommand()) #endif { command.CommandText = commandText; await command.ExecuteNonQueryAsync(); } if (LocalDbLogging.SqlLoggingEnabled) { LocalDbLogging.Log($@"Executed SQL ({stopwatch.ElapsedMilliseconds}.ms): {commandText.IndentLines()}"); } } catch (DbException exception) { throw BuildException(connection, commandText, exception); } catch (Exception exception) { throw BuildException(connection, commandText, exception); } }
LocalDbLoggingUsage() { #region LocalDbLoggingUsage LocalDbLogging.EnableVerbose(); #endregion #region LocalDbLoggingUsageSqlLogging LocalDbLogging.EnableVerbose(sqlLogging: true); #endregion }
public static void Initialize() { XunitLogging.Init(); LocalDbLogging.EnableVerbose(); SerializerBuilder.IgnoreMember <LocalDbInstanceInfo>(x => x.OwnerSID); SerializerBuilder.IgnoreMember <LocalDbInstanceInfo>(x => x.Connection); SerializerBuilder.IgnoreMember <LocalDbInstanceInfo>(x => x.LastStartUtc); SerializerBuilder.IgnoreMember <LocalDbInstanceInfo>(x => x.Build); SerializerBuilder.IgnoreMember <LocalDbInstanceInfo>(x => x.Major); SerializerBuilder.IgnoreMember <LocalDbInstanceInfo>(x => x.Minor); SerializerBuilder.IgnoreMember <LocalDbInstanceInfo>(x => x.Revision); }
public static void Log(MethodBase method, long milliseconds, string message) { if (!LocalDbLogging.Enabled) { return; } if (message == null) { LocalDbLogging.Log($"{method.Name} {milliseconds}ms"); return; } LocalDbLogging.Log($"{method.Name} {milliseconds}ms {message}"); }
public static void Initialize() { XunitContext.Init(); LocalDbLogging.EnableVerbose(); SharedVerifySettings.ModifySerialization(settings => { settings.IgnoreMember <LocalDbInstanceInfo>(x => x.OwnerSID); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Connection); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.LastStartUtc); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Build); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Major); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Minor); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Revision); }); }
public static void Initialize() { XunitContext.Init(); LocalDbLogging.EnableVerbose(); LocalDbSettings.ConnectionBuilder((instance, database) => $"Data Source=(LocalDb)\\{instance};Database={database};Pooling=true;Connection Timeout=300"); VerifierSettings.ScrubLinesContaining("filename = '"); VerifierSettings.ModifySerialization(settings => { settings.IgnoreMember <LocalDbInstanceInfo>(x => x.OwnerSID); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Connection); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.LastStartUtc); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Build); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Major); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Minor); settings.IgnoreMember <LocalDbInstanceInfo>(x => x.Revision); }); }
static IntegrationTests() { LocalDbLogging.EnableVerbose(sqlLogging: true); sqlInstance = new SqlInstance <EfFilterDbContext>( buildTemplate: async dbContext => { await dbContext.Database.EnsureCreatedAsync(); await dbContext.Database.ExecuteSqlCommandAsync( @"create view ParentEntityView as select Property from ParentEntities"); }, constructInstance: builder => { builder.AddFilters(); return(new EfFilterDbContext(builder.Options)); }); }
void InnerStart(DateTime timestamp, Func <SqlConnection, Task> buildTemplate) { void CleanStart() { FileExtensions.FlushDirectory(Directory); LocalDbApi.CreateInstance(instance); LocalDbApi.StartInstance(instance); startupTask = CreateAndDetachTemplate( timestamp, buildTemplate, rebuild: true, optimize: true); InitRollbackTask(); } var info = LocalDbApi.GetInstance(instance); if (!info.Exists) { CleanStart(); return; } if (!info.IsRunning) { LocalDbApi.DeleteInstance(instance); CleanStart(); return; } var templateLastMod = File.GetCreationTime(TemplateDataFile); if (timestamp == templateLastMod) { LocalDbLogging.Log("Not modified so skipping rebuild"); startupTask = CreateAndDetachTemplate(timestamp, buildTemplate, false, false); } else { startupTask = CreateAndDetachTemplate(timestamp, buildTemplate, true, false); } InitRollbackTask(); }
void InnerStart(DateTime timestamp, Action <SqlConnection> buildTemplate) { void CleanStart() { FileExtensions.FlushDirectory(directory); LocalDbApi.CreateInstance(instance); LocalDbApi.StartInstance(instance); RunOnceOfOptimizations(); CreateTemplate(); ExecuteBuildTemplate(buildTemplate); DetachTemplate(timestamp); } var info = LocalDbApi.GetInstance(instance); if (!info.Exists) { CleanStart(); return; } if (!info.IsRunning) { LocalDbApi.DeleteInstance(instance); CleanStart(); return; } PurgeDbs(); DeleteNonTemplateFiles(); var templateLastMod = File.GetCreationTime(TemplateDataFile); if (timestamp == templateLastMod) { LocalDbLogging.Log("Not modified so skipping rebuild"); return; } DeleteTemplateFiles(); CreateTemplate(); ExecuteBuildTemplate(buildTemplate); DetachTemplate(timestamp); }
public void Start(DateTime timestamp, Func <DbConnection, Task> buildTemplate) { #if RELEASE try { #endif var stopwatch = Stopwatch.StartNew(); InnerStart(timestamp, buildTemplate); var message = $"Start `{ServerName}` {stopwatch.ElapsedMilliseconds}ms."; LocalDbLogging.Log(message); #if RELEASE } catch (Exception exception) { throw ExceptionBuilder.WrapLocalDbFailure(instance, Directory, exception); } #endif }
async Task CreateAndDetachTemplate(DateTime timestamp, Func <DbConnection, Task> buildTemplate, bool rebuild, bool optimize) { masterConnection = buildConnection(MasterConnectionString); await masterConnection.OpenAsync(); var takeDbsOffline = ExecuteOnMasterAsync(SqlBuilder.TakeDbsOfflineCommand); if (LocalDbLogging.Enabled) { LocalDbLogging.Log($"SqlServerVersion: {masterConnection.ServerVersion}"); } if (optimize) { await ExecuteOnMasterAsync(SqlBuilder.GetOptimizationCommand(size)); } if (!rebuild || templateProvided) { await takeDbsOffline; return; } DeleteTemplateFiles(); await ExecuteOnMasterAsync(SqlBuilder.GetCreateTemplateCommand(TemplateDataFile, TemplateLogFile)); FileExtensions.MarkFileAsWritable(TemplateDataFile); FileExtensions.MarkFileAsWritable(TemplateLogFile); using (var templateConnection = buildConnection(TemplateConnectionString)) { await templateConnection.OpenAsync(); await buildTemplate(templateConnection); } await ExecuteOnMasterAsync(SqlBuilder.DetachTemplateCommand); File.SetCreationTime(TemplateDataFile, timestamp); await takeDbsOffline; }
static void WriteLine(CommandEventData data) { LocalDbLogging.Log($"EF {data}"); }
LocalDbLoggingUsage() { #region LocalDbLoggingUsage LocalDbLogging.EnableVerbose(); #endregion }