예제 #1
0
        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            // Event log trace
            EventLogTraceListener traceListener = new EventLogTraceListener(SyslogServiceName);

            // Add the event log trace listener to the collection.
            Trace.Listeners.Add(traceListener);

            if (ssSwitch.TraceInfo)
            {
                Trace.WriteLine(SyslogServiceName + " starting", DbTraceListener.catInfo);
            }

            try
            {
                // Initialize database
                string connString = Properties.Settings.Default.DbConnection;
                if (connString == null || connString.Length == 0)
                {
                    throw new Exception("No database connection string specified");
                }

                // Connect to database - try a few times should database not be up yet
                int  tryCount  = 3;
                bool connected = false;
                do
                {
                    try
                    {
                        using (OleDbConnection conn = new OleDbConnection(connString))
                        {
                            conn.Open();
                        }
                        connected = true;
                    }
                    catch (Exception ex)
                    {
                        if (ssSwitch.TraceError)
                        {
                            Trace.WriteLine("Could not connect to database: " + ex.Message,
                                            DbTraceListener.catError);
                        }

                        // Sleep for 5 seconds then try again
                        Thread.Sleep(5000);
                        tryCount--;
                    }
                } while (!connected && (tryCount > 0));

                if (!connected)
                {
                    throw new Exception("Could not connect to database");
                }

                // Start database log
                DatabaseLog.Instance.Initialize(connString);

                // Load Configuration
                SyslogConfiguration.Instance.Initialize(connString);

                // Clear down messages periodically
                _cleanup = new SyslogMessageCleanup(SyslogConfiguration.Instance.RetentionPeriod);
                _cleanup.Initialize(connString);

                // Create server channel
                int serverPort = Properties.Settings.Default.ServerPort;
                if (serverPort <= 0)
                {
                    throw new Exception("Invalid server port specified in configuration file");
                }

                _channel = new HttpChannel(serverPort);
                ChannelServices.RegisterChannel(_channel, false);

                // Create shared object
                _sharedData = new SharedData();

                // Marshall object
                _obj = RemotingServices.Marshal(_sharedData, "SyslogSharedData");

                // Register for updates
                SyslogConfiguration.Instance.ConfigurationChanged += new
                                                                     Configuration.ConfigurationChangedDel(OnConfigurationChanged);
            }
            catch (Exception ex)
            {
                if (ssSwitch.TraceError)
                {
                    Trace.WriteLine("Could not initialize: " + ex.Message,
                                    DbTraceListener.catError);
                }

                // Fatal - stop right now
                throw;
            }

            try
            {
                Connect();
            }
            catch (Exception ex)
            {
                if (ssSwitch.TraceError)
                {
                    Trace.WriteLine(String.Format("Could not start to collect syslog messages on port {0}: {1}",
                                                  SyslogConfiguration.Instance.Port, ex.Message), DbTraceListener.catError);
                }

                // Not fatal - probably port is in use - so carry on regardless
                // Hope for configuration
                Disconnect();
            }
        }
예제 #2
0
        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            // Event log trace
            EventLogTraceListener traceListener = new EventLogTraceListener(SyslogServiceName);
            // Add the event log trace listener to the collection.
            Trace.Listeners.Add(traceListener);

            if (ssSwitch.TraceInfo)
                Trace.WriteLine(SyslogServiceName + " starting", DbTraceListener.catInfo);

            try
            {
                // Initialize database
                string connString = Properties.Settings.Default.DbConnection;
                if (connString == null || connString.Length == 0)
                    throw new Exception("No database connection string specified");

                // Connect to database - try a few times should database not be up yet
                int tryCount = 3;
                bool connected = false;
                do
                {
                    try
                    {
                        using (OleDbConnection conn = new OleDbConnection(connString))
                        {
                            conn.Open();
                        }
                        connected = true;
                    }
                    catch (Exception ex)
                    {
                        if (ssSwitch.TraceError)
                            Trace.WriteLine("Could not connect to database: " + ex.Message,
                                DbTraceListener.catError);

                        // Sleep for 5 seconds then try again
                        Thread.Sleep(5000);
                        tryCount--;
                    }
                } while (!connected && (tryCount > 0));

                if (!connected)
                    throw new Exception("Could not connect to database");

                // Start database log
                DatabaseLog.Instance.Initialize(connString);

                // Load Configuration
                SyslogConfiguration.Instance.Initialize(connString);

                // Clear down messages periodically
                _cleanup = new SyslogMessageCleanup(SyslogConfiguration.Instance.RetentionPeriod);
                _cleanup.Initialize(connString);

                // Create server channel
                int serverPort = Properties.Settings.Default.ServerPort;
                if (serverPort <= 0)
                    throw new Exception("Invalid server port specified in configuration file");

                _channel = new HttpChannel(serverPort);
                ChannelServices.RegisterChannel(_channel, false);

                // Create shared object
                _sharedData = new SharedData();

                // Marshall object
                _obj = RemotingServices.Marshal(_sharedData, "SyslogSharedData");

                // Register for updates
                SyslogConfiguration.Instance.ConfigurationChanged +=new
                    Configuration.ConfigurationChangedDel(OnConfigurationChanged);
            }
            catch (Exception ex)
            {
                if (ssSwitch.TraceError)
                    Trace.WriteLine("Could not initialize: " + ex.Message,
                        DbTraceListener.catError);

                // Fatal - stop right now
                throw;
            }

            try
            {
                Connect();
            }
            catch (Exception ex)
            {
                if (ssSwitch.TraceError)
                    Trace.WriteLine(String.Format("Could not start to collect syslog messages on port {0}: {1}",
                        SyslogConfiguration.Instance.Port, ex.Message), DbTraceListener.catError);

                // Not fatal - probably port is in use - so carry on regardless
                // Hope for configuration
                Disconnect();
            }
        }