public LogEntry(LogEntryType type, string text) { LogEntryType = type; TimeStamp = DateTime.Now; EntryText = text; FullText = "[ " + TimeStamp.ToString() + " ] [ " + LogEntryType.ToString() + " ] [ " + EntryText + " ]"; }
private static int LogMessageToTextFile(LogEntryType type, string message) { try { string logmsg = "----------------------------\r\n" + //separator for new log "Local time: " + DateTime.Now.ToString() + "\r\n" + //local time //summary info "Application Name: " + applicationName + "\r\n" + "Log Type: " + type.ToString() + "\r\n" + "Error Message:\r\n" + message + "\r\n"; //allow error log to be encoded by local time string logFileName = string.Format(errorLogFileName, DateTime.Now); using (StreamWriter w = File.AppendText(logFileName)) { w.Write(logmsg); } return 0; //logged to local file } catch (Exception e) { //ignore errors, avoid infinite loop of trying to log errors #if DEBUG System.Console.WriteLine(e.ToString()); System.Diagnostics.Debug.WriteLine(e.ToString()); #endif return -1; } }
public void Custom(LogEntryType type, string message, Exception exception = null) { lock (writeLock) { writer.WriteLine("{0}: {1}", type.ToString().ToUpper(), message); WriteException(exception); } }
/// <summary> /// Writes a log entry by invoking all registered log processors and letting them write /// the log entry. /// Processors can be registered by adding the assembly name of the respective processor class. /// </summary> /// <param name="pType"> /// The type. /// </param> /// <param name="pMessage"> /// The message. /// </param> /// <param name="pException"> /// The p Exception. /// </param> private static void WriteEntry(LogEntryType pType, string pMessage, Exception pException = null) { try { var logEntry = new LogEntry { Caller = GetCaller(), LineNumber = GetCallerLine(), Message = pMessage, Type = pType, Exception = pException }; LogLevel logLevel; Enum.TryParse(pType.ToString(), true, out logLevel); // Now, invoke all processors in the pipeline to let them log the entry their way // Invoke the method for each processor foreach (var processor in Processors) { if ((int)LogConfig.GetLogLevel(processor) >= (int)logLevel) { var calledType = Type.GetType(processor, false, false); if (calledType == null) { return; } // Looks for a method called "WriteLogEntry" MethodInfo method = calledType.GetMethod(WRITE_LOG_ENTRY); if (method != null) { try { object processorInstance = Activator.CreateInstance(calledType, null); method.Invoke(processorInstance, new object[] { logEntry }); } catch { // Do nothing on error. Don't try to log something here, you might create a loop! } } } } } catch { // Do nothing on error. Don't try to log something here, you might create a loop! } }
public void RecordEntry(LogEntryType type, string key, string value1, string value2, string value3, string value4) { try { string line = string.Format("{0},{1},{2}", type.ToString(), (DateTime.Now - FirstRecordableTime).Seconds, key); int commas = 0; if (value1 != null) { commas = 1; } if (value2 != null) { commas = 2; } if (value3 != null) { commas = 3; } if (value4 != null) { commas = 4; } if (commas >= 1) { line += "," + String(value1); } if (commas >= 2) { line += "," + String(value2); } if (commas >= 3) { line += "," + String(value3); } if (commas >= 4) { line += "," + String(value4); } EntryBuilder.AppendLine(line); } catch (Exception ex) { EntryBuilder.AppendLine("ERROR Writing Log Entry: " + ex.Message); } }
/// <summary> /// Writes a new entry to the log. /// </summary> /// <param name="type">Specifies the <see cref="LogEntryType"/>.</param> /// <param name="message">The message to write.</param> public void WriteLine(LogEntryType type, string message) { StringBuilder lineBuilder = new StringBuilder(); lineBuilder.Append(string.Format("[{0}] ", type.ToString())); lineBuilder.Append(string.Format("[{0}] ", DateTime.Now.ToString())); lineBuilder.Append(message); // add line to in memory log _Log.Append(lineBuilder.ToString()); _Log.Append(Environment.NewLine); // write line to all output channels foreach (TextWriter writer in _OutputChannels) { writer.WriteLine(lineBuilder.ToString()); } }
/* Handler designed to use the appropriate tracing tool depending upon the intent of the end-user */ private static void WriteToTraceLevel(string message, LogEntryType entryType) { switch(entryType) { case LogEntryType.Error: //Designed to fall-through to the failure case case LogEntryType.Failure: Trace.TraceError(message); break; case LogEntryType.Information: Trace.TraceInformation(message); break; case LogEntryType.Warning: Trace.TraceWarning(message); break; default: Trace.WriteLine(message, entryType.ToString()); break; } }
public void Log(LogEntryType prefix, string contents, bool isOnlyConsole = false) { string _date; if (DateTime.Now.Hour.ToString().Length < 2) { _date = DateTime.Now.ToShortDateString() + " 0" + DateTime.Now.ToShortTimeString(); } else { _date = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); } string logLineCtor = _date + " [" + prefix.ToString().ToUpper() + "]" + " " + contents + "\r\n"; #if DEBUG if (!isOnlyConsole) { File.AppendAllText(_file, logLineCtor); } Console.Write(logLineCtor); LogEntry entry = new LogEntry { Contents = contents, EntryDateTime = _date, Type = prefix }; LogEntries.Add(entry); #else if (prefix != LogEntryType.Debug) { if (!isOnlyConsole) { File.AppendAllText(_file, logLineCtor); } Console.Write(logLineCtor); LogEntry entry = new LogEntry { Contents = contents, EntryDateTime = _date, Type = prefix }; LogEntries.Add(entry); } #endif }
/* Handler designed to use the appropriate tracing tool depending upon the intent of the end-user */ private static void WriteToTraceLevel(string message, LogEntryType entryType) { switch (entryType) { case LogEntryType.Error: //Designed to fall-through to the failure case case LogEntryType.Failure: Trace.TraceError(message); break; case LogEntryType.Information: Trace.TraceInformation(message); break; case LogEntryType.Warning: Trace.TraceWarning(message); break; default: Trace.WriteLine(message, entryType.ToString()); break; } }
public override void LogMessage(LogEntryType entryType, string description) { if (LoggingEnabled) { if (IsAdministrator() && (System.Environment.OSVersion.Platform == PlatformID.Win32NT)) { try { EventLog.WriteEntry(ServerSourceName, String.Format("Server: {0}\r\n{1}", Name, description), LogEntryTypeToEventLogEntryType(entryType)); } catch { // ignore an error writing to the event log (it's probably complaining that it's full) } } if (_log != null) { lock (_log) { _log.Write ( String.Format ( "{0} {1}{2}\r\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff"), (entryType == LogEntryType.Information) ? String.Empty : String.Format("{0}: ", entryType.ToString()), description ) ); } } } }
/// <summary> /// Writes new entry in log-file /// </summary> /// <param name="prefix">Line prefix</param> /// <param name="contents">Line contents</param> /// <param name="isOnlyConsole">If <code>true</code> log line will be only in console, else writes at file also</param> public void Log(LogEntryType prefix, string contents, bool isOnlyConsole = false) { string _date; if (DateTime.Now.Hour.ToString().Length < 2) { _date = DateTime.Now.ToShortDateString() + " 0" + DateTime.Now.ToLongTimeString(); } else { _date = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString(); } string logLineCtor = _date + " [" + prefix.ToString().ToUpper() + "]" + " " + contents + "\r\n"; #if DEBUG if (!isOnlyConsole) { File.AppendAllText(_file, logLineCtor); } switch (prefix) { default: case LogEntryType.Info: case LogEntryType.Done: case LogEntryType.Success: case LogEntryType.Connection: Console.ForegroundColor = ConsoleColor.Gray; break; case LogEntryType.Error: case LogEntryType.Critical: case LogEntryType.Crash: Console.ForegroundColor = ConsoleColor.Red; break; case LogEntryType.Warning: case LogEntryType.Unknown: case LogEntryType.Stacktrace: case LogEntryType.Debug: Console.ForegroundColor = ConsoleColor.Yellow; break; } Console.Write(logLineCtor); LogEntry entry = new LogEntry { Contents = contents, EntryDateTime = _date, Type = prefix }; LogEntries.Add(entry); #else if (prefix != LogEntryType.Debug) { if (!isOnlyConsole) { File.AppendAllText(_file, logLineCtor); } Console.Write(logLineCtor); LogEntry entry = new LogEntry { Contents = contents, EntryDateTime = _date, Type = prefix }; LogEntries.Add(entry); } #endif Console.ResetColor(); }
/// <summary> /// Logs exception in log file with specified prefix. If <paramref name="consoleOnly"/> is false, puts line in file /// </summary> /// <param name="prefix">Log lines prefix</param> /// <param name="ex">Exception to be logged</param> /// <param name="consoleOnly">If <paramref name="consoleOnly"/> is false, puts line in file</param> public void LogException(LogEntryType prefix, Exception ex, bool consoleOnly) { LogException(prefix.ToString().ToUpper(), ex, consoleOnly); }
/// <summary> /// Logs exception in log file with specified prefix. /// </summary> /// <param name="prefix">Log lines prefix</param> /// <param name="additionalPrefix">Additional log line prefix. Will be put in [PREFIX/ADDITIONAL_PREFIX]</param> /// <param name="ex">Exception to be logged</param> public void LogException(LogEntryType prefix, string additionalPrefix, Exception ex) { LogException(prefix.ToString().ToUpper() + "/" + additionalPrefix.ToUpper(), ex, false); }
public void ShowAndAddEntry(string message, LogEntryType type) { this.AddEntry(message, type); MessageBox.Show(message, type.ToString(), MessageBoxButtons.OK, IconForLogLevel(type)); }
public void LogEntry(LogEntryType log, int level, string str) { if (IsDebugging() || Global.developer >= level) { Debug.Log(string.Format("<color=white>{0}</color>[<color={3}>{1}</color>] {2}", log.ToString().PadRight(10), ToString(), str, GetLogColor()), base.gameObject); } }
/// <summary> /// Logs exception in log file with specified prefix. /// </summary> /// <param name="prefix">Log lines prefix</param> /// <param name="ex">Exception to be logged</param> public void LogException(LogEntryType prefix, Exception ex) { LogException(prefix.ToString().ToUpper(), ex, false); }
/// <summary> /// Logs line with specified prefix, contents. If <paramref name="consoleOnly"/> is false, puts line in file /// </summary> /// <param name="prefix">Log line prefix from defined list of prefixes</param> /// <param name="contents">Log line contents</param> /// <param name="consoleOnly">If <paramref name="consoleOnly"/> is false, puts line in file</param> public void Log(LogEntryType prefix, string contents, bool consoleOnly) { Log(prefix.ToString().ToUpper(), contents, consoleOnly); }
public static void Log(LogEntryType errorType, SourceType sourceType, string message) { if (_connectionString != String.Empty) { using (DBManager manager = new DBManager(_provider, _connectionString)) { string query = ""; manager.Open(); if (_provider == DataProvider.SqlServer) { query = "INSERT INTO [Logs](LogDate, Source, Message, LogEntryType) VALUES(CONVERT(datetime,'" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "',103), '" + sourceType.ToString() + "', '" + message + "','" + errorType.ToString() + "')"; } else if (_provider == DataProvider.Sqlite) { query = "INSERT INTO [Logs](LogDate, Source, Message, LogEntryType) VALUES(current_timestamp, '" + sourceType.ToString() + "', '" + message + "','" + errorType.ToString() + "')"; } manager.ExecuteNonQuery(CommandType.Text, query); } } }
public override void Log(LogEntryType type, string message, params object[] args) { var msg = string.Format(message, args); _logFile.Append(string.Format("{0}: {1}: {2}\n", type.ToString().ToUpper(), DateTime.Now, msg)); }