Пример #1
0
        public static string WriteExceptionToLog(string codeSection, Exception ex, bool writeToDumpFile, string details = null, bool addDumpHeaderInfo = false)
        {
            try
            {
                StringBuilder sbError = new StringBuilder();
                sbError.AppendLine("**************************************************************");
                sbError.AppendLine(string.Format("{0} - {1} | Exception generated by {2} | {3}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString(), codeSection, DumpLogHelper.AppVersionInfo));
                if (!string.IsNullOrEmpty(details))
                {
                    sbError.AppendLine(string.Format("Details: {0}", details));
                }
                if (addDumpHeaderInfo)
                {
                    DumpLogHelper.AppendDumpHeaderInfo(sbError);
                }
                sbError.AppendLine(GetFullExceptionMessage(ex));
                sbError.AppendLine("**************************************************************");

                if (writeToDumpFile)
                {
                    DumpLogHelper.WriteExceptionToDumpLog(ex, codeSection, details);
                }

                _debugQueue.Enqueue(sbError.ToString());
                if (_shouldDoConsoleOutput)
                {
                    Console.WriteLine(sbError.ToString());
                }
                WriteToEventLog(sbError.ToString(), System.Diagnostics.EventLogEntryType.Error);
                while (_debugQueue.Count > 0)
                {
                    System.Threading.Thread.Sleep(10);
                }
                return(sbError.ToString());
            }
            catch
            {
                return(GetFullExceptionMessage(ex));
            }
        }
Пример #2
0
        static DebugHelper()
        {
            CheckLogFileSize();

            _tDebug = new System.Threading.Thread(() =>
            {
                while (_shouldRun)
                {
                    int debugQCnt = _debugQueue.Count;
                    int iCnt      = debugQCnt;
                    System.Text.StringBuilder sb = new StringBuilder();
                    if (debugQCnt > 0 && !_initialInfoOutput)
                    {
                        _initialInfoOutput = true;
                        DumpLogHelper.AppendDumpHeaderInfo(sb);
                    }
                    string str = string.Empty;
                    for (int i = 0; i < iCnt; i++)
                    {
                        if (_debugQueue.TryDequeue(out str))
                        {
                            sb.AppendLine(str);
                        }
                    }
                    if (iCnt > 0)
                    {
                        lock (_logLock)
                        {
                            System.IO.File.AppendAllText(LogFileName, sb.ToString());
                        }
                    }
                    System.Threading.Thread.Sleep(100);
                }
            });
            _tDebug.IsBackground = true;

            _tDebug.Start();
        }