Merge pull request #891 from WhiteXZ/general-devel
SQL log actually ensures its table exists
This commit is contained in:
commit
42a505e18f
1 changed files with 22 additions and 8 deletions
|
|
@ -22,6 +22,7 @@ using System.Data;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
using TShockAPI.DB;
|
using TShockAPI.DB;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace TShockAPI
|
||||||
|
|
@ -55,7 +56,7 @@ namespace TShockAPI
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the database connection and the initial log level.
|
/// Sets the database connection and the initial log level.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="db"></param>
|
/// <param name="db">Database connection</param>
|
||||||
/// <param name="textlogFilepath">File path to a backup text log in case the SQL log fails</param>
|
/// <param name="textlogFilepath">File path to a backup text log in case the SQL log fails</param>
|
||||||
/// <param name="clearTextLog"></param>
|
/// <param name="clearTextLog"></param>
|
||||||
public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog)
|
public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog)
|
||||||
|
|
@ -63,6 +64,20 @@ namespace TShockAPI
|
||||||
FileName = string.Format("{0}://database", db.GetSqlType());
|
FileName = string.Format("{0}://database", db.GetSqlType());
|
||||||
_database = db;
|
_database = db;
|
||||||
_backupLog = new TextLog(textlogFilepath, clearTextLog);
|
_backupLog = new TextLog(textlogFilepath, clearTextLog);
|
||||||
|
|
||||||
|
var table = new SqlTable("Logs",
|
||||||
|
new SqlColumn("ID", MySqlDbType.Int32) {AutoIncrement = true, Primary = true},
|
||||||
|
new SqlColumn("TimeStamp", MySqlDbType.Text),
|
||||||
|
new SqlColumn("LogLevel", MySqlDbType.Int32),
|
||||||
|
new SqlColumn("Caller", MySqlDbType.Text),
|
||||||
|
new SqlColumn("Message", MySqlDbType.Text)
|
||||||
|
);
|
||||||
|
|
||||||
|
var creator = new SqlTableCreator(db,
|
||||||
|
db.GetSqlType() == SqlType.Sqlite
|
||||||
|
? (IQueryBuilder) new SqliteQueryCreator()
|
||||||
|
: new MysqlQueryCreator());
|
||||||
|
creator.EnsureTableStructure(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool MayWriteType(TraceLevel type)
|
public bool MayWriteType(TraceLevel type)
|
||||||
|
|
@ -232,9 +247,8 @@ namespace TShockAPI
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)",
|
_database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)",
|
||||||
level, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
|
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, (int)level, message);
|
||||||
caller, message);
|
|
||||||
|
|
||||||
var success = true;
|
var success = true;
|
||||||
while (_failures.Count > 0 && success)
|
while (_failures.Count > 0 && success)
|
||||||
|
|
@ -243,8 +257,8 @@ namespace TShockAPI
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)",
|
_database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)",
|
||||||
info.logLevel, info.timestamp, info.caller, info.message);
|
info.timestamp, info.caller, (int)info.logLevel, info.message);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -254,7 +268,7 @@ namespace TShockAPI
|
||||||
caller = "TShock",
|
caller = "TShock",
|
||||||
logLevel = TraceLevel.Error,
|
logLevel = TraceLevel.Error,
|
||||||
message = string.Format("SQL Log insert query failed: {0}", ex),
|
message = string.Format("SQL Log insert query failed: {0}", ex),
|
||||||
timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
|
timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -271,7 +285,7 @@ namespace TShockAPI
|
||||||
logLevel = level,
|
logLevel = level,
|
||||||
message = message,
|
message = message,
|
||||||
caller = caller,
|
caller = caller,
|
||||||
timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
|
timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue