コード例 #1
0
ファイル: Manager.cs プロジェクト: yonglehou/TimberWinR
        public void ProcessConfiguration(CancellationToken cancelToken, Configuration config)
        {
            // Read the Configuration file
            if (config != null)
            {
                if (OnConfigurationProcessed != null)
                {
                    OnConfigurationProcessed(config);
                }

                if (config.StatsDOutputs != null)
                {
                    foreach (var ro in config.StatsDOutputs)
                    {
                        var output = new StatsDOutput(this, ro, cancelToken);
                        Outputs.Add(output);
                    }
                }

                if (config.RedisOutputs != null)
                {
                    foreach (var ro in config.RedisOutputs)
                    {
                        var redis = new RedisOutput(this, ro, cancelToken);
                        Outputs.Add(redis);
                    }
                }
                if (config.ElasticsearchOutputs != null)
                {
                    foreach (var ro in config.ElasticsearchOutputs)
                    {
                        var els = new ElasticsearchOutput(this, ro, cancelToken);
                        Outputs.Add(els);
                    }
                }
                if (config.StdoutOutputs != null)
                {
                    foreach (var ro in config.StdoutOutputs)
                    {
                        var stdout = new StdoutOutput(this, ro, cancelToken);
                        Outputs.Add(stdout);
                    }
                }

                if (config.FileOutputs != null)
                {
                    foreach (var ro in config.FileOutputs)
                    {
                        var output = new FileOutput(this, ro, cancelToken);
                        Outputs.Add(output);
                    }
                }

                foreach (Parser.IISW3CLogParameters iisw3cConfig in config.IISW3C)
                {
                    var elistner = new IISW3CInputListener(iisw3cConfig, cancelToken);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (Parser.W3CLogParameters iisw3cConfig in config.W3C)
                {
                    var elistner = new W3CInputListener(iisw3cConfig, cancelToken);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (Parser.WindowsEvent eventConfig in config.Events)
                {
                    var elistner = new WindowsEvtInputListener(eventConfig, cancelToken);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (var logConfig in config.Logs)
                {
                    var elistner = new LogsListener(logConfig, cancelToken);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (var logConfig in config.TailFiles)
                {
                    var elistner = new TailFileListener(logConfig, cancelToken);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (var tcp in config.Tcps)
                {
                    var elistner = new TcpInputListener(tcp, cancelToken, tcp.Port);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (var udp in config.Udps)
                {
                    var elistner = new UdpInputListener(udp, cancelToken, udp.Port);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (var stdin in config.Stdins)
                {
                    var elistner = new StdinListener(stdin, cancelToken);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }

                foreach (var stdin in config.Generators)
                {
                    var elistner = new GeneratorInput(stdin, cancelToken);
                    Listeners.Add(elistner);
                    foreach (var output in Outputs)
                    {
                        output.Connect(elistner);
                    }
                }


                var computerName = System.Environment.MachineName + "." +
                                   Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                    @"SYSTEM\CurrentControlSet\services\Tcpip\Parameters")
                                   .GetValue("Domain", "")
                                   .ToString();

                foreach (var output in Outputs)
                {
                    var     name = Assembly.GetExecutingAssembly().GetName();
                    JObject json = new JObject(
                        new JProperty("TimberWinR",
                                      new JObject(
                                          new JProperty("version",
                                                        Assembly.GetEntryAssembly().GetName().Version.ToString()),
                                          //GetAssemblyByName("TimberWinR.ServiceHost").GetName().Version.ToString()),
                                          new JProperty("host", computerName),
                                          new JProperty("output", output.Name),
                                          new JProperty("initialized", DateTime.UtcNow)
                                          )));
                    json.Add(new JProperty("type", "Win32-TimberWinR"));
                    json.Add(new JProperty("host", computerName));
                    output.Startup(json);
                }
            }
        }
コード例 #2
0
ファイル: TailFileTests.cs プロジェクト: thoean/TimberWinR
        public void TestTailFile()
        {
            List <JObject> events = new List <JObject>();

            if (File.Exists(".timberwinrdb"))
            {
                File.Delete(".timberwinrdb");
            }

            var mgr = new Manager();

            mgr.LogfileDir = ".";

            var tf = new TailFileArguments();
            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)
            {
                Console.WriteLine("Done!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Assert.AreEqual(100, events.Count);
            }
        }