コード例 #1
0
        public void AppLog_ReadWrite_DefaultLocation()
        {
            string       folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"LillTek\AppLogs\Test");
            AppLogWriter writer = null;
            AppLogReader reader = null;
            AppLogRecord rWrite, rRead;

            try
            {
                Helper.DeleteFile(folderPath, true);

                writer = AppLogWriter.Open("Test", "my schema", new Version("1.2.3.4"), 0);

                rWrite = new AppLogRecord();
                rWrite.Add("Foo", "Bar");
                writer.Write(rWrite);
                writer.Close();
                writer = null;

                reader = AppLogReader.Open("Test");
                rRead  = reader.Read();
                Assert.AreEqual(rWrite, rRead);
                Assert.AreEqual("my schema", rRead.SchemaName);
                Assert.AreEqual(new Version("1.2.3.4"), rRead.SchemaVersion);
                Assert.IsNull(reader.Read());
                Assert.IsNull(reader.Read());
                reader.Close();
                reader = null;

                Assert.IsTrue(Directory.Exists(folderPath));
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }

                Helper.DeleteFile(folderPath, true);
            }
        }
コード例 #2
0
        public void AppLog_ReadWrite_SpecificLocation()
        {
            string config =
                @"
&section LillTek.AppLog

RootFolder         = {0}
MaxFileSize        = 4096
BufferSize         = 128K
IdleCommitInterval = 5m

&endsection
";

            string       root = TestFolder;
            string       folder = root + "\\AppLogs";
            AppLogWriter writer = null;
            AppLogReader reader = null;
            AppLogRecord rWrite, rRead;

            Helper.CreateFolderTree(root);

            try
            {
                Config.SetConfig(string.Format(config, folder).Replace('&', '#'));
                folder += "\\Test";

                writer = AppLogWriter.Open("Test", "my schema", new Version("1.2.3.4"), 0);

                rWrite = new AppLogRecord();
                rWrite.Add("Foo", "Bar");
                writer.Write(rWrite);
                writer.Close();
                writer = null;

                reader = AppLogReader.Open("Test");
                rRead  = reader.Read();
                Assert.AreEqual(rWrite, rRead);
                Assert.AreEqual("my schema", rRead.SchemaName);
                Assert.AreEqual(new Version("1.2.3.4"), rRead.SchemaVersion);
                Assert.IsNull(reader.Read());
                Assert.IsNull(reader.Read());
                reader.Close();
                reader = null;
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }
            }

            DeleteFolder(root);
        }
コード例 #3
0
        public void AppLog_Position_END()
        {
            string config =
                @"
&section LillTek.AppLog

RootFolder         = {0}
MaxFileSize        = 4096
BufferSize         = 128K
IdleCommitInterval = 5m

&endsection
";

            string       root   = TestFolder;
            string       folder = root + "\\AppLogs";
            AppLogWriter writer = null;
            AppLogReader reader = null;
            AppLogRecord r;

            byte[] data;
            string posEnd;

            data = new byte[2048];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)i;
            }

            Helper.CreateFolderTree(root);

            try
            {
                Config.SetConfig(string.Format(config, folder).Replace('&', '#'));
                folder += "\\Test";

                writer = AppLogWriter.Open("Test", "my schema", new Version("1.2.3.4"), 0);

                for (int i = 0; i < 10; i++)
                {
                    r = new AppLogRecord();
                    r.Add("Index", i.ToString());
                    r.Add("Data", data);
                    writer.Write(r);
                }

                writer.Commit();

                Assert.AreEqual(5, Directory.GetFiles(folder, "*.log").Length);
                Assert.AreEqual(1, Directory.GetFiles(folder, "*.new").Length);
                Assert.IsTrue(File.Exists(folder + "\\Writer.lock"));

                reader = AppLogReader.Open("Test");

                for (int i = 0; i < 10; i++)
                {
                    r = reader.ReadDelete();
                    Assert.AreEqual("my schema", r.SchemaName);
                    Assert.AreEqual(new Version("1.2.3.4"), r.SchemaVersion);

                    Assert.AreEqual(i.ToString(), r["index"]);
                    CollectionAssert.AreEqual(data, (byte[])r["data"]);
                }

                Assert.IsNull(reader.ReadDelete());
                Assert.IsNull(reader.ReadDelete());

                posEnd = reader.Position;

                r = new AppLogRecord();
                r.Add("Index", "10");
                r.Add("Data", data);
                writer.Write(r);
                writer.Commit();

                Assert.AreEqual(1, Directory.GetFiles(folder, "*.log").Length);

                reader.Position = posEnd;
                r = reader.Read();
                Assert.AreEqual("10", r["index"]);
                CollectionAssert.AreEqual(data, (byte[])r["data"]);
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }
            }

            DeleteFolder(root);
        }
コード例 #4
0
        public void AppLog_Idle_Commit()
        {
            string config =
                @"
&section LillTek.AppLog

RootFolder         = {0}
MaxFileSize        = 4096
BufferSize         = 128K
IdleCommitInterval = 2s

&endsection
";

            string       root   = TestFolder;
            string       folder = root + "\\AppLogs";
            AppLogWriter writer = null;
            AppLogReader reader = null;
            AppLogRecord r;

            Helper.CreateFolderTree(root);

            try
            {
                Config.SetConfig(string.Format(config, folder).Replace('&', '#'));
                folder += "\\Test";

                writer = AppLogWriter.Open("Test", "my schema", new Version("1.2.3.4"), 0);

                r = new AppLogRecord();
                r.Add("Index", "1");
                writer.Write(r);

                Thread.Sleep(4000);     // Long enough for an idle commit
                Assert.AreEqual(1, Directory.GetFiles(folder, "*.log").Length);

                r = new AppLogRecord();
                r.Add("Index", "2");
                writer.Write(r);

                Thread.Sleep(4000);     // Long enough for an idle commit
                Assert.AreEqual(2, Directory.GetFiles(folder, "*.log").Length);

                reader = AppLogReader.Open("Test");

                r = reader.Read();
                Assert.AreEqual("1", r["index"]);

                r = reader.Read();
                Assert.AreEqual("2", r["index"]);

                Assert.IsNull(reader.Read());
                Assert.IsNull(reader.Read());
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }
            }

            DeleteFolder(root);
        }
コード例 #5
0
        public void AppLog_Peek()
        {
            string config =
                @"
&section LillTek.AppLog

RootFolder         = {0}
MaxFileSize        = 4096
BufferSize         = 128K
IdleCommitInterval = 5m

&endsection
";

            string       root   = TestFolder;
            string       folder = root + "\\AppLogs";
            AppLogWriter writer = null;
            AppLogReader reader = null;
            AppLogRecord r;

            Helper.CreateFolderTree(root);

            try
            {
                Config.SetConfig(string.Format(config, folder).Replace('&', '#'));
                folder += "\\Test";

                writer = AppLogWriter.Open("Test", "my schema", new Version("1.2.3.4"), 0);

                r = new AppLogRecord();
                r.Add("index", "0");
                writer.Write(r);

                r = new AppLogRecord();
                r.Add("index", "1");
                writer.Write(r);

                r = new AppLogRecord();
                r.Add("index", "2");
                writer.Write(r);

                writer.Close();
                writer = null;

                //-----------------------------------------

                reader = AppLogReader.Open("Test");

                r = reader.Peek();
                Assert.AreEqual("0", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("0", (string)r["index"]);

                r = reader.Read();
                Assert.AreEqual("0", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Read();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("2", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("2", (string)r["index"]);

                r = reader.Read();
                Assert.AreEqual("2", (string)r["index"]);

                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Read());
                Assert.IsNull(reader.Read());

                reader.Close();
                reader = null;

                //-----------------------------------------

                reader          = AppLogReader.Open("Test");
                reader.Position = "BEGINNING";

                r = reader.ReadDelete();
                Assert.AreEqual("0", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Read();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("2", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("2", (string)r["index"]);

                r = reader.Read();
                Assert.AreEqual("2", (string)r["index"]);

                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Read());
                Assert.IsNull(reader.Read());

                reader.Close();
                reader = null;

                //-----------------------------------------

                reader          = AppLogReader.Open("Test");
                reader.Position = "BEGINNING";

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Read();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("2", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("2", (string)r["index"]);

                r = reader.ReadDelete();
                Assert.AreEqual("2", (string)r["index"]);

                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.ReadDelete());
                Assert.IsNull(reader.ReadDelete());

                reader.Close();
                reader = null;

                //-----------------------------------------

                reader          = AppLogReader.Open("Test");
                reader.Position = "BEGINNING";

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.Peek();
                Assert.AreEqual("1", (string)r["index"]);

                r = reader.ReadDelete();
                Assert.AreEqual("1", (string)r["index"]);

                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.ReadDelete());
                Assert.IsNull(reader.ReadDelete());

                reader.Close();
                reader = null;

                //-----------------------------------------

                reader          = AppLogReader.Open("Test");
                reader.Position = "BEGINNING";

                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Peek());
                Assert.IsNull(reader.Read());
                Assert.IsNull(reader.ReadDelete());

                reader.Close();
                reader = null;
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }
            }

            DeleteFolder(root);
        }
コード例 #6
0
        public void AppLog_PersistReaderPos()
        {
            string config =
                @"
&section LillTek.AppLog

RootFolder         = {0}
MaxFileSize        = 4096
BufferSize         = 128K
IdleCommitInterval = 5m

&endsection
";

            string       root   = TestFolder;
            string       folder = root + "\\AppLogs";
            AppLogWriter writer = null;
            AppLogReader reader = null;
            AppLogRecord r;

            Helper.CreateFolderTree(root);

            try
            {
                Config.SetConfig(string.Format(config, folder).Replace('&', '#'));
                folder += "\\Test";

                writer = AppLogWriter.Open("Test", "my schema", new Version("1.2.3.4"), 0);

                for (int i = 0; i < 5; i++)
                {
                    r = new AppLogRecord();
                    r.Add("index", i.ToString());
                    writer.Write(r);
                }

                writer.Commit();

                for (int i = 5; i < 10; i++)
                {
                    r = new AppLogRecord();
                    r.Add("index", i.ToString());
                    writer.Write(r);
                }

                writer.Commit();

                for (int i = 0; i < 10; i++)
                {
                    reader = AppLogReader.Open("Test");

                    r = reader.Read();
                    Assert.AreEqual(i.ToString(), r["index"]);

                    reader.Close();
                    reader = null;
                }

                r = new AppLogRecord();
                r.Add("index", "10");
                writer.Write(r);
                writer.Commit();

                reader = AppLogReader.Open("Test");

                r = reader.Read();
                Assert.AreEqual("10", r["index"]);

                reader.Close();
                reader = null;

                writer.Close();
                writer = null;
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }
            }

            DeleteFolder(root);
        }
コード例 #7
0
        public void AppLog_MaxLogSize()
        {
            // Verify that the log prunes log files to stay within the maximum log size.

            string config =
                @"
&section LillTek.AppLog

RootFolder         = {0}
MaxFileSize        = 4096
BufferSize         = 128K
IdleCommitInterval = 2s
PurgeInterval      = 2s

&endsection
";
            const long MaxLogSize = 10 * 1024 * 1024;  // 10MB

            string       root   = TestFolder;
            string       folder = root + "\\AppLogs";
            AppLogWriter writer = null;
            AppLogRecord record;

            byte[] data;
            long[] fileSizes;
            long   totalSize;

            Helper.CreateFolderTree(root);

            try
            {
                Config.SetConfig(string.Format(config, folder).Replace('&', '#'));
                folder += "\\Test";

                writer = new AppLogWriter("Test", "TestSchema", new Version("1.2.3.4"), MaxLogSize);

                // Write out ~50MB worth of log records and then wait a bit for the log
                // to perform the purge, and then scan the folder to make sure the total
                // size of all of the log files, except for the newest does not exceed
                // the limit.

                data = new byte[1024 * 1024];

                for (int i = 0; i < 50; i++)
                {
                    record         = new AppLogRecord();
                    record["data"] = data;

                    writer.Write(record);
                }

                Thread.Sleep(6000);

                var query =
                    from path in Directory.GetFiles(folder, "*.log", SearchOption.TopDirectoryOnly)
                    orderby path.ToLower() ascending
                    select new FileInfo(path).Length;

                fileSizes = query.ToArray();
                totalSize = 0;

                for (int i = 0; i < fileSizes.Length - 1; i++)
                {
                    totalSize += fileSizes[i];
                }

                Assert.IsTrue(totalSize <= MaxLogSize);
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }
            }

            DeleteFolder(root);
        }
コード例 #8
0
        public void AppLog_RecordAvailableEvent()
        {
            string config =
                @"
&section LillTek.AppLog

RootFolder         = {0}
MaxFileSize        = 4096
BufferSize         = 128K
IdleCommitInterval = 5m

&endsection
";

            string       root   = TestFolder;
            string       folder = root + "\\AppLogs";
            AppLogWriter writer = null;
            AppLogReader reader = null;
            AppLogRecord r;

            Helper.CreateFolderTree(root);

            try
            {
                Config.SetConfig(string.Format(config, folder).Replace('&', '#'));
                folder += "\\Test";

                newRecords = false;

                writer = AppLogWriter.Open("Test", "my schema", new Version("1.2.3.4"), 0);
                reader = AppLogReader.Open("Test");
                reader.RecordAvailable += new LogRecordAvailableHandler(OnRecordAvailable);

                Assert.IsNull(reader.Read());

                r = new AppLogRecord();
                r.Add("foo", "bar");
                writer.Write(r);
                writer.Commit();

                Thread.Sleep(4000);     // Give the record available event a chance to be raised
                Assert.IsTrue(newRecords);

                r = reader.Read();
                Assert.AreEqual("bar", r["foo"]);
            }
            finally
            {
                Config.SetConfig(null);

                if (writer != null)
                {
                    writer.Close();
                }

                if (reader != null)
                {
                    reader.Close();
                }
            }

            DeleteFolder(root);
        }
コード例 #9
0
        /// <summary>
        /// Archives a location fix for an entity.
        /// </summary>
        /// <param name="entityID">The entity identifier.</param>
        /// <param name="groupID">The group identifier or <c>null</c>.</param>
        /// <param name="fix">The location fix.</param>
        /// <remarks>
        /// <note>
        /// <see cref="IGeoFixArchiver" /> implementations must silently handle any internal
        /// error conditions.  <see cref="GeoTrackerNode" /> does not expect to see any
        /// exceptions raised from calls to any of these methods.  Implementations should
        /// catch any exceptions thrown internally and log errors or warnings as necessary.
        /// </note>
        /// </remarks>
        public void Archive(string entityID, string groupID, GeoFix fix)
        {
            lock (syncLock)
            {
                try
                {
                    if (isStopped)
                    {
                        throw new InvalidOperationException("AppLogGeoFixArchiver: Cannot log to a stopped archiver.");
                    }

                    if (logWriter == null)
                    {
                        logWriter = new AppLogWriter(logName, SchemaName, SchemaVersion, maxSize);
                        logWriter.PurgeInterval = purgeInterval;
                    }

                    var record = new AppLogRecord();

                    if (entityID != null)
                    {
                        record["EntityID"] = entityID;
                    }

                    if (groupID != null)
                    {
                        record["GroupID"] = groupID;
                    }

                    if (fix.TimeUtc.HasValue)
                    {
                        record["TimeUtc"] = fix.TimeUtc;
                    }

                    record["Technology"] = fix.Technology;

                    if (!double.IsNaN(fix.Latitude))
                    {
                        record["Latitude"] = fix.Latitude;
                    }

                    if (!double.IsNaN(fix.Longitude))
                    {
                        record["Longitude"] = fix.Longitude;
                    }

                    if (!double.IsNaN(fix.Altitude))
                    {
                        record["Altitude"] = fix.Altitude;
                    }

                    if (!double.IsNaN(fix.Course))
                    {
                        record["Course"] = fix.Course;
                    }

                    if (!double.IsNaN(fix.Speed))
                    {
                        record["Speed"] = fix.Speed;
                    }

                    if (!double.IsNaN(fix.HorizontalAccuracy))
                    {
                        record["HorizontalAccuracy"] = fix.HorizontalAccuracy;
                    }

                    if (!double.IsNaN(fix.VerticalAccurancy))
                    {
                        record["VerticalAccurancy"] = fix.VerticalAccurancy;
                    }

                    record["NetworkStatus"] = fix.NetworkStatus;

                    logWriter.Write(record);
                    node.IncrementFixesReceivedBy(1);
                }
                catch (Exception e)
                {
                    SysLog.LogException(e);
                }
            }
        }