Exemple #1
0
        /// <summary>
        /// Write information to the text file.
        /// </summary>
        /// <returns>false if problem persists</returns>
        private static bool WriteErrorLog(string strPathName, Exception objException, params object[] debugParams)
        {
            try
            {
                sw = new StreamWriter(strPathName, true);
                lock (sw)
                {
                    try
                    {
                        sw.WriteLine("Source      : " + objException.Source.ToString().Trim());
                    }
                    catch { }
                    try
                    {
                        sw.WriteLine("Method      : " + objException.TargetSite.Name.ToString());
                    }
                    catch { }
                    sw.WriteLine("Date        : " + DateTime.Now.ToShortDateString());
                    sw.WriteLine("Time        : " + DateTime.Now.ToLongTimeString());
                    //sw.WriteLine("Computer    : " + Dns.GetHostName().ToString());
                    sw.WriteLine("Error       : " + objException.Message.ToString().Trim());
                    sw.WriteLine("Stack Trace : " + objException.StackTrace.ToString().Trim());

                    if ((debugParams != null) && (debugParams.Length > 0))
                    {
                        sw.WriteLine("Debug Info  : ");
                        foreach (object obj in debugParams)
                        {
                            sw.WriteLine("      - " + obj.ToString());
                        }
                    }

                    sw.WriteLine("^^-------------------------------------------------------------------^^");
                    sw.Flush();
                    sw.Close();
                }

                return(true);
            }
            catch (System.IO.IOException ex)
            {
                // File could be in used. Retry to write log into a temporary log file.
                FileInfo logFile           = new FileInfo(strPathName);
                string   uniqueTempLogFile = Path.Combine(logFile.DirectoryName, UniqueValueGenerator.GetUniqueString() + logFile.Name);
                return(WriteErrorLog(uniqueTempLogFile, objException, debugParams));
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// Write information to the text file.
        /// </summary>
        /// <returns>false if problem persists</returns>
        private static bool WriteLog(string strPathName, string strMessage, params object[] debugParams)
        {
            try
            {
                sw = new StreamWriter(strPathName, true);
                lock (sw)
                {
                    sw.WriteLine("Date        : " + DateTime.Now.ToShortDateString());
                    sw.WriteLine("Time        : " + DateTime.Now.ToLongTimeString());
                    sw.WriteLine("Message     : " + strMessage);

                    if ((debugParams != null) && (debugParams.Length > 0))
                    {
                        sw.WriteLine("Debug Info  : ");
                        foreach (object obj in debugParams)
                        {
                            sw.WriteLine("      - " + obj.ToString());
                        }
                    }

                    sw.WriteLine("^^-------------------------------------------------------------------^^");
                    sw.Flush();
                    sw.Close();
                }

                return(true);
            }
            catch (IOException)
            {
                // File could be in used. Retry to write log into a temporary log file.
                FileInfo logFile           = new FileInfo(strPathName);
                string   uniqueTempLogFile = Path.Combine(logFile.DirectoryName, UniqueValueGenerator.GetUniqueString() + logFile.Name);
                return(WriteLog(uniqueTempLogFile, strMessage, debugParams));
            }
            catch
            {
                return(false);
            }
        }