コード例 #1
0
ファイル: Program.cs プロジェクト: snakefoot/snakeeyes-net
        static void ProgramStart(ref bool serviceRunning)
        {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

            path = System.IO.Path.GetDirectoryName(path);
            Trace.Listeners.Add(new ConsoleTraceListener());
            Trace.Listeners.Add(new LogTraceListener(path + "\\SnakeEyes.txt"));
            Trace.AutoFlush = true;
            Trace.WriteLine("Starting SnakeEyes...");

            EventLog eventLog = null;

            try
            {
                if (EventLog.SourceExists("SnakeEyes"))
                {
                    eventLog        = new EventLog();
                    eventLog.Source = "SnakeEyes";
                }
                else
                {
                    Trace.WriteLine("Warning not installed properly. Cannot write to EventLog");
                }
            }
            catch (System.Security.SecurityException ex)
            {
                Trace.WriteLine("Warning not installed properly. Cannot write to EventLog : %1", ex.Message);
            }

            var builder = new Autofac.Builder.ContainerBuilder();

            builder.RegisterCollection <IProbe>().As <IEnumerable <IProbe> >();
            var dynamicProbes = ProbeTypeLoader.LoadList(typeof(IProbe), path);

            dynamicProbes.AddRange(ProbeTypeLoader.LoadList(typeof(IProbe), System.IO.Path.Combine(path, "Probes")));
            foreach (var probeType in dynamicProbes)
            {
                builder.Register(probeType).As <IProbe>().ExternallyOwned().FactoryScoped().Named(probeType.FullName).MemberOf <IEnumerable <IProbe> >();
                //builder.Register<PerfMonProbe>().As<IProbe>().ExternallyOwned().FactoryScoped().Named(typeof(PerfMonProbe).FullName).MemberOf<IEnumerable<IProbe>>();
                //builder.Register<EventLogProbe>().As<IProbe>().ExternallyOwned().FactoryScoped().Named(typeof(EventLogProbe).FullName).MemberOf<IEnumerable<IProbe>>();
            }

            var container = builder.Build();

            Microsoft.Practices.ServiceLocation.IServiceLocator locator = new AutofacServiceLocator(container);

            Configuration exeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ConfigurationSection diagnosticsSection = exeConfiguration.GetSection("system.diagnostics");

            ConfigurationElementCollection tracesources = diagnosticsSection.ElementInformation.Properties["sources"].Value as ConfigurationElementCollection;

            List <KeyValuePair <string, IProbeMonitor> > probeMonitors = new List <KeyValuePair <string, IProbeMonitor> >();

            foreach (TraceListener traceListener in Trace.Listeners)
            {
                IProbeMonitor probeMonitor = traceListener as IProbeMonitor;
                if (probeMonitor != null)
                {
                    Trace.WriteLine("Configures ProbeMonitor: " + traceListener.Name);
                    probeMonitors.Add(new KeyValuePair <string, IProbeMonitor>(traceListener.Name, probeMonitor));
                }
            }

            ProbeList            probes       = new ProbeList();
            ForwardTraceListener forwardTrace = new ForwardTraceListener();

            foreach (ConfigurationElement tracesource in tracesources)
            {
                string name = tracesource.ElementInformation.Properties["name"].Value.ToString();
                Trace.WriteLine("Configures TraceSource: " + name);

                if (name.IndexOf('.') != -1)
                {
                    string typeName = name.Substring(name.IndexOf('.') + 1);

                    IProbe probe = locator.GetInstance <IProbe>("SnakeEyes." + typeName);
                    if (probe == null)
                    {
                        Trace.WriteLine("Unknown Probe Type: " + typeName);
                        if (eventLog != null)
                        {
                            eventLog.WriteEntry("Unknown Probe Type: " + typeName, EventLogEntryType.Error);
                        }
                        continue;
                    }
                    TraceSource probeSource = probe.ConfigureProbe(name);
                    probeSource.Listeners.Add(forwardTrace);

                    foreach (KeyValuePair <string, IProbeMonitor> probeMonitor in probeMonitors)
                    {
                        probeMonitor.Value.RegisterProbe(probeSource);
                    }

                    probes.AddProbe(DateTime.MinValue, probe);
                }
            }

            if (probes.Count == 0)
            {
                Trace.WriteLine("SnakeEyes have no probes to poll");
                if (eventLog != null)
                {
                    eventLog.WriteEntry("SnakeEyes have no probes to poll", EventLogEntryType.Error);
                }
                return;
            }

            Trace.WriteLine("SnakeEyes starts monitoring...");

            foreach (KeyValuePair <string, IProbeMonitor> probeMonitor in probeMonitors)
            {
                probeMonitor.Value.StartMonitor(probeMonitor.Key);
            }

            using (probes)
            {
                // Implement an email trace listener:
                //  - http://www.barebonescoder.com/2010/04/writing-an-email-custom-trace-listener-for-a-wcf-service/
                while (serviceRunning)
                {
                    if (probes.GetNextPollTime() >= DateTime.UtcNow)
                    {
                        System.Threading.Thread.Sleep(10);
                        continue;
                    }

                    IProbe probe = probes.GetNextPollProbe();
                    try
                    {
                        TimeSpan nextPollTime = probe.ExecuteProbe();
                        probes.PollExecuted(nextPollTime, probe);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.TraceError("Probe Failure: " + ex.Message);
                        if (ex.InnerException != null)
                        {
                            System.Diagnostics.Trace.TraceError("Probe Failure: " + ex.InnerException.Message);
                        }
                        System.Diagnostics.Trace.WriteLine("Probe Failure: " + ex.ToString());

                        probes.PollExecuted(TimeSpan.FromSeconds(5), probe);

                        if (eventLog != null)
                        {
                            eventLog.WriteEntry("Probe Failure: " + ex.Message, EventLogEntryType.Error);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: snakefoot/snakeeyes-net
        private void Form1_Load(object sender, EventArgs e)
        {
            var logView = new LogViewListener(WriteTraceEvent);

            Trace.Listeners.Add(logView);
            Trace.AutoFlush = true;

            Configuration exeConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ConfigurationSection diagnosticsSection = exeConfiguration.GetSection("system.diagnostics");

            ConfigurationElementCollection tracesources = diagnosticsSection.ElementInformation.Properties["sources"].Value as ConfigurationElementCollection;

            List <KeyValuePair <string, IProbeMonitor> > probeMonitors = new List <KeyValuePair <string, IProbeMonitor> >();

            foreach (TraceListener traceListener in Trace.Listeners)
            {
                IProbeMonitor probeMonitor = traceListener as IProbeMonitor;
                if (probeMonitor != null)
                {
                    Trace.WriteLine("Configures ProbeMonitor: " + traceListener.Name);
                    probeMonitors.Add(new KeyValuePair <string, IProbeMonitor>(traceListener.Name, probeMonitor));
                }
            }

            var builder = new Autofac.Builder.ContainerBuilder();

            builder.RegisterCollection <IProbe>().As <IEnumerable <IProbe> >();
            string path          = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            var    dynamicProbes = ProbeTypeLoader.LoadList(typeof(IProbe), path);

            dynamicProbes.AddRange(ProbeTypeLoader.LoadList(typeof(IProbe), System.IO.Path.Combine(path, "Probes")));
            foreach (var probeType in dynamicProbes)
            {
                builder.Register(probeType).As <IProbe>().ExternallyOwned().FactoryScoped().Named(probeType.FullName).MemberOf <IEnumerable <IProbe> >();
                //builder.Register<PerfMonProbe>().As<IProbe>().ExternallyOwned().FactoryScoped().Named(typeof(PerfMonProbe).FullName).MemberOf<IEnumerable<IProbe>>();
                //builder.Register<EventLogProbe>().As<IProbe>().ExternallyOwned().FactoryScoped().Named(typeof(EventLogProbe).FullName).MemberOf<IEnumerable<IProbe>>();
                //builder.Register<PingProbe>().As<IProbe>().ExternallyOwned().FactoryScoped().Named(typeof(PingProbe).FullName).MemberOf<IEnumerable<IProbe>>();
            }

            var container = builder.Build();

            Microsoft.Practices.ServiceLocation.IServiceLocator locator = new AutofacServiceLocator(container);

            foreach (ConfigurationElement tracesource in tracesources)
            {
                string name = tracesource.ElementInformation.Properties["name"].Value.ToString();
                Trace.WriteLine("Configures TraceSource: " + name);

                if (name.IndexOf('.') != -1)
                {
                    string logicalName = name.Substring(0, name.IndexOf('.'));
                    string typeName    = name.Substring(name.IndexOf('.') + 1);

                    IProbe probe = locator.GetInstance <IProbe>("SnakeEyes." + typeName);
                    if (probe == null)
                    {
                        Trace.WriteLine("Unknown Probe Type: " + typeName);
                        continue;
                    }
                    TraceSource probeSource = probe.ConfigureProbe(name);
                    probeSource.Listeners.Add(logView);

                    foreach (KeyValuePair <string, IProbeMonitor> probeMonitor in probeMonitors)
                    {
                        probeMonitor.Value.RegisterProbe(probeSource);
                    }

                    _probes.AddProbe(DateTime.MinValue, probe);
                }
            }

            if (_probes.Count == 0)
            {
                Trace.WriteLine("SnakeEyes have no probes to poll");
                return;
            }

            Trace.WriteLine("SnakeEyes starts monitoring...");

            foreach (KeyValuePair <string, IProbeMonitor> probeMonitor in probeMonitors)
            {
                probeMonitor.Value.StartMonitor(probeMonitor.Key);
            }

            _probeTimer.Enabled = true;
        }