public void SimpleTest()
        {
            LogEntityFactory logEntityFactory = new LogEntityFactory();
            LogEntity        logEntity        = logEntityFactory.GetRandomLogEntity();
            ILogParser       logParser        = new CommonLogParser();
            DateTime         now = DateTime.UtcNow;

            try
            {
                var response = logParser.Parse(logEntity.ToString(), now);

                Assert.AreEqual(logEntity.IpAddress, response.IpAddress, "ip address");
                Assert.AreEqual(logEntity.UserId, response.UserId, "user id");
                Assert.AreEqual(logEntity.ClientIdentity, response.ClientIdentity, "client identity");
                Assert.AreEqual(logEntity.Time.ToShortTimeString(), response.Time.ToShortTimeString(), "time");
                Assert.AreEqual(logEntity.HttpMethod, response.HttpMethod, "http method");
                Assert.AreEqual(logEntity.HttpPath, response.HttpPath, "http path");
                Assert.AreEqual(logEntity.HttpVersion, response.HttpVersion, "http version");
                Assert.AreEqual(logEntity.StatusCode, response.StatusCode, "status code");
                Assert.AreEqual(logEntity.ResponseSize, response.ResponseSize, "size");
                Assert.AreEqual(now, response.TimeReadFromFile, "read from file");
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
Exemple #2
0
        static async Task WriteToFileAsync(CancellationToken ct)
        {
            try
            {
                var directoryPath    = Path.GetDirectoryName(LogSettings.FILE_PATH);
                var logEntityFactory = new LogEntityFactory();

                Log($"Checking if folder {directoryPath} exists...");
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                Log($"Using file {LogSettings.FILE_PATH} for logging...");

                using (Stream stream = File.Open(LogSettings.FILE_PATH, FileMode.Append, FileAccess.Write, FileShare.Read))
                    using (TextWriter writer = new StreamWriter(stream))
                    {
                        Stopwatch sw = Stopwatch.StartNew();

                        while (true)
                        {
                            // Give some feedback that we are doing something ... :)
                            if (sw.Elapsed > TimeSpan.FromSeconds(5))
                            {
                                Log("Still writing...");
                                sw.Restart();
                            }

                            var log = logEntityFactory.GetRandomLogEntity();
                            await writer.WriteLineAsync(log.ToString());

                            await writer.FlushAsync();

                            // Do we need to stop?
                            if (!ct.IsCancellationRequested)
                            {
                                await Task.Delay(_interval);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                Log($"Oops! Hit exception: {ex}");
            }
        }