public void LogMessage(string message, string user = "******", LogMessageType logMessageType = LogMessageType.Trace, LogLevel logLevel = LogLevel.Info) { string connectionString = LogSettings.GetValue("LogDBConnectionString"); try { using (NpgsqlConnection conn = new NpgsqlConnection(connectionString)) { conn.Open(); using (NpgsqlCommand cmd = new NpgsqlCommand()) { cmd.Connection = conn; cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "INSERT INTO public.\"Log\"(message, username, type, level, severity, datetime) VALUES (:message,:username,:type,:level,:severity,:datetimeParamName)"; cmd.Parameters.AddWithValue(":datetimeParamName", DateTime.Now); cmd.Parameters.AddWithValue(":message", message); cmd.Parameters.AddWithValue(":type", logMessageType.ToString()); cmd.Parameters.AddWithValue(":level", logLevel.ToString()); cmd.Parameters.AddWithValue(":severity", 0); cmd.Parameters.AddWithValue(":username", user); cmd.ExecuteNonQuery(); } } } catch (Exception ex) { //TODO } }
internal static string BuildExceptionMessage(Exception ex, LogMessageType logMessageType) { string exMessage = string.Format("Date: {0}, [Exception]\n {1}\n Message: {2}\n Stack: {3}", DateTime.Now.ToString(), logMessageType.ToString(), ex.Message, ex.StackTrace); // Include the Data collection exMessage += "\n Data:"; foreach (var item in ex.Data.Keys) { exMessage += string.Format(" key:{0}, value:{1};", item, ex.Data[item]); } // Are there any inner exceptions? while (ex.InnerException != null) { exMessage += BuildInnerExceptionMessage(ex.InnerException); ex = ex.InnerException; } return(exMessage); }
/// <summary> /// Write a message to the log. /// </summary> /// <param name="messageType"> /// The type of the message. /// </param> /// <param name="message"> /// The message. /// </param> protected override void TemplateWrite(LogMessageType messageType, string message) { try { StringBuilder logEntry = new StringBuilder(); logEntry.Append(DateTime.Now.ToString()); logEntry.Append(" "); logEntry.Append(messageType.ToString()); logEntry.Append(": "); logEntry.Append(message); lock (this) { using (StreamWriter writer = new StreamWriter(FileName, true)) { writer.WriteLine(logEntry.ToString()); writer.Flush(); } } } catch (Exception ex) { throw new Exception("failed to write message to log file (FileName=" + FileName + ", messageType=" + messageType.ToString() + ", message=" + message + ")", ex); } }
public void Log(LogMessageType messageType, string message) { using (var streamWriter = new StreamWriter(_filePath, true)) { streamWriter.WriteLine("[" + messageType.ToString() + "]: " + message); } }
// Help methods that use our FileSystem abstraction public override void AppendLog(String message, LogMessageType messageType) { string textEntry = string.Format("{0:MM-dd-yyyy HH:mm:ss:ffff} :{1}: [{2}] {3}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, messageType.ToString(), message); switch (messageType) { case LogMessageType.Info: Android.Util.Log.Info(TAG, message); break; case LogMessageType.Warn: Android.Util.Log.Debug(TAG, message); break; case LogMessageType.Debug: Android.Util.Log.Debug(TAG, message); break; case LogMessageType.Error: Android.Util.Log.Error(TAG, message); break; case LogMessageType.Fatal: Android.Util.Log.Wtf(TAG, message); break; case LogMessageType.Metric: Android.Util.Log.Debug( TAG_METRIC, message ); break; default: break; } AppendText(LogFileName, textEntry); }
public override string ToString() { string result = ""; result += _time.Hour.ToString() + ":" + _time.Minute.ToString() + ":" + _time.Second.ToString(); result += "\t"; result += _messageType.ToString(); result += "\t"; result += _sourceFile + ":" + _lineNumber.ToString(); result += " ("; result += _callingFunction; result += ")"; result += "\t\t"; result += _message; return(result); }
public override string ToString() { StringBuilder result = new StringBuilder(); result.AppendLine(string.Format("Date Time : {0}", _date.ToString())); result.AppendLine(string.Format("{0} : {1}", _logMessageType.ToString(), _message)); return(result.ToString()); }
public void NewLogMessageHandler(string message, LogMessageType type, DateTime timestamp) { string timestampStr = timestamp.ToString("yyyy.MM.dd. HH:mm:ss"); string typeStr = type.ToString().ToUpper(); string formattedMessage = string.Format("[{0}] [{1}] {2}", timestampStr, typeStr, message); writeToFile(formattedMessage); }
// Help methods that use our FileSystem abstraction public override void AppendLog(String message, LogMessageType messageType) { string textEntry = string.Format("{0:MM-dd-yyyy HH:mm:ss:ffff} :{1}: [{2}] {3}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, messageType.ToString(), message); Console.WriteLine(textEntry); AppendText(LogFileName, textEntry); }
public void NewLogMessageHandler(string message, LogMessageType type, DateTime timestamp) { string timestampStr = timestamp.ToString("yyyy.MM.dd. HH:mm:ss"); string typeStr = type.ToString().ToUpper(); string colorStr = colorToHex(getColorByType(type)); string formattedMessage = string.Format(MESSAGE_TEMPLATE, timestampStr, typeStr, message, colorStr); writeToFile(formattedMessage); }
public void Log(LogMessageType logMessageType, string message) { if (string.IsNullOrWhiteSpace(message)) { throw new ArgumentNullException("Message can not be empty!"); } Console.WriteLine(logMessageType.ToString() + ": " + message); }
/// <summary> /// Writes a message to the console /// </summary> /// <param name="message">Message to write</param> /// <param name="type">Type of message to write</param> protected override void WriteMessage(string message, LogMessageType type) { ConsoleLoggerSettings consoleSettings = Settings as ConsoleLoggerSettings; string typePrefix = string.Format("[{0}]: ", type.ToString()[0]); string timestamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt: "); bool showTimestamp = false; ConsoleColor foreground = Console.ForegroundColor; ConsoleColor background = Console.BackgroundColor; switch (type) { case LogMessageType.Information: foreground = consoleSettings.InformationForegroundColor; background = consoleSettings.InformationBackgroundColor; showTimestamp = consoleSettings.TimeStampOnInformation; break; case LogMessageType.Warning: foreground = consoleSettings.WarningForegroundColor; background = consoleSettings.WarningBackgroundColor; showTimestamp = consoleSettings.TimeStampOnWarning; break; case LogMessageType.Error: foreground = consoleSettings.ErrorForegroundColor; background = consoleSettings.ErrorBackgroundColor; showTimestamp = consoleSettings.TimeStampOnError; break; default: foreground = consoleSettings.DefaultForegroundColor; background = consoleSettings.DefaultBackgroundColor; showTimestamp = consoleSettings.TimeStampOnNone; typePrefix = string.Empty; break; } StringBuilder sb = new StringBuilder(); if (showTimestamp) { sb.Append(timestamp); } if (consoleSettings.ShowMessageTypePrefixes && type != LogMessageType.None) { sb.Append(typePrefix); } sb.Append(message); WriteMessage(sb.ToString(), foreground, background); }
private void Log(LogMessageType messageType, object[] methodParameters) { var methodName = messageType == LogMessageType.Trace ? "Debug" : messageType.ToString(); var isEnabledInfo = iLogType.GetProperty($"Is{methodName}Enabled", typeof(bool)); if (isEnabledInfo != null && (bool)isEnabledInfo.GetValue(iLog)) { iLogType.GetMethod(methodName, methodParameters.Select(x => x.GetType()).ToArray()) ?.Invoke(iLog, methodParameters); } }
/// <summary> /// Writes a message to the text file /// </summary> /// <param name="message">Message to write</param> /// <param name="type">Type of message to write</param> protected override void WriteMessage(string message, LogMessageType type) { TextFileLoggerSettings textFileSettings = Settings as TextFileLoggerSettings; string typePrefix = string.Format("[{0}]: ", type.ToString()[0]); string timestamp = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt: "); bool showTimestamp = false; switch (type) { case LogMessageType.Information: showTimestamp = textFileSettings.TimeStampOnInformation; break; case LogMessageType.Warning: showTimestamp = textFileSettings.TimeStampOnWarning; break; case LogMessageType.Error: showTimestamp = textFileSettings.TimeStampOnError; break; default: showTimestamp = textFileSettings.TimeStampOnNone; typePrefix = string.Empty; break; } StringBuilder sb = new StringBuilder(); if (showTimestamp) { sb.Append(timestamp); } if (textFileSettings.ShowMessageTypePrefixes && type != LogMessageType.None) { sb.Append(typePrefix); } sb.Append(message); try { if (!File.Exists(textFileSettings.FilePath)) { File.Create(textFileSettings.FilePath).Close(); } File.AppendAllLines(textFileSettings.FilePath, new string[] { sb.ToString() }); } catch { } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { LogMessageType logMessageType = (LogMessageType)value; Uri iconUri; switch (logMessageType) { case LogMessageType.Error: iconUri = new Uri("/LogViewer;component/Icons/icons8-cancel-48.png", UriKind.Relative); break; case LogMessageType.Warning: iconUri = new Uri("/LogViewer;component/Icons/icons8-error-48.png", UriKind.Relative); break; case LogMessageType.Information: iconUri = new Uri("/LogViewer;component/Icons/icons8-info-50.png", UriKind.Relative); break; case LogMessageType.Debug: iconUri = new Uri("/LogViewer;component/Icons/icons8-bug-50.png", UriKind.Relative); break; case LogMessageType.Unknown: iconUri = new Uri("/LogViewer;component/Icons/icons8-help-50.png", UriKind.Relative); break; default: return(new TextBlock() { Text = logMessageType.ToString(), HorizontalAlignment = HorizontalAlignment.Center }); } if (iconUri != null) { var icon = new Image() { Width = 30, Height = 30, HorizontalAlignment = HorizontalAlignment.Center }; var bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = iconUri; bitmap.EndInit(); icon.Stretch = Stretch.Fill; icon.StretchDirection = StretchDirection.Both; icon.Source = bitmap; return(icon); } return(null); }
public void Insert(Type type, string methodName, string message, string stackTrace, LogMessageType logType) { var entity = new OtherLogInfo { CreateDate = DateTime.Now, ClassName = type.Name, MethodName = methodName, Message = message, StackTrace = stackTrace, LogType = logType.ToString() }; Add(entity); }
/// <summary> /// Initializes a new instance of the <see cref="LogMessage" /> class. /// </summary> /// <param name="module">The module.</param> /// <param name="type">The type.</param> /// <param name="text">The text.</param> /// <param name="exception">The exception.</param> /// <param name="callerInfo">The caller info.</param> public LogMessage(string module, LogMessageType type, string text, Exception exception, CallerInfo callerInfo) { if (exception != null) { // if file logging is enabled, write it here ErrorFileLogger.WriteExceptionToFile(exception, type.ToString() + ", " + text + ", " + module + ", File: " + (callerInfo?.FilePath ?? "none") + ", Line: " + (callerInfo?.LineNumber.ToString() ?? "none")); } Module = module; Type = type; Text = text; Exception = exception; CallerInfo = callerInfo; }
/// <summary> /// Write a message to the log. /// </summary> /// <param name="messageType"> /// The type of the message. /// </param> /// <param name="message"> /// The message. /// </param> protected override void TemplateWrite(LogMessageType messageType, string message) { StringBuilder logEntry = new StringBuilder(); logEntry.Append(DateTime.Now.ToString()); logEntry.Append(" "); logEntry.Append(messageType.ToString()); logEntry.Append(": "); logEntry.Append(message); lock (this) { Console.WriteLine(logEntry.ToString()); } }
public void Insert(Type type, string methodName, string message, string clientName, string uniqueName, string tableName, LogMessageType logType = LogMessageType.Warning) { var entity = new OtherLogInfo { CreateDate = DateTime.Now, ClassName = type.Name, MethodName = methodName, Message = message, ClientName = clientName, UniqueName = uniqueName, TableName = tableName, LogType = logType.ToString() }; Add(entity); }
public void Log(string message, LogMessageType type = LogMessageType.Log, object context = null) { switch (type) { case LogMessageType.Log: case LogMessageType.Warning: case LogMessageType.Error: { MessageBox.Show(message, type.ToString()); break; } default: { throw new ArgumentOutOfRangeException(nameof(type), type, null); } } }
private static void WriteToAllSinks(string message, LogMessageType type) { if (type == LogMessageType.Debug && !DebugEnabled) { return; } var logMessage = $"[{type.ToString().ToUpper()}]: {message}"; SetColorForLogType(type); Console.WriteLine(logMessage); Console.ResetColor(); if (type == LogMessageType.Error) { WriteErrorToTeamCity(message); } WriteToFile(logMessage); }
public override void AppendLog(String message, LogMessageType messageType) { string textEntry = string.Format("{0:MM-dd-yyyy HH:mm:ss:ffff} :{1}: [{2}] {3}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, messageType.ToString(), message); //First write to console Console.WriteLine(textEntry); bool passCheckPoint = false; if (CheckPointError && LogMessageType.Error == messageType) { passCheckPoint = true; } else if (CheckPointWarn && LogMessageType.Warn == messageType) { passCheckPoint = true; } else if (CheckPointInfo && LogMessageType.Info == messageType) { passCheckPoint = true; } else if (CheckPointDebug && LogMessageType.Debug == messageType) { passCheckPoint = true; } else if (CheckPointMetric && LogMessageType.Metric == messageType) { passCheckPoint = true; } // Log a Checkpoint if necessary if (passCheckPoint) { TestFlight.PassCheckpoint(message); } AppendText(textEntry); }
public StructuredLogObject CreateBaseStructuredLogObject(string message, LogMessageType type, string stackTrace = null, string playerId = null, string playerName = null) { StructuredLogObject structuredLogObject = new StructuredLogObject(); structuredLogObject.Value = message; structuredLogObject.LogMessageType = type.ToString(); structuredLogObject.ClientVersion = ClientInfo.Instance.ClientVersion; structuredLogObject.DeviceModel = SystemInfo.deviceModel; structuredLogObject.OperatingSystem = SystemInfo.operatingSystem; structuredLogObject.SystemMemorySize = Convert.ToString(SystemInfo.systemMemorySize); if (!string.IsNullOrEmpty(stackTrace)) { structuredLogObject.StackTrace = stackTrace; } playerName = playerName ?? "<not set>"; playerId = playerId ?? "<not set>"; structuredLogObject.PlayerName = playerName; structuredLogObject.PlayerId = playerId; return(structuredLogObject); }
/// <summary> /// LogMessage(string msg) /// Timestamps a message with the current time and inserts it at the head of the message log /// Can be called from any thread /// </summary> /// <param name="msg">Message string to log</param> /// <param name="type">see LogMessageType [INFO]</param> /// <param name="isLogToFile">true=>write to log file [true]</param> /// <param name="level">see LogMessageLevel [INFO]</param> static public void LogMessage(string msg, LogMessageType type = LogMessageType.MISC, bool isLogToFile = true, LogMessageLevel level = LogMessageLevel.INFO) { var newLogEntry = new LogMessage(msg, type); if (messages == null) { throw new NullReferenceException(); } if (isLogToFile == true) { string logMsgWithType = String.Format("[{0}]-{1}", type.ToString(), msg); switch (level) { case LogMessageLevel.DEBUG: Loggers.log.Debug(logMsgWithType); break; case LogMessageLevel.ERROR: Loggers.log.Error(logMsgWithType); break; case LogMessageLevel.WARN: Loggers.log.Warn(logMsgWithType); break; default: Loggers.log.Info(logMsgWithType); break; } } // Insert message at the head of the log so it shows up at the top. // messages.Insert(0, newLogEntry); // Can be called from any thread UIThread.Update(() => { messages.Insert(0, newLogEntry); }); }
/// <summary> /// Write a message to the log. /// </summary> /// <param name="messageType"> /// The type of the message. /// </param> /// <param name="message"> /// The message. /// </param> /// <remarks> /// The TemplateWrite method which is implement in the derived class /// is called (Template Method design pattern). /// </remarks> public void Write(LogMessageType messageType, string message) { try { if (Enabled == true) { LogMessageType safeMessageType = LogMessageType.Error; //if (messageType != null) { safeMessageType = messageType; } string safeMessage = string.Empty; if (message != null) { safeMessage = message; } TemplateWrite(safeMessageType, safeMessage); } } catch //(Exception ex) { string safeMessage = string.Empty; if (message != null) { safeMessage = message; } string safeMessageTypeString = string.Empty; //if (messageType != null) { safeMessageTypeString = messageType.ToString(); } //throw new Exception("failed to write to log (Enabled=" + Enabled + ", messageType=" + safeMessageTypeString + ", message=" + safeMessage + ")", ex); } }
internal static string BuildExceptionMessage(Exception ex, LogMessageType logMessageType ) { string exMessage = string.Format("Date: {0}, [Exception]\n {1}\n Message: {2}\n Stack: {3}", DateTime.Now.ToString(), logMessageType.ToString(), ex.Message, ex.StackTrace); // Include the Data collection exMessage += "\n Data:"; foreach (var item in ex.Data.Keys) exMessage += string.Format(" key:{0}, value:{1};", item, ex.Data[item]); // Are there any inner exceptions? while (ex.InnerException != null) { exMessage += BuildInnerExceptionMessage(ex.InnerException); ex = ex.InnerException; } return exMessage; }
// Help methods that use our FileSystem abstraction public override void AppendLog(String message, LogMessageType messageType) { string textEntry = string.Format("{0:MM-dd-yyyy HH:mm:ss:ffff} :{1}: [{2}] {3}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, messageType.ToString(), message); switch (messageType) { case LogMessageType.Info: Android.Util.Log.Info(TAG, message); break; case LogMessageType.Warn: Android.Util.Log.Debug(TAG, message); break; case LogMessageType.Debug: Android.Util.Log.Debug(TAG, message); break; case LogMessageType.Error: Android.Util.Log.Error(TAG, message); break; case LogMessageType.Fatal: Android.Util.Log.Wtf(TAG, message); break; case LogMessageType.Metric: Android.Util.Log.Debug(TAG_METRIC, message); break; default: break; } AppendText(LogFileName, textEntry); }
public void LogMessage(string message, LogMessageType type = LogMessageType.Info) { Console.WriteLine("[{0}] {1}", type.ToString().ToUpper(), message); }
public bool Log(string userID, string userName, string messageString, LogMessageType messageType) { string msgType = messageType.ToString(); string body = "User ID: " + userID + "\n"; body += "User Name: " + userName + "\n"; body += "Message Type: " + msgType + "\n"; body += "Message: " + messageString + "\n"; emailSender.SendMail("*****@*****.**", "Log (" + msgType + ") North Carolina Tax Recovery Calculator", body); return true; }
/// <summary> /// Writes message line to text log file. Type of message is also specified. /// </summary> /// <param name="message"></param> /// <param name="logMessageType"></param> public void WriteLine(string message, LogMessageType logMessageType) { string messageDateAndType = ""; string formattedMessage = ""; string messagePrefix = ""; string machineName = ""; try { if (this._showDatetime) { messageDateAndType = DateTime.Now.ToString(this._dateTimeFormat); } else { messageDateAndType = ""; } if (this._showMessageType) { if (messageDateAndType.Length > 0) { messageDateAndType += " "; } messageDateAndType += logMessageType.ToString().ToUpper() + ": "; } if (this.ShowErrorWarningTypes == true && this.ShowMessageType == false) { if (logMessageType == LogMessageType.Warning || logMessageType == LogMessageType.Error) { if (messageDateAndType.Length > 0) { messageDateAndType += " "; } messageDateAndType += logMessageType.ToString().ToUpper() + ": "; } } if (this._machineName.Length > 0) { if (this._applicationName.Length > 0) { machineName = " on " + this._machineName; } else { machineName = this._machineName; } } else { machineName = ""; } if ((this._applicationName.Length > 0) | (machineName.Length > 0)) { messagePrefix = (messageDateAndType + " <" + (this._applicationName + machineName).Trim() + "> ").Trim(); } else { messagePrefix = messageDateAndType.Trim(); } if (messagePrefix.Length > 0) { formattedMessage = messagePrefix + " " + message; } else { formattedMessage = message; } StreamWriter writer = File.AppendText(this._fileName); writer.WriteLine(formattedMessage); writer.Flush(); writer.Close(); } catch (Exception ex) { throw ex; } }
public override void Log(LogMessageType type, bool logToConsole, string message) { if (logToConsole || _alwaysPrintToConsole) Debug.Log(string.Format("[{0}]: {1}", type.ToString(), message)); }
/// <summary> /// Write message ty exception and message type /// </summary> /// <param name="e">exception</param> /// <param name="messageType">message type</param> /// <param name="classType">classType </param> /// <param name="methodName"> method Name</param> public void Write(Exception e, LogMessageType messageType, Type classType, string methodName) { Write(new LogMessage(e.Message + e.StackTrace, messageType.ToString(), classType, methodName)); }
internal static void Log(LogMessageType logMessage, string content) { Log(logMessage.ToString() + "\t" + content); }
// Help methods that use our FileSystem abstraction public virtual void AppendLog(String message, LogMessageType messageType) { string textEntry = string.Format("{0:MM-dd-yyyy HH:mm:ss:ffff} :{1}: [{2}] {3}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, messageType.ToString(), message); AppendText(LogFileName, textEntry); }
// Help methods that use our FileSystem abstraction public void AppendLog( String message, LogMessageType messageType ) { string textEntry = string.Format( "{0:MM-dd-yyyy HH:mm:ss:ffff} :{1}: [{2}] {3}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, messageType.ToString(), message ); Console.WriteLine( textEntry ); string fileName = LogPath + LogHelper.GetFileNameYYYMMDD( _fileType, _fileExt ); FileHelper.AppendText( fileName, textEntry ); }
/// <summary> /// The function called when a message is logged /// </summary> /// <param name="message">The message logged</param> /// <param name="type">The type of message logged</param> /// <param name="exception">An exception, may be null</param> public virtual void WriteMessage(string message, LogMessageType type, Exception exception) { m_stream.WriteLine(type.ToString() + ": " + message); if (exception != null) { m_stream.WriteLine(exception.ToString()); m_stream.WriteLine(); } if (EventLogged != null) EventLogged(message, type, exception); }
/// <summary> /// Write message by message content and type /// </summary> /// <param name="text">log message</param> /// <param name="messageType">message type</param> /// <param name="classType">class Type </param> /// <param name="methodName">method Name </param> public void Write(string text, LogMessageType messageType, Type classType, string methodName) { Write(new LogMessage(text, messageType.ToString(), classType, methodName)); }
// Help methods that use our FileSystem abstraction public void AppendLog(String message, LogMessageType messageType) { string textEntry = string.Format("{0:MM-dd-yyyy HH:mm:ss:ffff} :{1}: [{2}] {3}", DateTime.Now, System.Threading.Thread.CurrentThread.ManagedThreadId, messageType.ToString(), message); Console.WriteLine(textEntry); string fileName = LogPath + LogHelper.GetFileNameYYYMMDD(_fileType, _fileExt); FileHelper.AppendText(fileName, textEntry); }
private static string FormatMessage(LogMessageType type, string message) { return(string.Format("{0,-8}{1,-36}{2}", type.ToString() + ":", DateTime.Now.ToString("o"), message)); }