示例#1
0
 /// <summary>
 /// Report an serious error. Those errors are usually a sign something in the work
 /// of the application has gone seriously wrong, and operation can not continue
 /// properly (for ex. unexpected exception, access to critical resources etc.)
 /// </summary>
 /// <param name="errorMessage"></param>
 public static void Error(string errorMessage)
 {
     TracerHelper.TraceError(errorMessage);
     if (GlobalDiagnosticsMode)
     {
         Debug.Fail(errorMessage);
     }
 }
示例#2
0
        /// <summary>
        /// Helper, redefine with exception consumption.
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <param name="exception"></param>
        public static void Error(string errorMessage, Exception exception)
        {
            errorMessage = ProcessExceptionMessage(errorMessage, exception);

            TracerHelper.TraceError(errorMessage);
            if (GlobalDiagnosticsMode)
            {
                Debug.Fail(errorMessage);
            }
        }
示例#3
0
        public void SaveToFile()
        {
            TracerHelper.Trace("[" + _fullFileName + "] invoked by: " + ReflectionHelper.GetFullCallingMethodName(2));

            lock (this)
            {
                if (File.Exists(_fullFileName))
                {// Rename the old file - keep it as archive.
                    TimeSpan span            = DateTime.Now - new DateTime(DateTime.Now.Year, 1, 1);
                    string   customTimeValue = "." + DateTime.Now.Year + "." + (int)span.TotalSeconds;
                    string   newFileName     = _fullFileName.ToLower().Replace("." + FileExtension, customTimeValue) + "." + FileExtension;

                    while (File.Exists(newFileName))
                    {
                        newFileName = newFileName.Replace("." + FileExtension, "X" + "." + FileExtension);
                    }

                    File.Move(_fullFileName, newFileName);
                }

                try
                {
                    FileInfo fi = new FileInfo(_fullFileName);
                    if (fi.Directory.Exists == false)
                    {
                        fi.Directory.Create();
                    }

                    using (FileStream stream = new FileStream(_fullFileName, FileMode.Create, FileAccess.Write))
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, _pendingSaveObjects);
                        stream.Close();
                    }
                }
                catch (Exception exception)
                {
                    System.Diagnostics.Debug.Fail("Stream error.");
                    TracerHelper.TraceError("Error occured while creating file stream [" + exception.Message + "].");
                }
            }
        }
示例#4
0
        public bool RestoreFromFile()
        {
            TracerHelper.Trace("[" + _fullFileName + "] invoked by: " + ReflectionHelper.GetFullCallingMethodName(2));

            lock (this)
            {
                _restoredObjects.Clear();

                if (File.Exists(_fullFileName) == false)
                {
                    return(false);
                }

                try
                {
                    using (FileStream stream = new FileStream(_fullFileName, FileMode.Open, FileAccess.Read))
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        _restoredObjects = (Dictionary <string, PersistentData>)formatter.Deserialize(stream);
                        stream.Close();
                    }

                    // Transfer the restored info in the pending info places, where no info exists at all, to persist objects
                    // that are not currently active and will be restorable in the furure.
                    foreach (string id in _restoredObjects.Keys)
                    {
                        if (_pendingSaveObjects.ContainsKey(id) == false)
                        {
                            _pendingSaveObjects.Add(id, _restoredObjects[id]);
                        }
                    }

                    return(true);
                }
                catch (Exception exception)
                {
                    System.Diagnostics.Debug.Fail("Stream error.");
                    TracerHelper.TraceError("Error occured while parsing file stream [" + exception.Message + "].");
                }
            }
            return(false);
        }