示例#1
0
        /// <summary>
        /// Finds previous error data, if already exists, appends further occurrance data to the file,
        /// if not exists, creates the cache item
        /// </summary>
        /// <param name="error">error caused</param>
        /// <param name="errorItem"></param>
        /// <returns></returns>
        private static bool GetPreviousErrorData(string error, ref LoggingErrorCacheItem errorItem)
        {
            errorItem = (LoggingErrorCacheItem)_errorCache.Get(error);

            if (errorItem != null)
            {
                if (!CanLogData(errorItem.FileName))
                {
                    return(false);
                }

                if (errorItem.NumberOfErrors > _maximumReoccuranceCount)
                {
                    return(false);
                }

                // if this error has occurred before, then append the date/time to the existing message
                StreamWriter w = File.AppendText(errorItem.FileName);
                try
                {
                    if (errorItem.NumberOfErrors == 0)
                    {
                        w.WriteLine(String.Empty);
                    }

                    if (errorItem.NumberOfErrors < _maximumReoccuranceCount)
                    {
                        w.WriteLine("Further Occurance: {0}", DateTime.Now.ToString());
                    }
                    else if (errorItem.NumberOfErrors == _maximumReoccuranceCount)
                    {
                        w.WriteLine("Maximum Reoccurance Rate Reached - Further reporting on this error will be supressed");
                    }
                }
                finally
                {
                    // Update the underlying file.
                    w.Flush();
                    w.Close();
                    w.Dispose();
                    w = null;
                }

                errorItem.IncrementErrors();

                return(false);
            }
            else
            {
                if (_errorPath == null)
                {
                    string dummy = Path;
                }

                if (!Directory.Exists(_errorPath))
                {
                    Directory.CreateDirectory(_errorPath);
                }

                // add this error to the cache
                errorItem          = new LoggingErrorCacheItem(error, error);
                errorItem.FileName = _errorPath + String.Format("{0}.log",
                                                                DateTime.Now.ToString("ddMMyyyy HH mm ss ffff"));

                if (!CanLogData(errorItem.FileName))
                {
                    return(false);
                }

                if (!_errorCache.Add(error, errorItem))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#2
0
        /// <summary>
        /// Add's an exception to the event log
        /// </summary>
        /// <param name="e">Exception to add to log file</param>
        /// <param name="extraData">extra data to be added along with exception information</param>
        public static void Add(Exception e, string extraData = "")
        {
            if (e == null)
            {
                return;
            }

            using (TimedLock.Lock(_lockObject))
            {
                LoggingErrorCacheItem previousError = null;

                previousError = (LoggingErrorCacheItem)_errorCache.Get(e.Message);

                if (previousError != null)
                {
                    if (!CanLogData(previousError.FileName))
                    {
                        return;
                    }

                    if (previousError.NumberOfErrors > _maximumReoccuranceCount)
                    {
                        return;
                    }

                    // if this error has occurred before, then append the date/time to the existing message
                    StreamWriter w = File.AppendText(previousError.FileName);
                    try
                    {
                        if (previousError.NumberOfErrors == 0)
                        {
                            w.WriteLine(String.Empty);
                        }

                        if (previousError.NumberOfErrors < _maximumReoccuranceCount)
                        {
                            w.WriteLine("Further Occurance: {0}", DateTime.Now.ToString());
                        }
                        else if (previousError.NumberOfErrors == _maximumReoccuranceCount)
                        {
                            w.WriteLine("Maximum Reoccurance Rate Reached - Further reporting on this error will be supressed");
                        }
                    }
                    finally
                    {
                        // Update the underlying file.
                        w.Flush();
                        w.Close();
                        w.Dispose();
                        w = null;
                    }

                    previousError.IncrementErrors();

                    return;
                }
                else
                {
                    if (_errorPath == null)
                    {
                        string dummy = Path;
                    }

                    if (!Directory.Exists(_errorPath))
                    {
                        Directory.CreateDirectory(_errorPath);
                    }

                    // add this error to the cache
                    previousError          = new LoggingErrorCacheItem(e.Message, e.Message);
                    previousError.FileName = _errorPath + String.Format("{0}.log",
                                                                        DateTime.Now.ToString("ddMMyyyy HH mm ss ffff"));

                    if (!CanLogData(previousError.FileName))
                    {
                        return;
                    }

                    if (!_errorCache.Add(e.Message, previousError))
                    {
                        return;
                    }
                }

                string Inner      = "Unknown";
                string Message    = "Unknown";
                string Source     = "Unknown";
                string StackTrace = "Unknown";
                string TargetSite = "Unknown";

                if (e != null)
                {
                    Inner      = e.InnerException == null ? "InnerException is null" : e.InnerException.ToString();
                    Message    = e.Message;
                    Source     = e.Source;
                    StackTrace = e.StackTrace;
                    TargetSite = e.TargetSite == null ? "No Target Site" : e.TargetSite.ToString();
                }

                string Msg = String.Format("Date: {5} {6}\r\n\r\nError Message: {0}\r\n" +
                                           "\r\nInner Exception: {1}\r\n\r\nSource: {2}\r\n" +
                                           "\r\nStackTrace: {3}\r\n\r\nTarget Site: {4}\r\n",
                                           Message, Inner, Source, StackTrace, TargetSite, DateTime.Now.ToString("HH:mm:ss"),
                                           DateTime.Now.ToString("dd/MM/yyyy"));

                if (!String.IsNullOrEmpty(extraData))
                {
                    Msg = String.Format("{0}\r\n\r\n{1}\r\n", Msg, extraData);
                }

                try
                {
                    StreamWriter w = File.AppendText(previousError.FileName);
                    try
                    {
                        w.Write(Msg);
                    }
                    finally
                    {
                        // Update the underlying file.
                        w.Flush();
                        w.Close();
                        w.Dispose();
                        w = null;
                    }
                }
                catch
                {
                    //ignore, I suppose :-\
                }
            }
        }