Add FileName to ILog interface. Also fixes some missing XML warnings
This commit is contained in:
parent
6db4bab556
commit
cb46221409
3 changed files with 69 additions and 46 deletions
|
|
@ -20,27 +20,57 @@ using System;
|
||||||
|
|
||||||
namespace TShockAPI
|
namespace TShockAPI
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Flags to define which types of message are logged
|
||||||
|
/// </summary>
|
||||||
[Flags]
|
[Flags]
|
||||||
public enum LogLevel
|
public enum LogLevel
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// No messages will be logged
|
||||||
|
/// </summary>
|
||||||
None = 0,
|
None = 0,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Debug messages will be logged
|
||||||
|
/// </summary>
|
||||||
Debug = 1,
|
Debug = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Informative messages will be logged
|
||||||
|
/// </summary>
|
||||||
Info = 2,
|
Info = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Warning message will be logged
|
||||||
|
/// </summary>
|
||||||
Warning = 4,
|
Warning = 4,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error messages will be logged
|
||||||
|
/// </summary>
|
||||||
Error = 8,
|
Error = 8,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Data messages will be logged
|
||||||
|
/// </summary>
|
||||||
Data = 16,
|
Data = 16,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// All messages will be logged
|
||||||
|
/// </summary>
|
||||||
All = 31
|
All = 31
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Logging interface
|
/// Logging interface
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ILog
|
public interface ILog
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Log name
|
/// Log file name
|
||||||
/// </summary>
|
/// </summary>
|
||||||
string Name { get; }
|
string FileName { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks whether the log level contains the specified flag.
|
/// Checks whether the log level contains the specified flag.
|
||||||
|
|
@ -57,7 +87,7 @@ namespace TShockAPI
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an informative string to the log and to the console.
|
/// Writes an informative string to the log and to the console.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The format of the message to be written.</param>
|
/// <param name="format">The format of the message to be written.</param>
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
void ConsoleInfo(string format, params object[] args);
|
void ConsoleInfo(string format, params object[] args);
|
||||||
|
|
||||||
|
|
@ -92,6 +122,7 @@ namespace TShockAPI
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
void Error(string message);
|
void Error(string message);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an error to the log.
|
/// Writes an error to the log.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ namespace TShockAPI
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return String.Format("Message: {0}: {1}: {2}",
|
return string.Format("Message: {0}: {1}: {2}",
|
||||||
caller, logLevel.ToString().ToUpper(), message);
|
caller, logLevel.ToString().ToUpper(), message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -51,13 +51,18 @@ namespace TShockAPI
|
||||||
private readonly List<LogInfo> _failures = new List<LogInfo>(TShock.Config.RevertToTextLogsOnSqlFailures);
|
private readonly List<LogInfo> _failures = new List<LogInfo>(TShock.Config.RevertToTextLogsOnSqlFailures);
|
||||||
private bool _useTextLog;
|
private bool _useTextLog;
|
||||||
|
|
||||||
public string Name
|
public string FileName { get; set; }
|
||||||
{
|
|
||||||
get { return "SQL Log Writer"; }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the database connection and the initial log level.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logLevel"></param>
|
||||||
|
/// <param name="db"></param>
|
||||||
|
/// <param name="textlogFilepath">File path to a backup text log in case the SQL log fails</param>
|
||||||
|
/// <param name="clearTextLog"></param>
|
||||||
public SqlLog(LogLevel logLevel, IDbConnection db, string textlogFilepath, bool clearTextLog)
|
public SqlLog(LogLevel logLevel, IDbConnection db, string textlogFilepath, bool clearTextLog)
|
||||||
{
|
{
|
||||||
|
FileName = string.Format("{0}://database", db.GetSqlType());
|
||||||
_logLevel = logLevel;
|
_logLevel = logLevel;
|
||||||
_database = db;
|
_database = db;
|
||||||
_backupLog = new TextLog(textlogFilepath, logLevel, clearTextLog);
|
_backupLog = new TextLog(textlogFilepath, logLevel, clearTextLog);
|
||||||
|
|
@ -72,7 +77,7 @@ namespace TShockAPI
|
||||||
/// Writes data to the log file.
|
/// Writes data to the log file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
public void Data(String message)
|
public void Data(string message)
|
||||||
{
|
{
|
||||||
Write(message, LogLevel.Data);
|
Write(message, LogLevel.Data);
|
||||||
}
|
}
|
||||||
|
|
@ -84,14 +89,14 @@ namespace TShockAPI
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
public void Data(string format, params object[] args)
|
public void Data(string format, params object[] args)
|
||||||
{
|
{
|
||||||
Data(String.Format(format, args));
|
Data(string.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an error to the log file.
|
/// Writes an error to the log file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
public void Error(String message)
|
public void Error(string message)
|
||||||
{
|
{
|
||||||
Write(message, LogLevel.Error);
|
Write(message, LogLevel.Error);
|
||||||
}
|
}
|
||||||
|
|
@ -103,14 +108,14 @@ namespace TShockAPI
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
public void Error(string format, params object[] args)
|
public void Error(string format, params object[] args)
|
||||||
{
|
{
|
||||||
Error(String.Format(format, args));
|
Error(string.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an error to the log file.
|
/// Writes an error to the log file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
public void ConsoleError(String message)
|
public void ConsoleError(string message)
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
Console.WriteLine(message);
|
Console.WriteLine(message);
|
||||||
|
|
@ -125,14 +130,14 @@ namespace TShockAPI
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
public void ConsoleError(string format, params object[] args)
|
public void ConsoleError(string format, params object[] args)
|
||||||
{
|
{
|
||||||
ConsoleError(String.Format(format, args));
|
ConsoleError(string.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes a warning to the log file.
|
/// Writes a warning to the log file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
public void Warn(String message)
|
public void Warn(string message)
|
||||||
{
|
{
|
||||||
Write(message, LogLevel.Warning);
|
Write(message, LogLevel.Warning);
|
||||||
}
|
}
|
||||||
|
|
@ -144,14 +149,14 @@ namespace TShockAPI
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
public void Warn(string format, params object[] args)
|
public void Warn(string format, params object[] args)
|
||||||
{
|
{
|
||||||
Warn(String.Format(format, args));
|
Warn(string.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an informative string to the log file.
|
/// Writes an informative string to the log file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
public void Info(String message)
|
public void Info(string message)
|
||||||
{
|
{
|
||||||
Write(message, LogLevel.Info);
|
Write(message, LogLevel.Info);
|
||||||
}
|
}
|
||||||
|
|
@ -163,14 +168,14 @@ namespace TShockAPI
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
public void Info(string format, params object[] args)
|
public void Info(string format, params object[] args)
|
||||||
{
|
{
|
||||||
Info(String.Format(format, args));
|
Info(string.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an informative string to the log file. Also outputs to the console.
|
/// Writes an informative string to the log file. Also outputs to the console.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
public void ConsoleInfo(String message)
|
public void ConsoleInfo(string message)
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
Console.WriteLine(message);
|
Console.WriteLine(message);
|
||||||
|
|
@ -185,14 +190,14 @@ namespace TShockAPI
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
public void ConsoleInfo(string format, params object[] args)
|
public void ConsoleInfo(string format, params object[] args)
|
||||||
{
|
{
|
||||||
ConsoleInfo(String.Format(format, args));
|
ConsoleInfo(string.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes a debug string to the log file.
|
/// Writes a debug string to the log file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">The message to be written.</param>
|
/// <param name="message">The message to be written.</param>
|
||||||
public void Debug(String message)
|
public void Debug(string message)
|
||||||
{
|
{
|
||||||
Write(message, LogLevel.Debug);
|
Write(message, LogLevel.Debug);
|
||||||
}
|
}
|
||||||
|
|
@ -204,7 +209,7 @@ namespace TShockAPI
|
||||||
/// <param name="args">The format arguments.</param>
|
/// <param name="args">The format arguments.</param>
|
||||||
public void Debug(string format, params object[] args)
|
public void Debug(string format, params object[] args)
|
||||||
{
|
{
|
||||||
Debug(String.Format(format, args));
|
Debug(string.Format(format, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(string message, LogLevel level)
|
public void Write(string message, LogLevel level)
|
||||||
|
|
@ -251,7 +256,7 @@ namespace TShockAPI
|
||||||
{
|
{
|
||||||
caller = "TShock",
|
caller = "TShock",
|
||||||
logLevel = LogLevel.Error,
|
logLevel = LogLevel.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.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -278,9 +283,9 @@ namespace TShockAPI
|
||||||
_useTextLog = true;
|
_useTextLog = true;
|
||||||
_backupLog.ConsoleError("SQL Logging disabled due to errors. Reverting to text logging.");
|
_backupLog.ConsoleError("SQL Logging disabled due to errors. Reverting to text logging.");
|
||||||
|
|
||||||
foreach(var logInfo in _failures)
|
foreach (var logInfo in _failures)
|
||||||
{
|
{
|
||||||
_backupLog.Write(String.Format("SQL log failed at: {0}. {1}", logInfo.timestamp, logInfo),
|
_backupLog.Write(string.Format("SQL log failed at: {0}. {1}", logInfo.timestamp, logInfo),
|
||||||
LogLevel.Error);
|
LogLevel.Error);
|
||||||
}
|
}
|
||||||
_failures.Clear();
|
_failures.Clear();
|
||||||
|
|
|
||||||
|
|
@ -33,22 +33,9 @@ namespace TShockAPI
|
||||||
private readonly LogLevel _logLevel;
|
private readonly LogLevel _logLevel;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Log file name
|
/// File name of the Text log
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string fileName { get; private set; }
|
public string FileName { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Name of the TextLog
|
|
||||||
/// </summary>
|
|
||||||
public string Name
|
|
||||||
{
|
|
||||||
get { return "Text Log Writer"; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Sql
|
|
||||||
{
|
|
||||||
get { return false; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the log file stream and sets the initial log level.
|
/// Creates the log file stream and sets the initial log level.
|
||||||
|
|
@ -58,7 +45,7 @@ namespace TShockAPI
|
||||||
/// <param name="clear">Whether or not to clear the log file on initialization.</param>
|
/// <param name="clear">Whether or not to clear the log file on initialization.</param>
|
||||||
public TextLog(string filename, LogLevel logLevel, bool clear)
|
public TextLog(string filename, LogLevel logLevel, bool clear)
|
||||||
{
|
{
|
||||||
fileName = filename;
|
FileName = filename;
|
||||||
_logLevel = logLevel;
|
_logLevel = logLevel;
|
||||||
_logWriter = new StreamWriter(filename, !clear);
|
_logWriter = new StreamWriter(filename, !clear);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue