Example #1
0
        protected override void Init()
        {
            initialized = false;
            string vFileName = Path.GetFileName(fullName);

            if (FileWriter == null)
            {
                FileWriter = new FileWriter(workingFolder);
            }
            try
            {
                Sql = new SQL(SQLClientType.SQLite);
                if (File.Exists(fullName) == false)
                {
                    //_Sql.CreateDB(_FullName)
                    Sql.ConnectionString = "Data Source = " + fullName + ";";
                    Sql.Connection.Open();
                    //CreateLogTable(_Sql, _LoggingTableName, _Log.StaticDataColumns)
                }
                else
                {
                    Sql.ConnectionString = "Data Source = " + fullName + ";";
                    //_Sql.Close()
                    //_Sql = Nothing
                    //_Sql = New SQL(SQLClientType.SQLite)
                }

                //Check database for errors and recreate Db if get reoubles with connection
                if (Sql.DbObjectExist(enmDbObjectType.Table, loggingTableName) == false)
                {
                    CreateLogTable(Sql, loggingTableName, Log.StaticDataColumns);
                }
                Action <SqlException> vCurrentErrorHandler = Sql.ErrorHandler;
                Sql.ErrorHandler = null;
                try
                {
                    string vStrSqlSelect = string.Format("SELECT COUNT(*) FROM [{0}] LIMIT 1;", loggingTableName);
                    Sql.Value(vStrSqlSelect);
                }
                catch
                {
                    Sql.Connection.Close();
                    //Build Broken file name
                    string vBrokenDbFileName = string.Format("{0}\\Broken_{1}_{2}", Path.GetDirectoryName(fullName), String.Format("'{0:yyyy-MM-dd}'", DateTime.Now), Path.GetFileName(fullName));
                    //  Delete existing broken file!
                    //  If file does not exist this command
                    //will not cause exception so is safe to call without checking!
                    File.Delete(vBrokenDbFileName);
                    //  Rename Log.Db to Broken file name!
                    File.Move(fullName, vBrokenDbFileName);
                    //  Creating Log.Db file
                    Sql.CreateDB(fullName);
                    //Another try to connect to Log.Db
                    Sql.ConnectionString = "Data Source = " + fullName + ";";
                    CreateLogTable(Sql, loggingTableName, Log.StaticDataColumns);
                }
                finally
                {
                    Sql.ErrorHandler = vCurrentErrorHandler;
                }


                if (InsertCmd == null)
                {
                    InsertCmd = GetInsertCommand(Sql, Log.StaticDataColumns);
                }

                if (queue == null)
                {
                    queue = new List <LogData>();
                }
                if (logEvent == null)
                {
                    logEvent = new ManualResetEvent(false);
                }
                if (Th == null)
                {
                    Th      = new Thread(new ThreadStart(this.Writer));
                    Th.Name = this.GetType().FullName + ".Writer";
                    Th.Start();
                }
                initialized = true;
            }
            catch (Exception Ex)
            {
                FileWriter.Write(new SysLogData(this.GetType().FullName, "New", Ex));
                initialized = false;
            }
        }
        private void Init()
        {
            this.initialized = false;
            if (this.fileWriter == null)
            {
                this.fileWriter = new FileWriter(this.workingFolder);
            }

            try
            {
                this.sql = new SQL(SQLClientType.MSSQL);
                this.sql.ConnectionString = this.connectionString;

                // Check database for errors and recreate Db if get reoubles with connection
                if (this.sql.DbObjectExist(enmDbObjectType.Table, this.loggingTableName) == false)
                {
                    this.CreateLogTable(this.sql, this.loggingTableName, this.log.StaticDataColumns);
                }

                Action <SqlException> currentErrorHandler = this.sql.ErrorHandler;
                this.sql.ErrorHandler = null;
                try
                {
                    string sqlSelect = string.Format("SELECT COUNT(*) FROM [{0}] ;", this.loggingTableName);
                    this.sql.Value(sqlSelect);
                }
                catch
                {
                    this.sql.Connection.Close();
                    this.sql.ConnectionString = this.connectionString;
                    this.CreateLogTable(this.sql, this.loggingTableName, this.log.StaticDataColumns);
                }
                finally
                {
                    this.sql.ErrorHandler = currentErrorHandler;
                }

                if (this.insertComand != null)
                {
                    this.insertComand.Dispose();
                    this.insertComand = null;
                }

                this.insertComand = this.GetInsertCommand(this.sql, this.log.StaticDataColumns);

                if (this.queue == null)
                {
                    this.queue = new List <LogData>();
                }

                if (this.logEvent == null)
                {
                    this.logEvent = new ManualResetEvent(false);
                }

                if (this.thread == null)
                {
                    this.thread      = new Thread(new ThreadStart(this.Writer));
                    this.thread.Name = this.GetType().FullName + ".Writer";
                    this.thread.Start();
                }

                this.initialized = true;
            }
            catch (Exception ex)
            {
                this.fileWriter.Write(new SystemLogData(this.GetType().FullName, "New", ex));
                this.initialized = false;
            }
        }