示例#1
0
        protected override void Init()
        {
            initialized = false;
            if (FileWriter == null)
            {
                FileWriter = new FileWriter(workingFolder);
            }
            try
            {
                Sql = new SQL(SQLClientType.MSSQL);
                Sql.ConnectionString = connectionString;

                //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}] ;", loggingTableName);
                    Sql.Value(vStrSqlSelect);
                }
                catch
                {
                    Sql.Connection.Close();
                    Sql.ConnectionString = connectionString;
                    CreateLogTable(Sql, loggingTableName, Log.StaticDataColumns);
                }
                finally
                {
                    Sql.ErrorHandler = vCurrentErrorHandler;
                }

                if (InsertCmd != null)
                {
                    InsertCmd.Dispose();
                    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;
            }
        }
示例#2
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;
            }
        }