예제 #1
0
        public async Task AddLog(LogRoot logRoot)
        {
            logRoot.Ilog.Record = null; // no need to store as we are using AddRecords
            using var session   = Store.OpenAsyncSession("ApexClient");
            await session.StoreAsync(logRoot, "logRoot/" + logRoot.ApexId);

            await session.SaveChangesAsync();
        }
예제 #2
0
        /// <summary>
        /// Convert a LogRoot object to an XML string
        /// </summary>
        /// <param name="root">The LogRoot object that should be converted into an XML string</param>
        /// <returns>The XML string value of the serialized LogRoot object</returns>
        private string ToXml(LogRoot root)
        {
            string xml;

            using (StringWriter sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    // Convert the object back to XML
                    _serializer.Serialize(writer, root);
                    xml = sww.ToString();
                }
            }
            return(xml);
        }
예제 #3
0
        public bool CopyFrom(LogConfig obj)
        {
            LogConfig source = obj as LogConfig;

            if (null == source)
            {
                return(false);
            }
            if (null != source.Appenders)
            {
                Appenders = source.Appenders;
            }
            if (!source.Root.Equals(default(LogRoot)))
            {
                Root = source.Root;
            }
            return(true);
        }
예제 #4
0
        /// <inheritdoc />
        /// <summary>
        /// Export a Log object as an XML string asynchronously
        /// </summary>
        /// <param name="log">The Log object that should be exported</param>
        /// <returns>The Task object that is associated with this method</returns>
        public override async Task ExportLogAsync(Log log)
        {
            if (!ValidLog(log))
            {
                return;
            }
            await Task.Run(async() =>
            {
                string readContents = null;
                try
                {
                    using (FileStream fs = System.IO.File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        // Read the contents of the file
                        using (StreamReader sr = new StreamReader(fs, TextEncoding))
                        {
                            readContents = await sr.ReadToEndAsync();
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    // Ignored
                }

                LogRoot root;
                if (!string.IsNullOrEmpty(readContents))
                {
                    try
                    {
                        // Convert the XML into a LogRoot object
                        using (TextReader reader = new StringReader(readContents))
                        {
                            root = (LogRoot)_serializer.Deserialize(reader);
                        }
                    }
                    catch (Exception)
                    {
                        if (ThrowErrors)
                        {
                            throw;
                        }
                        return;
                    }
                }
                else
                {
                    root = new LogRoot();
                }

                // Add a Log to the LogRoot object
                root.Logs.Add(log);

                try
                {
                    using (FileStream fs = System.IO.File.Open(FilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                    {
                        string xml = ToXml(root);
                        using (StreamWriter sw = new StreamWriter(fs, TextEncoding))
                        {
                            await sw.WriteAsync(xml);
                            await sw.FlushAsync();
                        }
                    }
                }
                catch (Exception)
                {
                    if (ThrowErrors)
                    {
                        throw;
                    }
                }
            });
        }