Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="method"></param>
        /// <param name="error"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static void LogError(MethodBase method, string error, params object[] values)
        {
            ParameterInfo[] parms      = method.GetParameters();
            string          parameters = String.Empty;

            for (int i = 0; i < parms.Length; i++)
            {
                if (i >= values.Length)
                {
                    parameters += String.Format("{0} = {1}\r\n", parms[i].Name, "missing parameter value???");
                }
                else
                {
                    parameters += String.Format("{0} = {1}\r\n", parms[i].Name, values[i] == null ? "null" : values[i]);
                }
            }

            parameters += "\r\n\r\nOther Values:\r\n\r\n";

            for (int i = 0; i < values.Length; i++)
            {
                parameters += String.Format("{0}\r\n", values[i]);
            }

            string msg = String.Format("Date: {0}\r\n\r\nError Message: {1}\r\n" +
                                       "\r\nParameters: \r\n\r\n{2}", DateTime.Now.ToString("g"), error, parameters);

            LoggingErrorCacheItem previousError = null;

            if (!GetPreviousErrorData(error, ref previousError))
            {
                return;
            }

            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 :-\
            }
        }
Beispiel #2
0
        /// <summary>
        /// Logs an internal error
        /// </summary>
        /// <param name="method">Method where error occured</param>
        /// <param name="ex">Ecxeption being raised</param>
        /// <param name="values">Parameter values within the method</param>
        /// <returns>void</returns>
        public static void LogError(MethodBase method, Exception ex, params object[] values)
        {
            LoggingErrorCacheItem previousError = null;

            if (!GetPreviousErrorData(ex.Message, ref previousError))
            {
                return;
            }

            ParameterInfo[] parms      = method.GetParameters();
            string          parameters = String.Empty;

            for (int i = 0; i < parms.Length; i++)
            {
                if (i >= values.Length)
                {
                    parameters += String.Format("{0} = {1}\r\n", parms[i].Name, "missing parameter value???");
                }
                else
                {
                    parameters += String.Format("{0} = {1}\r\n", parms[i].Name, values[i] == null ? "null" : values[i]);
                }
            }


            string msg = String.Format("Date: {0}\r\n\r\nError Message: {1}\r\n" +
                                       "\r\nParameters: \r\n\r\n{2}\r\n\r\nStackTrace\r\n\r\n{3}\r\n\r\nInnerException\r\n\r\n{4}",
                                       DateTime.Now.ToString("g"), ex.Message, parameters,
                                       ex.StackTrace == null ? "No Stack Trace" : ex.StackTrace.ToString(),
                                       ex.InnerException == null ? "No Inner Exception" : ex.InnerException.ToString());

            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 :-\
            }
        }
Beispiel #3
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);
        }
Beispiel #4
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 :-\
                }
            }
        }