/// <summary> /// Initialize the logging class and set the Log Level property. /// </summary> /// <param name="filename">Filename for the log.</param> /// <param name="_logLevel">Log Level, value between 0 and 10.</param> public Logging(string filename, iLogLevel _logLevel) { this.logName = filename; this.LOG_LEVEL = _logLevel; }
/// <summary> /// Log a message to specifieced log file at the specified log level. /// </summary> /// <param name="logLevel">Log level to log this message at.</param> /// <param name="data">Message to be logged.</param> /// <param name="pre_fix">Optional string to prefix the log line with.</param> /// <param name="overwrite">Optional. If true the log file will be overwritten with the new data.</param> public void Log(iLogLevel logLevel, string data, string pre_fix = null, bool overwrite = false) { lock (_custom) { FileInfo fi = new FileInfo(this.logRoot + this.logName); StreamWriter sw; try { if (!Directory.Exists(this.logRoot)) Directory.CreateDirectory(this.logRoot); if (overwrite) { fi.Delete(); sw = fi.CreateText(); } else { if (fi.Exists && !overwrite) { if (fi.Length > Log_Size_Limit) { fi.CopyTo(string.Format("{0}{1}_{2}{3}", new object[] { this.logRoot, this.logName, DateTime.Now.ToFileTime().ToString(), this.oldLogExtention }), true); fi.Delete(); this.CheckNumberofOldLogs(); } } sw = fi.AppendText(); } if (!fi.Exists) { sw.WriteLine(this.FormatLogLine(0, "File Created", null)); } if (logLevel >= this.LOG_LEVEL) { sw.WriteLine(this.FormatLogLine(logLevel, data, pre_fix)); } sw.Close(); sw.Dispose(); } catch (UnauthorizedAccessException e) { MessageBox.Show("UnauthorizedAccessException caught in qqlogs: " + e.Message, "Unauthorized Access Exception", MessageBoxButtons.OK); } catch (IOException e) { MessageBox.Show("IOException caught in qqlogs: " + e.Message, "Generic IO Exception", MessageBoxButtons.OK); } catch (Exception e) { MessageBox.Show("Generic Exception caught in qqlogs: " + e.Message, "Generic Exception", MessageBoxButtons.OK); } } }
private string FormatLogLine(iLogLevel _logLevel, string _Message, string preFix) { string logLine = this.logLineFormat; if (preFix != null && preFix != string.Empty) { logLine = string.Format("{0}{1}", preFix, logLine); } logLine = logLine.Replace("%DateTime%", DateTime.Now.ToString()); logLine = logLine.Replace("%szLogLevel%", szLogLevel[(int)_logLevel]); logLine = logLine.Replace("%LogLevel%", _logLevel.ToString()); logLine = logLine.Replace("%Message%", _Message); return logLine; }
/// <summary> /// Initialize the logging class and set LogLevel, Log Size Limit, and Number of Old Logs to Keep. /// </summary> /// <param name="filename">Filename of the log.</param> /// <param name="_logLevel">iLogLevel value.</param> /// <param name="_logSizeLimit">Log Size Limit in Bytes.</param> /// <param name="_numberOfOldLogsToKeep">How many old logs would you like to keep around.</param> /// <exception cref="System.ArgumentOutOfRangeException"></exception> public Logging(string filename, iLogLevel _logLevel, long _logSizeLimit, uint _numberOfOldLogsToKeep) { this.logName = filename; this.LOG_LEVEL = _logLevel; if (_logSizeLimit >= 0) { this.Log_Size_Limit = _logSizeLimit; } else { throw new ArgumentOutOfRangeException("_logSizeLimit", "Log Size Limit must be a non-negative value."); } this.numberOfOldLogsToKeep = _numberOfOldLogsToKeep; }