Esempio n. 1
0
        private void SaveLog(SeverityLevel level, string text)
        {
            _connection = ConnectionInstance.Instance.GetConnection(_loggerConnectionString);
            text        = text.Replace("'", "''");
            text        = text.Replace("\"", "\\\"");
            var command = new OdbcCommand($"INSERT INTO `Log` (`Level`, `Message`, `Timestamp`) VALUES('{LevelNames[level]}', '{text}', '{DateTime.Now}')")
            {
                Connection = _connection
            };

            try
            {
                if (_connection.State == ConnectionState.Closed)
                {
                    _connection.Open();
                    if (_connection.State == ConnectionState.Open)
                    {
                        command.ExecuteNonQuery();
                    }
                }
            }
            finally
            {
                ConnectionInstance.CloseConnection(_loggerConnectionString);
            }
        }
Esempio n. 2
0
        public static void PurgeLog()
        {
            var _connection = ConnectionInstance.Instance.GetConnection($"DSN={DsnName}");
            var command     = new OdbcCommand($"DELETE * FROM `{LogTable}` WHERE `Timestamp` < {DeadDate.ToOADate()}")
            {
                Connection = _connection
            };

            _connection.Open();
            try
            {
                int affectedRows = command.ExecuteNonQuery();

                if (affectedRows > 0)
                {
                    Logger.Instance.Debug($"Purged {affectedRows} log entries that had timestamps before {DeadDate}.");
                }
            }
            catch (OdbcException e)
            {
                Logger.Instance.Error($"An error occured while purging log entries with timestamps before {DeadDate}: {e.Message}");
            }
            finally
            {
                ConnectionInstance.CloseConnection($"DSN={DsnName}");
            }
        }
Esempio n. 3
0
        public DataTable LoadLog()
        {
            var    _connection = ConnectionInstance.Instance.GetConnection($"DSN={DsnName}");
            string query       = $"SELECT `Timestamp`, `Level`, `Message` FROM `{LogTable}` WHERE `Level` NOT LIKE 'DEBUG' ORDER BY `Timestamp` DESC";

            if (Settings.DebugMode)
            {
                query = $"SELECT `Timestamp`, `Level`, `Message` FROM `{LogTable}` ORDER BY `Timestamp` DESC";
            }
            _connection.Open();
            OdbcDataAdapter adapter = new OdbcDataAdapter(query, _connection);
            DataTable       table   = new DataTable();

            try
            {
                adapter.Fill(table);
                table.DefaultView.AllowDelete = false;
                table.DefaultView.AllowEdit   = false;
                table.DefaultView.AllowNew    = false;

                return(table);
            }
            catch (Exception e)
            {
                Logger.Instance.Error($"An error occured while retrieving the log: {e.Message}");
            }
            finally
            {
                ConnectionInstance.CloseConnection($"DSN={DsnName}");
            }

            var           mapping    = new Mapping();
            List <string> logColumns = mapping.GetColumns(LogTable, DsnName);
            DataTable     logTable   = new DataTable();

            foreach (string logColumn in logColumns)
            {
                logTable.Columns.Add(logColumn);
            }

            logTable.DefaultView.AllowDelete = false;
            logTable.DefaultView.AllowEdit   = false;
            logTable.DefaultView.AllowNew    = false;

            return(logTable);
        }