private static string GenerateMessageAccordingToType(object logObject, string callerInfo) { string resultLogString = string.Empty; try { //Doing fake bug fix if (logObject is ILoggable) { resultLogString = ((ILoggable)logObject).ToLogString(); } else { resultLogString = logObject.ToString(); } } //these catches indicate an error in the our logger code or in the calling parameters //so these should be rethrown to protect the caller from silent failure catch (Exception ex) { CurrentLogger.Error(ex, string.Format("{0}:Error inside custom logger at GenerateMessageAccordingToType while called from {1}", LOCAL_MODULE_NAME, callerInfo)); throw; } return(resultLogString); }
/// <summary> /// log using Error log level /// </summary> /// <param name="ex"> the exception to be logged</param> /// <param name="logObjectsArr">array of objects to log their values for custom message user must override toSting() method</param> /// <param name="messageFormat">format of the message to be logged</param> /// <param name="messageParameters">the parameters to fill the message placeholders</param> public static void LogError(string messageFormat, object[] messageParameters = null , Exception ex = null, object[] logObjectsArr = null , [CallerMemberName] string callerInfo = "") { string finalMessage = CreateLogMessage(logObjectsArr, messageFormat, callerInfo, messageParameters); if (ex == null) { CurrentLogger.Error(finalMessage); } else { CurrentLogger.Error(ex, finalMessage); } }
/// <summary> /// 记录错误信息 /// </summary> /// <param name="msg"></param> public static void Error(string msg) { CurrentLogger.Error(msg); }
/// <summary> /// builds the message string according to message format and parameters and the logParamwers objects /// </summary> /// <param name="logObjectsArr">array of objects to log their values for custom message user must override toSting() method</param> /// <param name="messageFormat">format of the message to be logged</param> /// <param name="messageParameters"> the parameters to fill the message placeholders</param> /// <returns>returns the complete message to be logged</returns> private static string CreateLogMessage(object[] logObjectsArr, string messageFormat, string callerInfo, object[] messageParameters) { string finalMessage = string.Empty; //Test Shelv #region create formatted message try { if (messageFormat != null) { #region generate parameters string list string[] textParametersList = new string[messageParameters.Length]; if (messageParameters != null && messageParameters.Length > 0) { textParametersList = new string[messageParameters.Length]; for (int i = 0; i < messageParameters.Length; i++) { if (messageParameters[i] != null) { textParametersList[i] = GenerateMessageAccordingToType(messageParameters[i], callerInfo); } else { textParametersList[i] = ""; CurrentLogger.Warn(string.Format("{0}:Null is invalid parameter for the formatted string at parameter index {1} while called from {2}", LOCAL_MODULE_NAME, i, callerInfo)); } } } #endregion finalMessage = string.Format(callerInfo + ":" + messageFormat, textParametersList.ToArray <object>()); } else { #region log warnings if (messageFormat == null) { CurrentLogger.Warn(string.Format("{0}:Message format is null while called from {1}", LOCAL_MODULE_NAME, callerInfo)); } if (messageParameters == null) { CurrentLogger.Warn(string.Format("{0}:Message parameters is null while called from {1}", LOCAL_MODULE_NAME, callerInfo)); } if (messageParameters.Count() == 0) { CurrentLogger.Warn(string.Format("{0}:Message parameters count is 0 while called from {1}", LOCAL_MODULE_NAME, callerInfo)); } #endregion } #region create logs for passed separate array of objects if (logObjectsArr != null) { foreach (object currentParameterObject in logObjectsArr) { if (currentParameterObject != null) { finalMessage += "\n logged parameter " + GenerateMessageAccordingToType(currentParameterObject, callerInfo); } } } #endregion } //these catches indicate an error in the our logger code or in the calling parameters //so these should be rethrown to protect the caller from silent failure catch (FormatException ex) { CurrentLogger.Error(ex, string.Format("{0}:Error because of string.Format while called from {1}", LOCAL_MODULE_NAME, callerInfo)); throw; } catch (Exception ex) { CurrentLogger.Error(ex, string.Format("{0}:Error inside custom logger while called from {1}", LOCAL_MODULE_NAME, callerInfo)); throw; } #endregion return(finalMessage); }
/// <summary> /// 记录错误信息 /// </summary> /// <param name="msg"></param> public static void Error(string msg) { CurrentLogger.Error(msg); Console.WriteLine(msg); }