/// <summary> /// Logs the StageLogEntry to the implementation-specific persistence mechanism. /// In this implementation, it logs to a Sql Table (DataMigrationPhaseLog) in the cpms /// database in Sql Server. /// </summary> /// <param name="entry">A StageLogEntry with all the information needed to produce /// a useful stage log entry.</param> public void Log(PhaseLogEntry entry) { entries.Add(entry); var sqlCommand = String.Format(sqlCommandFormat, tableName, entry.LogSource, String.IsNullOrEmpty(entry.DataSourceCode) ? "null" : entry.DataSourceCode, entry.LoadNumber.HasValue ? entry.LoadNumber.ToString() : "null", entry.PhaseNumber, entry.NumberOfRecords.HasValue ? entry.NumberOfRecords.Value.ToString() : "null", entry.Description.Replace("'", "''")); // Use two single quotes. // It is useful to redundantly use the log4net log. log.Debug(entry.Description); try { using (var connection = new SqlConnection(ConnectionString)) { connection.Open(); using (var command = new SqlCommand(sqlCommand, connection)) { command.ExecuteNonQuery(); } } } catch (Exception ex) { // We're not going to fail the load for this, but we want it to be clear. log.ErrorFormat("Log - error attempting to log staging message = {0}", ex.Message); } }
/// <summary> /// Calls into Sql to get the actual list of Phase Log Entries. The cached set that /// we have is just an in-memory backup strategy, in case this fails. It only has the /// phase log entries that were done from the C#, not the ones done by T-SQL based /// Post-Processing stages. If this function is successful, it replaces the cached /// list of entries, so the entries can be more complete. /// </summary> /// <param name="loadNumber">Load number.</param> void AttemptToLoadTheEntriesStoresInSql(int loadNumber) { if (loadNumber == -1) { // Probably an error got caught before a loadnumber could be propagated back // to the MonitoringPublisher. This is fine, the PhaseLog entries are a nice //-to-have, but not essential. return; } var sqlCommand = String.Format("select * from DataMigrationPhaseLog where " + "loadnumber= {0} order by RecordDate", loadNumber); try { using (var connection = new SqlConnection(ConnectionString)) { connection.Open(); using (var command = new SqlCommand(sqlCommand, connection)) { var entriesFromSql = new List<PhaseLogEntry>(); var reader = command.ExecuteReader(); while (reader.Read()) { var entry = new PhaseLogEntry(); entry.LogSource = (int) reader["LogSource"]; entry.DataSourceCode = (string) reader["DataSourceCode"]; entry.LoadNumber = loadNumber; entry.PhaseNumber = GetNullableNumericField(reader, "Phase"); entry.NumberOfRecords = GetNullableNumericField(reader, "NumberOfRecords"); entry.Description = (string) reader["Description"]; entriesFromSql.Add(entry); } // We succeeded, so replace the cached list of entries with ones from // Sql, which are more complete. entries = entriesFromSql; } } } catch (Exception ex) { // We're not going to fail the load for this, but we want it to be clear. log.ErrorFormat("Log - error attempting to log staging message = {0}", ex.Message); } }