示例#1
0
        private void WriteOnFile()
        {
            string[] filePathArray = _filePath.Split('\\');
            string   fileKind      = filePathArray[filePathArray.Length - 1];

            string[]     stringArray;
            string       jsonResult = "";
            MasterLogger m_logger;

            //may need to create enum for this fileKind and add a functionality to each enum type by using extension method
            // then it will be easier for maintance
            switch (fileKind)
            {
            case "TestLog":
            case "logs":
                //reading log file : comma separated and format is timestamp, number, info, description
                //JSON format needs to be Time, Location, Level, Output
                stringArray = _jsonString.Split(',');
                // Time => DateTime.Now, Location => _fullPath, Level => stringArray[2], Output => stringArray[3]
                m_logger   = new MasterLogger(DateTime.Now.ToString("dd/MM/yy hh:mm tt"), _fullPath, stringArray[2], stringArray[3]);
                jsonResult = JsonConvert.SerializeObject(m_logger);

                break;

            case "xml":
                //reading xml file : hierachy is <actions>-<action>-<id>,<name>,<description>,<level>,<timestamp>
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(_jsonString);

                // Time => DateTime.Now, Location => _fullPath, Level => xmlDoc.level, Output => xml.name +" "+ xml.description
                m_logger   = new MasterLogger(DateTime.Now.ToString("dd/MM/yy hh:mm tt"), _fullPath, xmlDoc.SelectSingleNode("/action/level").InnerText, xmlDoc.SelectSingleNode("/action/name").InnerText + "--" + xmlDoc.SelectSingleNode("/action/description").InnerText);
                jsonResult = JsonConvert.SerializeObject(m_logger);
                break;

            case "csv":
                // reading csv file: comma separated and format is Item No, Description, Cost, Paid, Due Date, Result, Notes
                //JSON format needs to be Time, Location, Level, Output
                stringArray = _jsonString.Split(',');
                // Time => DateTime.Now, Location => _fullPath, Level => stringArray[5], Output => stringArray[]
                m_logger   = new MasterLogger(DateTime.Now.ToString("dd/MM/yy hh:mm tt"), _fullPath, stringArray[5], stringArray[1] + " " + stringArray[2] + " " + stringArray[3] + " " + stringArray[6]);
                jsonResult = JsonConvert.SerializeObject(m_logger);

                break;
            }

            Object locker = new Object();

            lock (locker)
            {
                using (StreamWriter sw = File.AppendText(_consolidatedLogFilePath))
                {
                    sw.Write(jsonResult);
                    sw.Write("\n");
                    sw.Flush();
                    sw.Close();
                    Console.WriteLine(jsonResult);
                }
            }
        }
示例#2
0
        private void WriteOnFile(MasterLogger m_logger)
        {
            string jsonResult = JsonConvert.SerializeObject(m_logger);
            Object locker     = new Object();

            lock (locker)
            {
                using (StreamWriter sw = File.AppendText(_consolidatedLogFilePath))
                {
                    sw.Write(jsonResult);
                    sw.Write("\n");
                    sw.Flush();
                    sw.Close();
                    Console.WriteLine(jsonResult);
                }
            }
        }