/// <summary> /// Write all message in log.json /// </summary> /// <param name="logString">message to log</param> /// <param name="obj">object/module passed message</param> /// <param name="dateTime">date and time logging</param> public void Log(string logString, object obj, DateTime?dateTime = null) { if (!dateTime.HasValue) { dateTime = DateTime.Now; } Type type = obj.GetType(); try { string path = ConfigFromFile.GetPathLogger(); path = path + "\\log.json"; if (!File.Exists(path)) { File.Create(path).Dispose(); } JObject jsonObj = new JObject(); jsonObj["dateTime"] = dateTime.ToString(); jsonObj["Message"] = logString; jsonObj["from"] = type.Namespace + " " + type.Name; using (StreamWriter sw = File.AppendText(path)) { sw.WriteLine(jsonObj.ToString()); } } catch (UnauthorizedAccessException e) { Console.WriteLine("Error access: " + e.Message); } catch (DirectoryNotFoundException e) { Console.WriteLine("Wrong path: " + e.Message); } catch (NotSupportedException e) { Console.WriteLine("Wrong path format: " + e.Message); } catch (Exception e) { Console.WriteLine("Something wrong: " + e.Message); } }
/// <summary> /// Write all message in log.xml /// </summary> /// <param name="logString">message to log</param> /// <param name="obj">object/module passed message</param> /// <param name="dateTime">date and time logging</param> public void Log(string logString, object obj, DateTime?dateTime = null) { if (!dateTime.HasValue) { dateTime = DateTime.Now; } Type type = obj.GetType(); try { string path = ConfigFromFile.GetPathLogger(); path = path + "\\log.xml"; if (!File.Exists(path)) { File.Create(path).Dispose(); } using (xw = new XmlTextWriter(path, null)) { xw.WriteStartDocument(); xw.WriteStartElement("message"); xw.WriteString(dateTime.ToString() + " Message: " + logString + " from " + type.Namespace + " " + type.Name); xw.WriteEndDocument(); } } catch (UnauthorizedAccessException e) { Console.WriteLine("Error access: " + e.Message); } catch (DirectoryNotFoundException e) { Console.WriteLine("Wrong path: " + e.Message); } catch (NotSupportedException e) { Console.WriteLine("Wrong path format: " + e.Message); } catch (Exception e) { Console.WriteLine("Something wrong: " + e.Message); } }
/// <summary> /// Write message in log.txt /// depending on the log level /// </summary> /// <param name="logString">message to log</param> /// <param name="logLevel">enum level log</param> /// <param name="obj">object/module called method/passed message</param> /// <param name="dateTime">date and time logging</param> public void Log(string logString, levels logLevel, object obj, DateTime?dateTime = null) { string logName; switch (logLevel) { case levels.debug: logName = "logdebug.txt"; break; case levels.error: logName = "logerror.txt"; break; case levels.info: logName = "loginfo.txt"; break; case levels.warning: logName = "logwarning.txt"; break; default: logName = "log.txt"; break; } if (!dateTime.HasValue) { dateTime = DateTime.Now; } Type type = obj.GetType(); try { string path = ConfigFromFile.GetPathLogger(); path = path + "\\" + logName; if (!File.Exists(path)) { File.Create(path).Dispose(); } using (sw = File.AppendText(path)) { sw.WriteLine(dateTime.ToString() + " Message: " + logString + " from " + type.Namespace + " " + type.Name); } } catch (UnauthorizedAccessException e) { Console.WriteLine("Error access: " + e.Message); } catch (DirectoryNotFoundException e) { Console.WriteLine("Wrong path: " + e.Message); } catch (NotSupportedException e) { Console.WriteLine("Wrong path format: " + e.Message); } catch (Exception e) { Console.WriteLine("Something wrong: " + e.Message); } }
/// <summary> /// Write message in log.json /// depending on the log level /// </summary> /// <param name="logString">message to log</param> /// <param name="logLevel">enum level log</param> /// <param name="obj">object/module called method/passed message</param> /// <param name="dateTime">date and time logging</param> public void Log(string logString, levels logLevel, object obj, DateTime?dateTime = null) { string logName; switch (logLevel) { case levels.debug: logName = "logdebug.json"; break; case levels.error: logName = "logerror.json"; break; case levels.info: logName = "loginfo.json"; break; case levels.warning: logName = "logwarning.json"; break; default: logName = "log.json"; break; } if (!dateTime.HasValue) { dateTime = DateTime.Now; } Type type = obj.GetType(); try { string path = ConfigFromFile.GetPathLogger(); path = path + "\\" + logName; FileInfo fileInf = new FileInfo(path); if (!fileInf.Exists) { fileInf.Create().Dispose(); } JObject jsonObj = new JObject(); jsonObj["dateTime"] = dateTime.ToString(); jsonObj["Message"] = logString; jsonObj["from"] = type.Namespace + " " + type.Name; using (sw = File.AppendText(path)) { sw.WriteLine(jsonObj.ToString()); } } catch (UnauthorizedAccessException e) { Console.WriteLine("Error access: " + e.Message); } catch (DirectoryNotFoundException e) { Console.WriteLine("Wrong path: " + e.Message); } catch (NotSupportedException e) { Console.WriteLine("Wrong path format: " + e.Message); } catch (Exception e) { Console.WriteLine("Something wrong: " + e.Message); } }