Exemple #1
0
        /// <summary>
        /// Writes the <see cref="LogItem"/> to a database table
        /// </summary>
        /// <param name="logItem">The <see cref="LogItem"/> to write</param>
        internal override void WriteLog(LogItem logItem)
        {
            //Create a connection to the destination database
            SqlConnection connection = new SqlConnection(mConnectionString);

            connection.Open();

            //Get the table to log to
            DataTable logDataTable = GetLogTable(connection);

            //Add a new row in the data table
            DataRowView dataRow = logDataTable.DefaultView.AddNew();

            try
            {
                if (logDataTable.Columns.IndexOf("ApplicationName") >= 0)
                {
                    dataRow["ApplicationName"] = logItem.AssemblyName.Length > logDataTable.Columns["ApplicationName"].MaxLength ? logItem.AssemblyName.Substring(1, logDataTable.Columns["ApplicationName"].MaxLength) : logItem.AssemblyName;
                }

                if (logDataTable.Columns.IndexOf("LoggerName") >= 0)
                {
                    dataRow["LoggerName"] = this.Name.Length > logDataTable.Columns["LoggerName"].MaxLength ? this.Name.Substring(1, logDataTable.Columns["LoggerName"].MaxLength) : this.Name;
                }

                if (logDataTable.Columns.IndexOf("Severity") >= 0)
                {
                    dataRow["Severity"] = logItem.LogLevel.ToString().Length > logDataTable.Columns["Severity"].MaxLength ? logItem.LogLevel.ToString().Substring(1, logDataTable.Columns["Severity"].MaxLength) : logItem.LogLevel.ToString();
                }

                if (logDataTable.Columns.IndexOf("Message") >= 0)
                {
                    dataRow["Message"] = logItem.Message.Length > logDataTable.Columns["Message"].MaxLength ? logItem.Message.Substring(1, logDataTable.Columns["Message"].MaxLength) : logItem.Message;
                }

                if (logDataTable.Columns.IndexOf("FullMessage") >= 0)
                {
                    string fullMessage = logItem.ToString();
                    dataRow["FullMessage"] = fullMessage.Length > logDataTable.Columns["FullMessage"].MaxLength ? fullMessage.Substring(1, logDataTable.Columns["FullMessage"].MaxLength) : fullMessage;
                }

                if (logDataTable.Columns.IndexOf("Exception") >= 0)
                {
                    string exception = logItem.Exception == null ? "None" : logItem.Exception.ToString();
                    dataRow["Exception"] = exception.Length > logDataTable.Columns["Exception"].MaxLength ? exception.Substring(1, logDataTable.Columns["Exception"].MaxLength) : exception;
                }

                if (logDataTable.Columns.IndexOf("LogDate") >= 0)
                {
                    dataRow["LogDate"] = logItem.LogDate;
                }

                dataRow.EndEdit();

                //Save the new row
                SaveTable(connection, logDataTable);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                    connection.Dispose();
                }
                logDataTable.Dispose();
            }
        }
Exemple #2
0
 /// <summary>
 /// Default implementation. Overridden in derived classes
 /// Writes the logitem to its destination.
 /// </summary>
 /// <param name="logItem">The <see cref="LogItem"/> to write</param>
 internal virtual void WriteLog(LogItem logItem)
 {
     Console.WriteLine(logItem.ToString());
 }