} // TryGetValue /// <summary> /// /// </summary> /// <param name="i_sKey"></param> /// <param name="o_sIpAddress"></param> /// <param name="o_sSessionId"></param> /// <param name="o_sComponentName"></param> /// <param name="o_sThreadId"></param> /// <param name="o_sVmcId"></param> /// <returns></returns> public bool TryGetValues(string i_sKey, out string o_sIpAddress, out string o_sSessionId, out string o_sComponentName, out string o_sThreadId, out string o_sVmcId) { bool bRet = true; LoggerThreadInfo oInfo = null; o_sIpAddress = o_sSessionId = o_sComponentName = o_sThreadId = o_sVmcId = ""; if ((i_sKey == null) || (i_sKey == "")) // Check to make sure there is a valid value in the key { i_sKey = m_csUnnamed; } bRet = ThreadInfoDictSingleton.GetInstance().TryGetValue(i_sKey, out oInfo); if (bRet) { o_sIpAddress = oInfo.m_sIpAddress; o_sSessionId = oInfo.m_sSessionId; o_sComponentName = oInfo.m_sComponentName; o_sThreadId = oInfo.m_sThreadId; o_sVmcId = oInfo.m_sVmcId; } else { StringBuilder sbKeys = null; sbKeys = new StringBuilder(); sbKeys.AppendFormat("{0} TryGetValues('{1}') not among: ", DateTime.Now.ToString(), i_sKey); foreach (string sTmp in ThreadInfoDictSingleton.GetInstance().m_LoggerThreadInfoDict.Keys) { sbKeys.AppendFormat("{0}, ", sTmp); } Console.WriteLine(sbKeys.ToString()); } return(bRet); }
} // ThreadInfoDictSingleton /// <summary> /// /// </summary> /// <param name="i_sKey"></param> /// <param name="o_Info"></param> /// <returns></returns> public bool TryGetValue(string i_sKey, out LoggerThreadInfo o_Info) { bool bRet = true; lock (m_LoggerThreadInfoDict) { if ((i_sKey == null) || (i_sKey == "")) // Check to make sure there is a valid value in the key { i_sKey = m_csUnnamed; } bRet = m_LoggerThreadInfoDict.TryGetValue(i_sKey, out o_Info); if (!bRet) { StringBuilder sbKeys = null; sbKeys = new StringBuilder(); sbKeys.AppendFormat("{0} TryGetValues('{1}') not among: ", DateTime.Now.ToString(), i_sKey); foreach (string sTmp in ThreadInfoDictSingleton.GetInstance().m_LoggerThreadInfoDict.Keys) { sbKeys.AppendFormat("{0}, ", sTmp); } Console.WriteLine(sbKeys.ToString()); } } return(bRet); } // TryGetValue
} // Log /// <summary> /// /// The format of the columns will be: DateTime, Severity, IpAddress, SessionId, ComponentName, MethodName, VmcId, ThreadId, Msg /// </summary> /// <param name="i_eSeverity"></param> /// <param name="i_iVmcId"></param> /// <param name="i_sComponent"></param> /// <param name="i_sMsg"></param> /// <returns></returns> public override bool Log(eSeverity i_eSeverity, int i_iVmcId, string i_sComponentName, string i_sMsg) { bool bRet = true, bRes = true; string sComponentName = i_sComponentName, sMethodName = "", sVmc = "", sThread = "", sRem = ""; LoggerThreadInfo oInfo = null; StackTrace oST = null; try { SetLastError(0, ""); oInfo = new LoggerThreadInfo(); oST = new StackTrace(2); // Skip two frames to avoid the overhead of getting a full stack trace // First check to see if we need to log this message or not if (i_eSeverity >= m_FilterLevel) { bRes = ThreadInfoDictSingleton.GetInstance().TryGetValue(Thread.CurrentThread.Name, out oInfo); // Don't need to check return value, we'll just use the empty strings assigned in the constructor. if (!bRes) { // This isn't always an error, some threads (like pooled or event-handlers) won't be named. if (oInfo == null) { oInfo = new LoggerThreadInfo(); } } sVmc = oInfo.m_sVmcId; sThread = oInfo.m_sThreadId; bRes = ReplaceIndexesIfEmbedded(i_sMsg, ref sVmc, ref sThread, out sRem); if (sComponentName.Length == 0) { sComponentName = oST.GetFrame(0).GetMethod().ReflectedType.FullName; } else { sComponentName = oInfo.m_sComponentName; } sComponentName = StripExtension(sComponentName); sMethodName = oST.GetFrame(0).GetMethod().ReflectedType.FullName + "." + oST.GetFrame(0).GetMethod().Name; oST = null; m_Logger.Log(ConvertSeverityToNSLevel(i_eSeverity), string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}", i_eSeverity.ToString(), oInfo.m_sIpAddress, oInfo.m_sSessionId, sComponentName, sMethodName, sVmc, sThread, sRem)); } } catch (Exception exc) { bRet = false; SetLastError(-1, "Caught exception in Log(3): " + exc.ToString()); Console.Error.WriteLine(m_sLastError); } return(bRet); } // Log
/// <summary> /// /// </summary> /// <returns></returns> public override bool Open() { bool bRet = true; try { string sComponentName = ""; LoggerThreadInfo oInfo = null; ThreadInfoDictSingleton oDict = ThreadInfoDictSingleton.GetInstance(); bool bRes = oDict.TryGetValue(Thread.CurrentThread.Name, out oInfo); if (bRes) { sComponentName = oInfo.m_sComponentName; } if (sComponentName == "") { StackTrace oST = new StackTrace(); sComponentName = oST.GetFrame(2).GetMethod().Module.Name; } sComponentName = StripExtension(sComponentName); string sLogFileTemplate = ConfigurationManager.AppSettings["LogFileTemplate"]; if (String.IsNullOrEmpty(sLogFileTemplate)) { sLogFileTemplate = "{yyyy}{mm}{dd}"; } string sFullyQualifiedLogFileName = Path.Combine(m_sPath, String.Format("{0}_{1}.log.txt", sComponentName, sLogFileTemplate)); m_LogConsole = NSpring.Logging.Logger.CreateConsoleLogger("{timeStamp} {msg}"); m_LogFile = NSpring.Logging.Logger.CreateFileLogger(sFullyQualifiedLogFileName, "{timeStamp}\t{msg}"); m_LogFile.Encoding = new UTF8Encoding(false); // Don't emit UTF-8 BOM to log file. m_Logger = NSpring.Logging.Logger.CreateCompositeLogger(m_LogConsole, m_LogFile); m_Logger.IsBufferingEnabled = true; m_Logger.BufferSize = 10000; // How much to buffer up before writing out m_Logger.AutoFlushInterval = 2000; // FIX - read from a config file. m_Logger.Open(); } catch (Exception exc) { bRet = false; SetLastError(-1, "Caught exception in TsvAndStdoutLogger.Open: " + exc.ToString()); } return(bRet); }
} // Init /// <summary> /// Updates the value of the named logger param. Note: this will only update the values that LoggerCore /// knows about, and not any of a derived class. /// </summary> /// <param name="i_sThreadId"></param> /// <param name="i_sName"></param> /// <param name="i_sValue"></param> /// <returns></returns> public virtual bool UpdateValue(string i_sThreadId, string i_sName, string i_sValue) { bool bRet = true, bRes = true; LoggerThreadInfo ltiTmp = null; ThreadInfoDictSingleton oDict = null; try { oDict = ThreadInfoDictSingleton.GetInstance(); bRes = oDict.TryGetValue(i_sThreadId, out ltiTmp); if (!bRes) { // We shouldn't get here, unless the caller forgot to call Init(), or has changed the thread-id bRet = false; } // Update the params that were passed in lock (ltiTmp) // Technically it shouldn't be necessary to lock, but it can't hurt { if (i_sName == eRequiredParams.IpAddress.ToString()) { ltiTmp.m_sIpAddress = i_sValue; } else if (i_sName == eRequiredParams.SessionId.ToString()) { ltiTmp.m_sSessionId = i_sValue; } else if (i_sName == eRequiredParams.ComponentName.ToString()) { ltiTmp.m_sComponentName = i_sValue; } else if (i_sName == eRequiredParams.VmcId.ToString()) { ltiTmp.m_sVmcId = i_sValue; } else { bRet = false; // Return false, even when some params have been correctly updated } } // lock } catch { bRet = false; } return(bRet); } // UpdateValue
/// <summary> /// Initializes the parameters, but does not open the logger. Init can be called multiple times. /// Remember to call base.Init() in your overriding member. /// </summary> /// <param name="i_Params"></param> /// <returns></returns> public virtual bool Init(ref ParamCollection i_Params) { bool bRet = true, bRes = true; string sParam = "", sThreadId = ""; LoggerThreadInfo ltiTmp = null; ThreadInfoDictSingleton oDict = null; try { sParam = eRequiredParams.ThreadId.ToString(); sThreadId = i_Params.Find(param => param.GetName() == sParam).GetValue(); oDict = ThreadInfoDictSingleton.GetInstance(); bRes = oDict.TryGetValue(sThreadId, out ltiTmp); if (!bRes) { ltiTmp = new LoggerThreadInfo(); oDict.Add(sThreadId, ltiTmp); ltiTmp.m_sThreadId = sThreadId; } lock (ltiTmp) // Technically it shouldn't be necessary to lock, but it can't hurt { // Pull the required params from the collection to the member variables, for easier access later. sParam = eRequiredParams.IpAddress.ToString(); ltiTmp.m_sIpAddress = i_Params.Find(param => param.GetName() == sParam).GetValue(); sParam = eRequiredParams.SessionId.ToString(); ltiTmp.m_sSessionId = i_Params.Find(param => param.GetName() == sParam).GetValue(); sParam = eRequiredParams.ComponentName.ToString(); ltiTmp.m_sComponentName = i_Params.Find(param => param.GetName() == sParam).GetValue(); sParam = eRequiredParams.VmcId.ToString(); ltiTmp.m_sVmcId = i_Params.Find(param => param.GetName() == sParam).GetValue(); } } catch { bRet = false; // Unnecessary if throwing an exception throw(new ArgumentException("Logger.Init: A required parameter was not included in i_Params: " + sParam)); } return(bRet); } // Init
} // ReplaceIndexesIfEmbedded /// <summary> /// /// The format of the columns will be: DateTime, Severity, IpAddress, SessionId, ComponentName, MethodName, VmcId, ThreadId, Msg /// </summary> /// <param name="i_eSeverity"></param> /// <param name="i_sMsg"></param> /// <returns></returns> public override bool Log(eSeverity i_eSeverity, string i_sMsg) { bool bRet = true, bRes = true; string sComponentName = "", sMethodName = "", sVmc = "", sThread = "", sRem = ""; LoggerThreadInfo oInfo = null; StackTrace oST = null; try { SetLastError(0, ""); oInfo = new LoggerThreadInfo(); oST = new StackTrace(2); // Skip two frames to avoid the overhead of getting a full stack trace // First check to see if we need to log this message or not if (i_eSeverity >= m_FilterLevel) { bRes = ThreadInfoDictSingleton.GetInstance().TryGetValue(Thread.CurrentThread.Name, out oInfo); if (!bRes) { // This isn't always an error, some threads (like pooled or event-handlers) won't be named. //Console.WriteLine("!!!!!{0} In Log(2) TryGetValue failed, calling method '{1}'", DateTime.Now.ToString(), oST.ToString()); if (oInfo == null) { oInfo = new LoggerThreadInfo(); } } sVmc = oInfo.m_sVmcId; sThread = oInfo.m_sThreadId; bRes = ReplaceIndexesIfEmbedded(i_sMsg, ref sVmc, ref sThread, out sRem); if (oInfo.m_sComponentName.Length == 0) { sComponentName = oST.GetFrame(0).GetMethod().Module.Name; } else { sComponentName = oInfo.m_sComponentName; } sComponentName = StripExtension(sComponentName); sMethodName = oST.GetFrame(0).GetMethod().DeclaringType.FullName + "." + oST.GetFrame(0).GetMethod().Name; oST = null; m_Logger.Log(ConvertSeverityToNSLevel(i_eSeverity), string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}", i_eSeverity.ToString(), oInfo.m_sIpAddress, oInfo.m_sSessionId, sComponentName, sMethodName, sVmc, sThread, sRem)); } else { // Below the filter level //Console.WriteLine("Log(2).Log() else - i_eSeverity = '{0}', m_FilterLevel = '{1}'.", i_eSeverity.ToString(), m_FilterLevel.ToString()); } } catch (Exception exc) { bRet = false; SetLastError(-1, string.Format("Caught exception in Log(2), calling method '{0}': '{1}'", oST.ToString(), exc.ToString())); Console.Error.WriteLine(DateTime.Now.ToString() + " " + m_sLastError); } return(bRet); } // Log