private void AutoConfigureHeader()
        {
            var line = _fileReader. ReadLine();            
            var fields = line.Split(new[] { Delimiter }, StringSplitOptions.None);
            bool headerValid = false;
            try
            {     
                var fieldList = new FieldType[fields.Length];
                for (int index = 0; index < fields.Length; index++)
                {
                    var field = fields[index];

                    if (UserSettings.Instance.CsvHeaderFieldTypes.ContainsKey(field))
                    {
                        fieldList[index] = UserSettings.Instance.CsvHeaderFieldTypes[field];
                        
                        //Note: This is a very basic check for a valid header. If any field is detected, the header
                        //is considered valid. This could be made more thorough. 
                        headerValid = true;
                    }
                    else
                        fieldList[index] = new FieldType(LogMessageField.Properties, field, field);                                        
                }

                if (headerValid)
                {
                    _fieldList = fieldList;
                }
                else
                {
                    MessageBox.Show("Could not Parse the Header: " + line, "Error Parsing CSV Header");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(string.Format("Could not Parse the Header: {0}\n\rError: {1}", line, ex),
                                "Error Parsing CSV Header");
            }
        }
Exemple #2
0
 /// <summary>
 /// To parse data according to database fields
 /// </summary>
 /// <param name="logMsg"></param>
 /// <param name="dataValue"></param>
 /// <param name="valueType"></param>
 private void ParseFields(ref LogMessage logMsg, string dataValue, FieldType valueType)
 {
     try
     {
         switch (valueType.Field)
         {
             case LogMessageField.SequenceNr:
                 logMsg.SequenceNr = ulong.Parse(dataValue);
                 break;
             case LogMessageField.LoggerName:
                 logMsg.LoggerName = dataValue;
                 break;
             case LogMessageField.Level:
                 //logMsg.Level = LogLevels.Instance[(Log2Console.Log.LogLevel)Enum.Parse(typeof(Log2Console.Log.LogLevel), dataValue)];
                 logMsg.Level = LogLevels.Instance[dataValue];
                 break;
             case LogMessageField.Message:
                 logMsg.Message = dataValue;
                 break;
             case LogMessageField.ThreadName:
                 logMsg.ThreadName = dataValue;
                 break;
             case LogMessageField.TimeStamp:
                 DateTime time = Convert.ToDateTime(dataValue);
                 logMsg.TimeStamp = time;
                 break;
             case LogMessageField.Exception:
                 logMsg.ExceptionString = dataValue;
                 break;
             case LogMessageField.Properties:
                 logMsg.Properties[valueType.Name] = dataValue;
                 break;
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemple #3
0
        /// <summary>
        /// Db call to fetch records from log table
        /// </summary>
        /// <param name="_fieldList"></param>
        /// <param name="connectionString"></param>
        /// <param name="logTableName"></param>
        /// <returns></returns>
        public List<LogMessage> GetLoggerItems(FieldType[] _fieldList, string connectionString, string logTableName, int rowcount)
        {
            List<LogMessage> logMsgs = new List<LogMessage>();
            MySqlConnection connection = null;

            using (connection = this.DbConnect(connectionString))
            {
                if (connection == null)
                {
                    return null;
                }
                try
                {
                    MySqlCommand sqlCmd = connection.CreateCommand();
                    sqlCmd.CommandText = GetMySqlAllLoggerStatement(_fieldList, logTableName, rowcount);
                    MySqlDataAdapter da = new MySqlDataAdapter();
                    da.SelectCommand = sqlCmd;
                    DataSet ds = new DataSet();

                    connection.Open();
                    da.Fill(ds);
                    connection.Close();

                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[1].Rows)
                        {
                            LogMessage logmsg = new LogMessage();
                            foreach (FieldType item in _fieldList)
                            {
                                ParseFields(ref logmsg, Convert.ToString(row[item.Name]), item);
                            }
                            logMsgs.Add(logmsg);
                        }
                    }
                }
                catch (MySqlException exceptionSQL)
                {
                    connection.Close();
                    ApplicationException exception1;
                    if (exceptionSQL.Message.Contains("Invalid column name"))
                    {
                        exception1 = new ApplicationException("Please map log table columns name properly", exceptionSQL);
                    }
                    else
                    {
                        exception1 = new ApplicationException("Error reading logrows from database collecting all logger names", exceptionSQL);
                    }
                    throw exception1;
                }
                catch (Exception exception)
                {
                    //Column '' does not belong to table Table.
                    connection.Close();
                    ApplicationException exception2;
                    if (exception.Message.Contains("does not belong to table Table"))
                    {
                        exception2 = new ApplicationException("Please map log table columns name properly", exception);
                    }
                    else
                        exception2 = new ApplicationException("Error reading logrows from database collecting all logger names", exception);
                    throw exception2;
                }
            }
            return logMsgs;
        }
Exemple #4
0
        /// <summary>
        /// MySQL command text
        /// </summary>
        /// <param name="_fieldList"></param>
        /// <param name="logTableName"></param>
        /// <returns></returns>
        public static string GetMySqlAllLoggerStatement(FieldType[] _fieldList, string logTableName, int rowscount)
        {
            if (_fieldList.Length > 0)
            {
                //If rows count not set by user then take values from webconfig file.
                if (rowscount <= 0)
                {
                    if (!string.IsNullOrEmpty(System.Configuration.ConfigurationSettings.AppSettings[const_databaseRowsCount]))
                        rowscount = Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings[const_databaseRowsCount]);
                }

                StringBuilder builder = new StringBuilder();

                //To get last inserted 'n' rows from database table.
                builder.Append("SET @RowsCount:=0,@rows:=0;select @RowsCount:=count(" + _fieldList[0].Name);
                builder.Append(") FROM ");
                builder.Append(logTableName);
                builder.Append(";SELECT ");
                foreach (FieldType item in _fieldList)
                {
                    if (!string.IsNullOrEmpty(item.Name))
                        builder.Append(item.Name + ",");
                }
                builder.Length -= 1;//To delete , as the last charecter
                builder.Append(" FROM (SELECT @rows:=@rows+1 AS Row,");
                foreach (FieldType item in _fieldList)
                {
                    if (!string.IsNullOrEmpty(item.Name))
                        builder.Append(item.Name + ",");
                }
                builder.Length -= 1;//To delete , as the last charecter
                builder.Append(" FROM " + logTableName + ") AS LogRows");
                builder.Append(" WHERE (Row between (@RowsCount-" + rowscount + "+1) AND @RowsCount)");

                return builder.ToString();
            }
            return string.Empty;
        }