コード例 #1
0
        /// <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);
        }
コード例 #2
0
        }         // 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
コード例 #3
0
        /// <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