///// <summary> ///// Changes the config file value. ///// </summary> ///// <param name="name"> ///// The name. ///// </param> ///// <param name="newvalue"> ///// The new value. ///// </param> ///// <returns> ///// A value indicating weather the change was successful or not. ///// </returns> //public static bool ChangeConfigFileValue(string name, string newvalue) //{ // try // { // XmlDocument xmlDoc = new XmlDocument(); // xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); // foreach (XmlElement element in xmlDoc.DocumentElement) // { // if (element.Name.Equals("applicationSettings")) // { // foreach (XmlNode node in element.ChildNodes) // { // foreach (XmlNode node2 in node.ChildNodes) // { // if (node2.Attributes.Count > 0 && node2.Attributes[0].Value.Equals(name)) // { // foreach (XmlNode node3 in node2.ChildNodes) // { // if (node3.Name == "value") // { // node3.InnerText = newvalue; // xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); // ConfigurationManager.RefreshSection("applicationSettings"); // return true; // } // } // XmlElement newelement2 = xmlDoc.CreateElement("value"); // newelement2.InnerText = newvalue; // node2.AppendChild(newelement2); // xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); // ConfigurationManager.RefreshSection("applicationSettings"); // return true; // } // } // } // } // } // } // catch (Exception ex) // { // FileOperation.LogError(ex, FileOperation.MaxLogFileSize); // } // return false; //} /// <summary> /// Compresses the file to ZIP. /// </summary> /// <param name="path"> /// The path. /// </param> /// <param name="fileToCompress"> /// The file to compress. /// </param> /// <param name="compressedFileName"> /// Name of the compressed file. /// </param> /// <returns> /// A boolean indicating that the file was zipped was successful. /// </returns> public static bool CompressFileToZIP(string path, string fileToCompress, string compressedFileName) { string srcFile = path.EndsWith("\\") ? path + fileToCompress : path + "\\" + fileToCompress; string dstFile = path.EndsWith("\\") ? path + compressedFileName : path + "\\" + compressedFileName; try { var zipfile = new ZipFile(dstFile); zipfile.CompressionLevel = Ionic.Zlib.CompressionLevel.Default; zipfile.CompressionMethod = CompressionMethod.Deflate; if (zipfile.ContainsEntry(srcFile)) { zipfile.RemoveEntry(srcFile); zipfile.AddFile(srcFile); } else { zipfile.AddFile(srcFile, string.Empty); } zipfile.Save(); zipfile = null; return(true); } catch (Exception ex) { FileOperation.LogError(ex, FileOperation.MaxLogFileSize); } return(false); }
/// <summary> /// The run. /// </summary> private void Run() { while (this.enabled) { try { System.Threading.Thread.Sleep(2000); List <FileWriterObject> fwol_write; lock (lockList) { fwol_write = this.fileWriterObjects; this.fileWriterObjects = new List <FileWriterObject>(100); } int counter = fwol_write.Count; for (int i = 0; i < counter; i++) { if (fwol_write[i] != null) { FileWriterObject fwo = fwol_write[i]; string path = fwo.Path; string fileName = fwo.FileName; StringBuilder logText = new StringBuilder(512 * 1024); logText.Append(fwo.Text); int maxFileSize = fwo.MaxFileSize; i++; while (i < counter && fwol_write[i].Path == path && fwol_write[i].FileName == fileName && logText.Length < 512 * 1024) { logText.Append(fwol_write[i].Text); i++; } if (i < counter) { i--; } this.WriteTextToFile(path, fileName, logText.ToString(), maxFileSize); } } } catch (Exception ex) { FileOperation.LogError(ex, FileOperation.MaxLogFileSize); } } }
/// <summary> /// Called when [reorganize or rebuild indices timer elapsed]. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param> private void OnReorganizeOrRebuildIndicesTimerElapsed(object sender, ElapsedEventArgs e) { this.timerReorganizeOrRebuildIndices.Stop(); this.timerReorganizeOrRebuildIndices.Interval = 3600000; try { ReOrganizeIndices(); } catch (Exception ex) { FileOperation.LogError(ex, FileOperation.MaxLogFileSize); } this.timerReorganizeOrRebuildIndices.Start(); }
/// <summary> /// Initializes a new instance of the <see cref="DataBaseFunctions" /> class. /// </summary> /// <param name="theDatabase">The database object <see cref="DataBase.Database" /> which is used to access the database.</param> /// <exception cref="System.Exception">Database connection failed! Terminating.</exception> public DataBaseFunctions(Database theDatabase) { Database = theDatabase; while (true) { if (!theDatabase.IsAvailable()) { int waitingTime = 5000; System.Threading.Thread.Sleep(waitingTime); FileOperation.LogError($"Database is not available. Retry in {waitingTime} ms", 128 * 1024 * 1024); } else { break; } } FileOperation.MaxLogFileSize = GetConfigValue <int>("MaxLogFileSize"); this.timerReorganizeOrRebuildIndices.Elapsed += this.OnReorganizeOrRebuildIndicesTimerElapsed; this.timerReorganizeOrRebuildIndices.AutoReset = false; this.timerReorganizeOrRebuildIndices.Start(); }
/// <summary> /// Query that returns no data but number of affected rows. This query works transactional, changes done are will be /// rolled back in case of errors. /// </summary> /// <param name="sqlStatement">The SQL statement.</param> /// <returns> /// The result of NonQuery. -4 general exception, -3 exception on rollback, -2 rollback done, all other depend on the /// SQL /// statement itself. /// </returns> public int NonQueryWithTransaction(string sqlStatement) { int retryCount = 0; int result = 0; if (this.ConnectionType == ConnType.MSSQL) { while (true) { try { using (var con = new SqlConnection(this.connectionStringBuilderForSql.ConnectionString)) { con.Open(); var adapter = new SqlDataAdapter(); SqlTransaction transaction = con.BeginTransaction(); try { adapter.SelectCommand = new SqlCommand(sqlStatement, con); adapter.SelectCommand.CommandTimeout = SqlCmdTimeout; adapter.SelectCommand.Transaction = transaction; result = adapter.SelectCommand.ExecuteNonQuery(); transaction.Commit(); } catch (Exception ex) { if (ex is SqlException && Enum.IsDefined(typeof(RetryableSqlErrors), ((SqlException)ex).Number) && retryCount < MaxRetry) { retryCount++; Thread.Sleep(((SqlException)ex).Number == (int)RetryableSqlErrors.Timeout ? LongWait : ShortWait); continue; } else { result = -2; this.WriteErrorLog(ex.ToString(), sqlStatement); // Attempt to roll back the transaction. try { transaction.Rollback(); throw; } catch (Exception ex2) { result = -3; // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail, such as // a closed connection. FileOperation.LogError(ex2, FileOperation.MaxLogFileSize); throw; } } } } break; } catch (Exception ex) { if (ex is SqlException && Enum.IsDefined(typeof(RetryableSqlErrors), ((SqlException)ex).Number) && retryCount < MaxRetry) { retryCount++; Thread.Sleep(((SqlException)ex).Number == (int)RetryableSqlErrors.Timeout ? LongWait : ShortWait); continue; } this.WriteErrorLog(ex.ToString(), sqlStatement); throw; } } } else if (this.ConnectionType == ConnType.ODBC) { using (var con = new OdbcConnection(this.connectionStringBuilderForOdbc.ConnectionString)) { con.Open(); var adapter = new OdbcDataAdapter(); OdbcTransaction transaction = con.BeginTransaction(); try { adapter.SelectCommand = new OdbcCommand(sqlStatement, con); adapter.SelectCommand.CommandTimeout = SqlCmdTimeout; adapter.SelectCommand.Transaction = transaction; result = adapter.SelectCommand.ExecuteNonQuery(); transaction.Commit(); } catch (Exception ex) { result = -2; this.WriteErrorLog(ex.ToString(), sqlStatement); // Attempt to roll back the transaction. try { transaction.Rollback(); throw; } catch (Exception ex2) { result = -3; // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail, such as // a closed connection. FileOperation.LogError(ex2, FileOperation.MaxLogFileSize); throw; } } } } else if (this.ConnectionType == ConnType.OLEDB) { using (var con = new OleDbConnection(this.connectionStringBuilderForOledb.ConnectionString)) { con.Open(); var adapter = new OleDbDataAdapter(); OleDbTransaction transaction = con.BeginTransaction(); try { adapter.SelectCommand = new OleDbCommand(sqlStatement, con); adapter.SelectCommand.CommandTimeout = SqlCmdTimeout; adapter.SelectCommand.Transaction = transaction; result = adapter.SelectCommand.ExecuteNonQuery(); transaction.Commit(); } catch (Exception ex) { result = -2; this.WriteErrorLog(ex.ToString(), sqlStatement); // Attempt to roll back the transaction. try { transaction.Rollback(); throw; } catch (Exception ex2) { result = -3; // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail, such as // a closed connection. FileOperation.LogError(ex2, FileOperation.MaxLogFileSize); throw; } } } } return(result); }
/// <summary> /// Writes an error into 'ErrorLog.txt' text file. /// </summary> /// <param name="message"> /// The Error message. /// </param> /// <param name="strCommand"> /// SQL command. /// </param> private void WriteErrorLog(string message, string strCommand) { FileOperation.LogError("Message: \r\n" + message + "\r\n Command: \r\n" + strCommand, FileOperation.MaxLogFileSize); }