public LogItem(logData other) { this.name = other.name; this.process = other.process; this.date = other.date; this.recDate = other.recDate; this.level = other.level; this.description = other.description; }
public logData ToStruct() { logData item = new logData(); item.name = this.name; item.date = this.date; item.level = this.level; item.description = this.description; return(item); }
/// <summary> /// Convert Log item to string /// </summary> /// <param name="item">Log Item</param> /// <param name="useRecTime">Using received data</param> /// <returns></returns> private string logItemToString(logData item, bool useRecTime = false) { if (useRecTime) { return(item.recDate.ToString("yyyy-MM-dd HH:mm:ss") + " " + item.level + " " + item.description); } else { return(item.date.ToString("yyyy-MM-dd HH:mm:ss") + " " + item.level + " " + item.description); } }
public DialogResult ShowDialog(logData item) { // ----- FILL COMPONENTS ----- lblDevice.Text = item.name; lblTime.Text = item.date.ToString("yyyy-MM-dd HH:mm:ss"); switch (item.level) { case 'E': lblLevel.Text = "Error"; lblLevel.ForeColor = Color.Red; break; case 'W': lblLevel.Text = "Warning"; lblLevel.ForeColor = Color.Orange; break; case 'I': lblLevel.Text = "Info"; lblLevel.ForeColor = Color.Green; break; case 'D': lblLevel.Text = "Debug"; lblLevel.ForeColor = Color.Brown; break; case 'X': lblLevel.Text = "eXtended"; lblLevel.ForeColor = Color.Black; break; default: lblLevel.Text = "Unknown"; lblLevel.ForeColor = Color.Gray; break; } txtDescription.Text = item.description; return(base.ShowDialog()); }
/// <summary> /// Add log to list /// </summary> /// <param name="text">Text to parse</param> /// <param name="dataPath">Path to log files</param> /// <param name="defName">Default device</param> /// <param name="useRecTime">Using received time</param> /// <param name="list">Add new items to list</param> /// <returns></returns> private List <logData> AddLog(string text, string dataPath = "", string defName = "", string defProcess = "", bool useRecTime = false, List <logData> list = null) { logData item = new logData(); item.name = defName; item.process = defProcess; item.level = 'U'; item.description = ""; item.date = DateTime.Now; string name = defName; string process = defProcess; bool isFilled = false; if (list == null) { list = new List <logData>(); } if (deviceList == null) { deviceList = new List <string>(); } text = text.Trim(); // ----- PARSE TO LINES ----- string[] lines = text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < lines.Length; i++) { // ----- CHECK IS DATE ON BEGIN ----- bool isDate = false; if (lines[i].Length > 20) { string datTest = lines[i].Substring(0, 19); DateTime logDate; isDate = DateTime.TryParseExact(datTest, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out logDate); } // ----- CHECK DEVICE NAME ----- int pos = lines[i].IndexOf(';'); if (pos >= 0 && !isDate) { name = lines[i].Substring(0, pos); process = lines[i].Substring(0, pos); lines[i] = lines[i].Remove(0, pos + 1); pos = name.IndexOf('/'); if (pos >= 0) { process = name.Substring(pos + 1, name.Length - (pos + 1)); name = name.Remove(pos, name.Length - pos); if (name.Length == 0) { name = defName; } } } else { name = defName; } if (lines[i].Length >= 21) { try { // ----- CHECK LOG DATE & LOG LEVEL----- string dat = lines[i].Substring(0, 19); if (Conv.IsInt(dat.Substring(0, 4)) && Conv.IsInt(dat.Substring(5, 2)) && Conv.IsInt(dat.Substring(8, 2)) && Conv.IsInt(dat.Substring(11, 2)) && Conv.IsInt(dat.Substring(14, 2)) && Conv.IsInt(dat.Substring(17, 2)) && (dat.Substring(4, 1) == "-") && (dat.Substring(13, 1) == ":") && (lines[i].Substring(20, 1)[0] == 'E' || lines[i].Substring(20, 1)[0] == 'W' || lines[i].Substring(20, 1)[0] == 'I' || lines[i].Substring(20, 1)[0] == 'D' || lines[i].Substring(20, 1)[0] == 'X' || lines[i].Substring(20, 1)[0] == 'U')) { // ----- IF CORRECT ----- DateTime logDate = DateTime.ParseExact(dat, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); if (!deviceList.Contains(name)) { deviceList.Add(name); string cbText = cbDevice.Text; cbDevice.Items.Clear(); cbDevice.Items.Add("all"); for (int j = 0; j < deviceList.Count; j++) { cbDevice.Items.Add(deviceList[j]); } cbDevice.Text = cbText; } // ----- IF DATA TO ADD -> ADD ----- if (isFilled) { list.Add(item); isFilled = false; if (dataPath != "") { // ----- SAVE LOG TO FILE ----- string dir = dataPath + System.IO.Path.DirectorySeparatorChar + item.name; System.IO.Directory.CreateDirectory(dir); string file = dir + System.IO.Path.DirectorySeparatorChar + item.process + "_" + DateTime.Now.ToString("yyMMdd") + ".log"; if (!System.IO.File.Exists(file)) { Files.SaveFile(file, "-----------------------\n " + item.name + " " + DateTime.Now.ToString("yyyy-MM-dd") + "\n-----------------------"); } Files.SaveFile(file, Environment.NewLine + logItemToString(item, useRecTime), true); } item.description = ""; } // ----- FILL DATA ----- item.name = name; item.process = process; item.date = logDate; if (dataPath != "") { item.recDate = DateTime.Now; } else { item.recDate = logDate; } item.level = lines[i].Substring(20, 1)[0]; if (lines[i].Length > 22) { item.description = lines[i].Remove(0, 22); item.description = item.description.Trim(); } isFilled = true; } else { // ----- FILL LINE TO PREVIOUS ITEM ----- item.description += Environment.NewLine; if (name != defName) { item.description += name + ";"; } item.description += lines[i]; } } catch (Exception err) { // ----- FILL LINE TO PREVIOUS ITEM ----- item.description += Environment.NewLine; if (name != defName) { item.description += name + ";"; } item.description += lines[i]; } } else { // ----- FILL LINE TO PREVIOUS ITEM ----- item.description += Environment.NewLine; if (name != defName) { item.description += name + ";"; } item.description += lines[i]; } } // ----- IF DATA TO ADD -> ADD ----- if (isFilled) { list.Add(item); if (dataPath != "") { // ----- SAVE LOG TO FILE ----- string dir = dataPath + System.IO.Path.DirectorySeparatorChar + item.name; System.IO.Directory.CreateDirectory(dir); string file = dir + System.IO.Path.DirectorySeparatorChar + item.process + "_" + DateTime.Now.ToString("yyMMdd") + ".log"; if (!System.IO.File.Exists(file)) { Files.SaveFile(file, "-----------------------\n " + item.name + " " + DateTime.Now.ToString("yyyy-MM-dd") + "\n-----------------------"); } Files.SaveFile(file, Environment.NewLine + logItemToString(item, useRecTime), true); } } else if (item.description.Length > 1) { // ----- IF ALONE DATA -> SAVE AS UNKNOWN ----- item.date = DateTime.Now; item.level = 'U'; if (item.description[0] == '\r') { item.description = item.description.Remove(0, 1); } if (item.description[0] == '\n') { item.description = item.description.Remove(0, 1); } list.Add(item); if (dataPath != "") { // ----- SAVE LOG TO FILE ----- string dir = dataPath + System.IO.Path.DirectorySeparatorChar + item.name; System.IO.Directory.CreateDirectory(dir); string file = dir + System.IO.Path.DirectorySeparatorChar + item.process + "_" + DateTime.Now.ToString("yyMMdd") + ".log"; if (!System.IO.File.Exists(file)) { Files.SaveFile(file, "-----------------------\n " + item.name + " " + DateTime.Now.ToString("yyyy-MM-dd") + "\n-----------------------"); } Files.SaveFile(file, Environment.NewLine + logItemToString(item, useRecTime), true); } } return(list); }