Exemplo n.º 1
0
        /// <summary>
        /// Logs the <see cref="LogItem"/> using a Smtp Client
        /// </summary>
        /// <param name="logItem">The <see cref="LogItem"/> to sink</param>
        internal override void WriteLog(LogItem logItem)
        {
            SmtpClient client = new SmtpClient();

            client.Host      = mSmtpHost;
            client.Port      = mSmtpPort;
            client.EnableSsl = mEnableSsl;

            //If the username or password has been supplied,
            //we new network credentials
            if (mUsername.Length > 0 | mPassword.Length > 0)
            {
                client.Credentials = new NetworkCredential(mUsername, mPassword);
            }

            //Create the mail message body
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(mHeader);
            sb.Append(logItem.ToString());
            sb.AppendLine(mFooter);

            try
            {
                //Send the message
                using (MailMessage message = new MailMessage(mFromAddress, mToAddress, mSubject, sb.ToString()))
                {
                    client.Send(message);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes the logItem on a tcp connection
        /// </summary>
        /// <param name="logItem">The logItem to write</param>
        internal override void WriteLog(LogItem logItem)
        {
            lock (this)
            {
                List <TcpLogClient> deadClients = null;
                //Send the log message to all tcp clients
                foreach (TcpLogClient client in mClients)
                {
                    try
                    {
                        client.Send(logItem.ToString() + "\f");
                    }
                    catch
                    {
                        if (deadClients == null)
                        {
                            deadClients = new List <TcpLogClient>();
                        }
                        deadClients.Add(client);
                    }
                }

                //Remove dead clients if any
                if (deadClients != null)
                {
                    foreach (TcpLogClient client in deadClients)
                    {
                        RemoveTcpClient(client);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Logs the <see cref="LogItem"/> to a text file
        /// </summary>
        /// <param name="logItem">The <see cref="LogItem"/> to sink</param>
        internal override void WriteLog(LogItem logItem)
        {
            StreamWriter sw = File.AppendText(GetFullFileName(logItem.AssemblyName));

            sw.WriteLine(logItem.ToString());
            sw.Close();
            sw.Dispose();
        }
Exemplo n.º 4
0
        internal override void WriteLog(LogItem logItem)
        {
            //Check to see if this log name exists in the event log
            if (!EventLog.SourceExists(logItem.AssemblyName))
            {
                EventLog.CreateEventSource(logItem.AssemblyName, mLogName);
            }


            EventLog.WriteEntry(logItem.AssemblyName, logItem.ToString(), TranslateLogLevel(logItem.LogLevel));
        }
Exemplo n.º 5
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();
            }
        }
Exemplo n.º 6
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());
 }