Adds a queue to the SQL log and fixes some issues pointed out
This commit is contained in:
parent
0459dfca11
commit
8ff36c6709
4 changed files with 65 additions and 33 deletions
|
|
@ -42,11 +42,6 @@ namespace TShockAPI
|
||||||
/// </summary>
|
/// </summary>
|
||||||
string Name { get; }
|
string Name { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the ILog is using SQL or false if not
|
|
||||||
/// </summary>
|
|
||||||
bool Sql { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks whether the log level contains the specified flag.
|
/// Checks whether the log level contains the specified flag.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -175,14 +175,6 @@ namespace TShockAPI
|
||||||
Debug(String.Format(format, args));
|
Debug(String.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disposes objects that are being used.
|
|
||||||
/// </summary>
|
|
||||||
public static void Dispose()
|
|
||||||
{
|
|
||||||
TShock.Log.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Internal method which writes a message directly to the log file.
|
/// Internal method which writes a message directly to the log file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,29 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
using TShockAPI.DB;
|
using TShockAPI.DB;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace TShockAPI
|
||||||
{
|
{
|
||||||
|
struct LogInfo
|
||||||
|
{
|
||||||
|
public string timestamp;
|
||||||
|
public string message;
|
||||||
|
public string caller;
|
||||||
|
public LogLevel logLevel;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return String.Format("Message: {0}: {1}: {2}",
|
||||||
|
caller, logLevel.ToString().ToUpper(), message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class inheriting ILog for writing logs to TShock's SQL database
|
/// Class inheriting ILog for writing logs to TShock's SQL database
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -32,7 +48,7 @@ namespace TShockAPI
|
||||||
private readonly LogLevel _logLevel;
|
private readonly LogLevel _logLevel;
|
||||||
private readonly IDbConnection _database;
|
private readonly IDbConnection _database;
|
||||||
private readonly TextLog _backupLog;
|
private readonly TextLog _backupLog;
|
||||||
private int _failures;
|
private readonly List<LogInfo> _failures = new List<LogInfo>(TShock.Config.RevertToTextLogsOnSqlFailures);
|
||||||
private bool _useTextLog;
|
private bool _useTextLog;
|
||||||
|
|
||||||
public string Name
|
public string Name
|
||||||
|
|
@ -40,11 +56,6 @@ namespace TShockAPI
|
||||||
get { return "SQL Log Writer"; }
|
get { return "SQL Log Writer"; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Sql
|
|
||||||
{
|
|
||||||
get { return true; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public SqlLog(LogLevel logLevel, IDbConnection db, string textlogFilepath, bool clearTextLog)
|
public SqlLog(LogLevel logLevel, IDbConnection db, string textlogFilepath, bool clearTextLog)
|
||||||
{
|
{
|
||||||
_logLevel = logLevel;
|
_logLevel = logLevel;
|
||||||
|
|
@ -218,25 +229,61 @@ namespace TShockAPI
|
||||||
_backupLog.Write(message, level);
|
_backupLog.Write(message, level);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)",
|
_database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)",
|
||||||
level, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
|
level, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
|
||||||
caller, message);
|
caller, message);
|
||||||
|
|
||||||
if (_failures > 0)
|
if (_failures.Count > 0)
|
||||||
_failures--;
|
{
|
||||||
|
var info = _failures.Last();
|
||||||
|
var success = true;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)",
|
||||||
|
info.logLevel, info.timestamp, info.caller, info.message);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
success = false;
|
||||||
|
_failures.Add(new LogInfo
|
||||||
|
{
|
||||||
|
caller = "TShock",
|
||||||
|
logLevel = LogLevel.Error,
|
||||||
|
message = String.Format("SQL Log insert query failed: {0}", ex),
|
||||||
|
timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
_failures.RemoveAt(_failures.Count - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_backupLog.ConsoleError("SQL Log insert query failed: {0}", ex);
|
_backupLog.ConsoleError("SQL Log insert query failed: {0}", ex);
|
||||||
_failures++;
|
|
||||||
_backupLog.Error("SQL logging will revert to text logging if {0} more failures occur.",
|
|
||||||
TShock.Config.RevertToTextLogsOnSqlFailures - _failures);
|
|
||||||
|
|
||||||
if (_failures >= TShock.Config.RevertToTextLogsOnSqlFailures)
|
_failures.Add(new LogInfo
|
||||||
{
|
{
|
||||||
_useTextLog = true;
|
logLevel = level,
|
||||||
_backupLog.ConsoleError("SQL Logging disabled due to errors. Reverting to text logging.");
|
message = message,
|
||||||
|
caller = caller,
|
||||||
|
timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_failures.Count >= TShock.Config.RevertToTextLogsOnSqlFailures)
|
||||||
|
{
|
||||||
|
_useTextLog = true;
|
||||||
|
_backupLog.ConsoleError("SQL Logging disabled due to errors. Reverting to text logging.");
|
||||||
|
|
||||||
|
for (var i = _failures.Count - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
_backupLog.Write(String.Format("SQL log failed at: {0}. {1}", _failures[i].timestamp, _failures[i]),
|
||||||
|
LogLevel.Error);
|
||||||
}
|
}
|
||||||
|
_failures.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -157,9 +157,9 @@ namespace TShockAPI
|
||||||
{
|
{
|
||||||
logPathSetupWarning =
|
logPathSetupWarning =
|
||||||
"Could not apply the given log path / log format, defaults will be used. Exception details:\n" + ex;
|
"Could not apply the given log path / log format, defaults will be used. Exception details:\n" + ex;
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
|
||||||
Console.WriteLine(logPathSetupWarning);
|
ServerApi.LogWriter.PluginWriteLine(this, logPathSetupWarning, TraceLevel.Error);
|
||||||
Console.ForegroundColor = ConsoleColor.Gray;
|
|
||||||
// Problem with the log path or format use the default
|
// Problem with the log path or format use the default
|
||||||
logFilename = Path.Combine(LogPathDefault, now.ToString(LogFormatDefault) + ".log");
|
logFilename = Path.Combine(LogPathDefault, now.ToString(LogFormatDefault) + ".log");
|
||||||
}
|
}
|
||||||
|
|
@ -197,9 +197,7 @@ namespace TShockAPI
|
||||||
}
|
}
|
||||||
catch (MySqlException ex)
|
catch (MySqlException ex)
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
ServerApi.LogWriter.PluginWriteLine(this, ex.ToString(), TraceLevel.Error);
|
||||||
Console.WriteLine(ex);
|
|
||||||
Console.ResetColor();
|
|
||||||
throw new Exception("MySql not setup correctly");
|
throw new Exception("MySql not setup correctly");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue