/// <summary> /// log using warn log level /// </summary> /// <param name="ex"> the exception to be logged</param> /// <param name="logParaemtersArr">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 LogWarn(string messageFormat, object[] messageParameters = null , Exception ex = null, object[] logObjectsArr = null , [CallerMemberName] string callerInfo = "") { string finalMessage = CreateLogMessage(logObjectsArr, messageFormat, callerInfo, messageParameters); CurrentLogger.Warn(ex, finalMessage); }
public static void Warn(string msg) { CurrentLogger.Warn(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); }
public static void Warn(string msg) { CurrentLogger.Warn(msg); Console.WriteLine(msg); }