Пример #1
0
        /// <summary>
        /// Logs an error to a text file
        /// </summary>
        /// <param name="loggInfo">The <see cref="LoggInfo"/> to log from.</param>
        public override void Log(LoggInfo loggInfo)
        {
            StringBuilder loggBuilder = new StringBuilder(loggInfo.Date.ToString() + ";");
            loggBuilder.AppendFormat(" - DEF: {0};", loggInfo.Type.ToString());
            if (!String.IsNullOrWhiteSpace(loggInfo.Text))
            {
                loggBuilder.AppendFormat(" - TEXT: {0};", loggInfo.Text);
            }

            if (!String.IsNullOrWhiteSpace(loggInfo.StackTrace))
            {
                loggBuilder.AppendFormat(" - STACK: {0};", loggInfo.StackTrace);
            }

            if (loggInfo.InnerException != null)
            {
                loggBuilder.AppendFormat(" - EXTYPE: {0};", loggInfo.InnerException.ExceptionType);
                loggBuilder.AppendFormat(" - EXCEPTION: {0} - STACKTRACE: {1}", loggInfo.InnerException.Message, loggInfo.InnerException.StackTrace);
            }

            string filePath = Path.Combine(BaseDirectory, AppName, FileName);
            CreateDirIfNotExist();
            AddToObservable(loggInfo);
            using (StreamWriter log_writer = new StreamWriter(filePath, true, Encoding.UTF8))
            {
                log_writer.WriteLine(loggBuilder.ToString());
            }
        }
Пример #2
0
        protected override System.Collections.ObjectModel.ObservableCollection<LoggInfo> GetLogg()
        {
            ObservableCollection<LoggInfo> loggInfos = new ObservableCollection<LoggInfo>();

            Regex loggRegex = new Regex(@"^([0-9 \.\-\:]+); - DEF: (Error|Info|General|Warning);(?: - TEXT: (.+);)?(:? STACK: (.+);)?(?: - EXTYPE: (.+); - EXCEPTION: (.+) - STACKTRACE: (.*))?$", RegexOptions.IgnoreCase);

            foreach (string filePath in Directory.EnumerateFiles(Path.Combine(BaseDirectory, AppName)))
            {
                using (StreamReader log_reader = new StreamReader(filePath, Encoding.UTF8))
                {
                    while (log_reader.Peek() >= 0)
                    {
                        string line = log_reader.ReadLine();
                        Match logMatch = loggRegex.Match(line);
                        if (logMatch.Success)
                        {
                            LoggInfo loggInfo;
                            DateTime date = DateTime.Parse(logMatch.Groups[1].Value);
                            string text = logMatch.Groups[3].Value;
                            LoggType loggType;
                            if (!Enum.TryParse<LoggType>(logMatch.Groups[2].Value, out loggType))
                                continue;

                            if (logMatch.Groups[4].Success)
                            {
                                Type type = Type.GetType(logMatch.Groups[4].Value);
                                string innerMessage = logMatch.Groups[5].Value;
                                string innerStack = logMatch.Groups[6].Value;
                                loggInfo = new LoggInfo(date, text, loggType, type, innerMessage, innerStack);
                            }
                            else
                            {
                                loggInfo = new LoggInfo(text, loggType);
                                loggInfo.Date = date;
                            }

                            loggInfos.Add(loggInfo);
                        }
                    }
                }
            }
            return loggInfos;
        }
Пример #3
0
 /// <summary>
 /// Logs a new line of text to a text file
 /// </summary>
 /// <param name="type">The type of the error to log</param>
 /// <param name="text">The error message to log.</param>
 /// <param name="innerException">The InnerException of this error</param>
 public override void Log(LoggType type, string text, Exception InnerException = null)
 {
     LoggInfo loggInfo = new LoggInfo(text, type, InnerException);
     Log(loggInfo);
 }
Пример #4
0
 protected static void AddToObservable(LoggInfo loggInfo)
 {
     LoggInfos.Add(loggInfo);
 }
Пример #5
0
 /// <summary>
 /// Logs an error
 /// </summary>
 /// <param name="loggInfo">The <see cref="LoggInfo"/> to log from.</param>
 public abstract void Log(LoggInfo loggInfo);
Пример #6
0
        /// <summary>
        /// Logs an error to an xml file
        /// </summary>
        /// <param name="loggInfo">The <see cref="LoggInfo"/> to log from.</param>
        public override void Log(LoggInfo loggInfo)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Auto;
            settings.Encoding = Encoding.UTF8;
            settings.Indent = Indent;
            string filePath = Path.Combine(BaseDirectory, AppName, FileName);
            bool exists = File.Exists(filePath);
            AddToObservable(loggInfo);
            XmlDocument document = new XmlDocument();
            XmlNode rootNode = document.CreateElement("logs");
            if (exists)
            {
                document.Load(filePath);
            }
            else
            {
                rootNode = document.CreateElement("logs");
                document.AppendChild(rootNode);
            }

            XmlNode logNode = document.CreateElement("log");
            XmlAttribute dateAttr = document.CreateAttribute("date");
            dateAttr.Value = loggInfo.Date.ToString();
            XmlAttribute typeAttr = document.CreateAttribute("type");
            typeAttr.Value = loggInfo.Type.ToString();
            logNode.Attributes.Append(dateAttr);
            logNode.Attributes.Append(typeAttr);

            if (!String.IsNullOrWhiteSpace(loggInfo.Text))
            {
                XmlNode textNode = document.CreateElement("text");
                textNode.InnerText = loggInfo.Text;
                logNode.AppendChild(textNode);
            }
            if (!String.IsNullOrWhiteSpace(loggInfo.StackTrace))
            {
                XmlNode stackNode = document.CreateElement("stackTrace");
                stackNode.InnerText = loggInfo.StackTrace;
                logNode.AppendChild(stackNode);
            }
            if (loggInfo.InnerException != null)
            {
                XmlNode innerNode = document.CreateElement("innerexception");
                if (loggInfo.InnerException.ExceptionType != null)
                {
                    XmlAttribute exTypeAttr = document.CreateAttribute("extype");
                    exTypeAttr.Value = loggInfo.InnerException.ExceptionType.ToString();
                    innerNode.Attributes.Append(exTypeAttr);
                }
                if (!String.IsNullOrWhiteSpace(loggInfo.InnerException.Message))
                {
                    XmlNode innerMessage = document.CreateElement("innermessage");
                    innerMessage.InnerText = loggInfo.InnerException.Message;
                    innerNode.AppendChild(innerMessage);
                }
                if (!String.IsNullOrWhiteSpace(loggInfo.InnerException.StackTrace))
                {
                    XmlNode innerStac = document.CreateElement("innerstacktrace");
                    innerStac.InnerText = loggInfo.InnerException.StackTrace;
                    innerNode.AppendChild(innerStac);
                }

                logNode.AppendChild(innerNode);
            }

            document.DocumentElement.AppendChild(logNode);

            using (XmlWriter writer = XmlWriter.Create(filePath, settings))
            {
                document.Save(writer);
            }
        }
Пример #7
0
        protected override System.Collections.ObjectModel.ObservableCollection<LoggInfo> GetLogg()
        {
            System.Collections.ObjectModel.ObservableCollection<LoggInfo> loggInfos = new System.Collections.ObjectModel.ObservableCollection<LoggInfo>();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            settings.IgnoreWhitespace = true;
            settings.IgnoreProcessingInstructions = true;
            if (CreateDirIfNotExist())
                foreach (string filePath in Directory.EnumerateFiles(Path.Combine(BaseDirectory, AppName), "*" + Path.GetExtension(FileName)))
                {
                    using (XmlReader logReader = XmlReader.Create(filePath, settings))
                    {
                        XmlDocument document = new XmlDocument();
                        document.Load(logReader);

                        foreach (XmlNode logNode in document["logs"].ChildNodes)
                        {
                            LoggInfo loggInfo = new LoggInfo();
                            loggInfo.Date = DateTime.Parse(logNode.Attributes["date"].InnerText);
                            LoggType loggType;
                            if (!Enum.TryParse<LoggType>(logNode.Attributes["type"].InnerText, out loggType))
                                continue;
                            loggInfo.Type = loggType;
                            if (logNode["text"] != null)
                                loggInfo.Text = logNode["text"].InnerText;

                            if (logNode["stackTrace"] != null)
                                loggInfo.StackTrace = logNode["stackTrace"].InnerText;

                            if (logNode["innerexception"] != null)
                            {
                                XmlNode innerNode = logNode["innerexception"];
                                loggInfo.InnerException = new InnerException();
                                if (innerNode.Attributes["extype"] != null)
                                    loggInfo.InnerException.ExceptionType = Type.GetType(innerNode.Attributes["extype"].InnerText);
                                if (innerNode["innermessage"] != null)
                                    loggInfo.InnerException.Message = innerNode["innermessage"].InnerText;
                                if (innerNode["innerstacktrace"] != null)
                                    loggInfo.InnerException.StackTrace = innerNode["innerstacktrace"].InnerText;
                            }

                            loggInfos.Add(loggInfo);
                        }
                    }
                }

            return loggInfos;
        }