/// <summary> /// Serialize object in xml format /// </summary> /// <param name="log">Object which we need to serialize</param> public void XmlSerialize(LoggerAssist log) { try { if (!File.Exists($@"{ConfigurationManager.AppSettings["path"]}\Log.{ConfigurationManager.AppSettings["format"]}")) { LoggerAssist[] logs = new LoggerAssist[] { log }; XmlSerializer xmlFormatter = new XmlSerializer(typeof(LoggerAssist[])); using (var fStream = new FileStream($@"{ConfigurationManager.AppSettings["path"]}\Log.{ConfigurationManager.AppSettings["format"]}", FileMode.Create, FileAccess.Write)) { xmlFormatter.Serialize(fStream, logs); } } else { XDocument doc = XDocument.Load($@"{ConfigurationManager.AppSettings["path"]}\Log.{ConfigurationManager.AppSettings["format"]}"); XElement school = doc.Element("ArrayOfLoggerAssist"); school.Add(new XElement("LoggerAssist", new XElement("Type", log.Type), new XElement("Date", log.Date), new XElement("Module", log.Module), new XElement("LogString", log.LogString))); doc.Save($@"{ConfigurationManager.AppSettings["path"]}\Log.{ConfigurationManager.AppSettings["format"]}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("-> Saved log in XML format."); }
/// <summary> /// Make log with next parameters: module, logString /// </summary> /// <param name="module">Module where log was recorded</param> /// <param name="logString">Text of message</param> public void Log(Type module, string logString) { LoggerAssist ls = new LoggerAssist(module, logString); Console.WriteLine($"\t\t\t\t{module}\t{logString}"); string[] arr = new string[1] { String.Concat("\t\t\t\t", module.ToString(), "\t", logString) }; Serialize serialize = new Serialize(); serialize.StartSerialize(ls, arr); }
/// <summary> /// Make log with next parameters: type, date, module, logString /// </summary> /// <param name="type">Type of log message</param> /// <param name="date">Date of log message</param> /// <param name="module">Module where log was recorded</param> /// <param name="logString">Text of message</param> public void Log(TypeOfLog type, DateTime date, Type module, string logString) { LoggerAssist ls = new LoggerAssist(type, date, module, logString); Console.WriteLine($"{type}\t{date}\t{module}\t{logString}"); string[] arr = new string[1] { String.Concat(type.ToString(), "\t", date.ToString(), "\t", module.ToString(), "\t", logString) }; Serialize serialize = new Serialize(); serialize.StartSerialize(ls, arr); }
/// <summary> /// Serialize object in xml format /// </summary> /// <param name="log">Object which we need to serialize</param> public void JsonSerialize(LoggerAssist log) { string[] jsonData = new string[] { JsonConvert.SerializeObject(log) }; try { File.AppendAllLines($@"{ConfigurationManager.AppSettings["path"]}\Log.{ConfigurationManager.AppSettings["format"]}", jsonData); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("-> Saved log in Json format."); }
/// <summary> /// Method which reads app settings from config file and make log in specified format /// </summary> /// <param name="log">Object which we need serialize</param> /// <param name="arr">Text which we need to write in file if we don't need serialization</param> public void StartSerialize(LoggerAssist log, string[] arr) { switch (ConfigurationManager.AppSettings["format"]) { case "xml": XmlSerialize(log); break; case "json": JsonSerialize(log); break; case "txt": PlainText(arr); break; default: Console.WriteLine("Unexpected format"); break; } }