Exemplo n.º 1
0
        /// <summary>
        /// This function builds and writes a JSON formatted log message into the text file
        /// This text file can later be imported either into a JSON reader program/web app or a dedicated web app build specifically for your project
        /// </summary>
        /// <param name="q">qualifier - used to organize the messages</param>
        /// <param name="m">actual message</param>
        /// <param name="caller_function">function that contains the logger call</param>
        public void Log(lq q, string m, string[] tags_array = null)
        {
            // omit all irrelevant logging messages
            if (!show.Contains(q))
            {
                return;
            }
            // calculate time,millisecond,frame
            string daytime = DateTime.Now.Hour.ToString("D2") + ":" + DateTime.Now.Minute.ToString("D2") + ":" + DateTime.Now.Second.ToString("D2") + "." + DateTime.Now.Millisecond.ToString("D3") + " " + DateTime.Now.ToString("tt", System.Globalization.CultureInfo.InvariantCulture);

            // add process tag
            // create and write a message
            try
            {
                // create a temporary json object with current message
                LogMessage temp = new LogMessage
                {
                    timestamp = daytime,
                    qualifier = q.ToString(),
                    caller    = _instance.get_caller(),
                    message   = m,
                    tags      = tags_array
                };
                // add message to the archive
                message_archive.Add(temp);

                // serialize the entire archive, overwriting the file
                using (StreamWriter file = File.CreateText(_instance._log_filename_base))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    // serialize object directly into file stream
                    serializer.Serialize(file, message_archive);
                }
            }
            catch (DirectoryNotFoundException e)
            {
                System.IO.Directory.CreateDirectory("./logs");
                using (StreamWriter stream = File.AppendText(_instance._log_filename_base))
                {
                    stream.Write("created a logs directory due to: " + e.ToString());
                }
            }
            catch (IOException e)
            {
                Debug.WriteLine("could not write to a log: " + e);
            }
        }
Exemplo n.º 2
0
 public OperationResponseDataContract(lq instance)
 {
     _internal = instance;
 }