Changeset 4750 for 2013


Ignore:
Timestamp:
2013-07-28 23:23:25 (6 years ago)
Author:
dezhidki
Message:

Jatkettu MrsMarplen? (virhelogguksen) kehitystä

Location:
2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Errors/MrsMarple.cs

    r4749 r4750  
    44using System.Linq; 
    55using System.Text; 
     6using System.Windows.Forms; 
    67 
    78namespace CastleMaster.Errors 
     
    2223        private const string ERROR_START = "BEGIN ERROR:"; 
    2324        private const string ERROR_END = "END ERROR"; 
     25        private const string EXCEPTION_NAME = "Type of the exception: "; 
    2426        private const string ERROR_METHOD = "Came from method: "; 
    2527        private const string ERROR_MESSAGE = "Message for the developer: "; 
    2628        private const string TRACE = "Stack trace: "; 
     29        private const string LOG_EXTENSION = ".log"; 
    2730 
     31        /// <summary> 
     32        /// Logs the exception's error into a file as a .log file. 
     33        /// </summary> 
     34        /// <param name="e">Exception to handle.</param> 
     35        /// <param name="prefix">Starting message into the log file.</param> 
     36        /// <param name="namePreference">Preference for log's name.</param> 
     37        /// <param name="customName">If namePreference is set to CUSTOM, the name will be changed to this.</param> 
    2838        public static void LogError(Exception e, string prefix, NamePreferences namePreference, string customName = "") 
    2939        { 
    3040            string path = @"logs\"; 
    31  
    3241            if (!Directory.Exists(path)) 
    3342                Directory.CreateDirectory(path); 
     
    4049            } 
    4150 
     51            string finalPath = new StringBuilder(path).Append(fileName).Append(LOG_EXTENSION).ToString(); 
     52            int id = 1; 
     53            while (File.Exists(finalPath)) 
     54            { 
     55                id++; 
     56                finalPath = new StringBuilder(path).Append(fileName).Append("-").Append(id).Append(LOG_EXTENSION).ToString(); 
     57            } 
     58 
    4259            errorMessage.AppendLine(prefix); 
    4360            errorMessage.AppendLine(ERROR_START); 
     61            errorMessage.Append(EXCEPTION_NAME).AppendLine(e.GetType().Name); 
    4462            errorMessage.Append(ERROR_METHOD).AppendLine(e.TargetSite.Name); 
    4563            errorMessage.Append(ERROR_MESSAGE).AppendLine(e.Message); 
     
    4765            errorMessage.Append(ERROR_END); 
    4866 
    49             using (StreamWriter sw = File.CreateText(path + fileName + ".log")) 
     67            using (StreamWriter sw = File.CreateText(finalPath)) 
    5068            { 
    5169                sw.Write(errorMessage.ToString()); 
    5270            } 
    5371        } 
     72 
     73        /// <summary> 
     74        /// Creates a simple error message. 
     75        /// </summary> 
     76        /// <param name="message">Text of the error message.</param> 
     77        public static void ThrowMessage(string message) 
     78        { 
     79            MessageBox.Show(message, Game.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error); 
     80        } 
     81 
     82        /// <summary> 
     83        /// Creates a error message and a .log file. 
     84        /// </summary> 
     85        /// <param name="message">Error message.</param> 
     86        /// <param name="e">Exception that caused the crash.</param> 
     87        /// <param name="logFileNamePreference">Preference for naming .log file.</param> 
     88        /// <param name="customFileName">Will be used to name the .log file if logFileNamePreference is CUSTOM.</param> 
     89        public static void Error(string message, Exception e, NamePreferences logFileNamePreference, string customFileName = "") 
     90        { 
     91            ThrowMessage(message); 
     92            LogError(e, "An error occurred on " + DateTime.Now.ToString("dd'-'MM'-'yyyy") + " at " + DateTime.Now.ToString("H:mm"), logFileNamePreference, customFileName); 
     93        } 
    5494    } 
    5595} 
  • 2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Program.cs

    r4749 r4750  
    1  
    21using CastleMaster.Errors; 
    32using System; 
     3using System.Windows.Forms; 
    44namespace CastleMaster 
    55{ 
     
    1212        static void Main(string[] args) 
    1313        { 
     14#if !DEBUG 
    1415            try 
    1516            { 
     17#endif 
    1618                using (Game game = new Game()) 
    1719                { 
    1820                    game.Run(); 
    1921                } 
     22#if !DEBUG 
    2023            } 
    2124            catch (Exception e) 
    2225            { 
    23                 MrsMarple.LogError(e, "An error has occured:", NamePreferences.DATETIME); 
    24                 Environment.Exit(1); 
     26                MrsMarple.Error("An error has occurred while running.\nA .log file was generated in the logs folder.", e, NamePreferences.DATETIME); 
    2527            } 
     28#endif 
    2629        } 
    2730    } 
Note: See TracChangeset for help on using the changeset viewer.