diff --git a/TimberWinR.UnitTests/TailFileTests.cs b/TimberWinR.UnitTests/TailFileTests.cs new file mode 100644 index 0000000..5cfc1cc --- /dev/null +++ b/TimberWinR.UnitTests/TailFileTests.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Newtonsoft.Json; +using NUnit.Framework; +using TimberWinR.Inputs; +using TimberWinR.Parser; +using Newtonsoft.Json.Linq; +using System.Diagnostics; + +namespace TimberWinR.UnitTests +{ + [TestFixture] + public class TailFileTests + { + [Test] + public void TestTailFile() + { + List events = new List(); + + if (File.Exists(".timberwinrdb")) + File.Delete(".timberwinrdb"); + + var mgr = new Manager(); + mgr.LogfileDir = "."; + + var tf = new TailFile(); + var cancelTokenSource = new CancellationTokenSource(); + tf.Location = "TestTailFile1.log"; + + + if (File.Exists(tf.Location)) + File.Delete(tf.Location); + + try + { + var listener = new TailFileListener(tf, cancelTokenSource.Token); + + listener.OnMessageRecieved += o => + { + events.Add(o); + if (events.Count >= 100) + cancelTokenSource.Cancel(); + }; + + GenerateLogFile(tf.Location); + + bool createdFile = false; + while (!listener.Stop && !cancelTokenSource.IsCancellationRequested) + { + Thread.Sleep(100); + if (!createdFile) + { + GenerateLogFile(tf.Location); + createdFile = true; + } + } + } + catch (OperationCanceledException oex) + { + Console.WriteLine("Done!"); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + + } + finally + { + Assert.AreEqual(100, events.Count); + } + } + + private static void GenerateLogFile(string fileName) + { + using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName)) + { + for (int i = 0; i < 100; i++) + { + file.WriteLine("Log Line Number {0}", i); + } + } + } + } +} diff --git a/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj b/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj index 09026ad..577242f 100644 --- a/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj +++ b/TimberWinR.UnitTests/TimberWinR.UnitTests.csproj @@ -67,6 +67,7 @@ + diff --git a/TimberWinR/Inputs/TailFileListener.cs b/TimberWinR/Inputs/TailFileListener.cs index 13af05f..e28ecf3 100644 --- a/TimberWinR/Inputs/TailFileListener.cs +++ b/TimberWinR/Inputs/TailFileListener.cs @@ -240,8 +240,6 @@ namespace TimberWinR.Inputs // threads. private void TailFileWatcher(string fileToWatch) { - Dictionary _fnfmap = new Dictionary(); - using (var syncHandle = new ManualResetEventSlim()) { // Execute the query @@ -253,6 +251,8 @@ namespace TimberWinR.Inputs { string path = Path.GetDirectoryName(fileToWatch); string name = Path.GetFileName(fileToWatch); + if (string.IsNullOrEmpty(path)) + path = "."; // Ok, we have a potential file filter here as 'fileToWatch' could be foo.log or *.log @@ -292,12 +292,8 @@ namespace TimberWinR.Inputs } catch (FileNotFoundException fnfex) { - string fn = fnfex.FileName; - - if (!_fnfmap.ContainsKey(fn)) - LogManager.GetCurrentClassLogger().Warn(fnfex.Message); - else - _fnfmap[fn] = fn; + string fn = fnfex.FileName; + LogManager.GetCurrentClassLogger().Warn(fnfex.Message); } catch (OperationCanceledException oce) { diff --git a/TimberWinR/Manager.cs b/TimberWinR/Manager.cs index c3dbce2..f199c34 100644 --- a/TimberWinR/Manager.cs +++ b/TimberWinR/Manager.cs @@ -60,6 +60,11 @@ namespace TimberWinR Interlocked.Add(ref numMessages, count); } + public Manager() + { + LogsFileDatabase.Manager = this; + } + public Manager(string jsonConfigFile, string logLevel, string logfileDir, CancellationToken cancelToken) { LogsFileDatabase.Manager = this;