/// <summary>Logs the Syslog message to database.</summary> /// <param name="rcvd"> Message that was received from incoming transmission</param> /// <param name="sender">The sender IPAddress</param> public void LogToDatabase(string rcvd, IPAddress sender) { if (logLevel != LogLevels.Debug) { LogToConsole("Start handling received data from " + sender.ToString() + "\r\n" + rcvd); SysLogMessage mymsg = new SysLogMessage(rcvd); mymsg.Sender = sender.ToString(); LogToConsole("SyslogMessage object created."); dataConnection = Data.GetConnection(connectionString); if (dataConnection.State != ConnectionState.Open) { LogToConsole("Database connection is not Open! The state is: " + dataConnection.State.ToString() + "\r\nSending the message to EventLog"); } else { Data.GenerateEntry(dataConnection, mymsg); } } LogToConsole("Message handled from " + sender.ToString() + ":\r\n" + rcvd); }
public void LogApplicationActivity(string msg, Severities severity, Facilities facility, SqlConnection dataConnection) { SysLogMessage message = new SysLogMessage(); message.received = DateTime.Now; message.senderIP = IPHelpers.GetLocalAddress().ToString(); message.sender = IPHelpers.GetLocalHost(); message.severity = Severities.Informational; message.facility = Facilities.log_audit; message.version = 1; message.hostname = IPHelpers.GetLocalHost().HostName; message.appName = "Virvent SysLog Service"; message.procID = "0"; message.timestamp = DateTime.Now; message.msgID = "VIRVENT@32473"; message.prival = 6; message.msg = msg; Data.GenerateEntry(dataConnection, message); }
public static void GenerateEntry(SqlConnection sqlConnection, SysLogMessage sysLogMessage) { if (sqlConnection.State != ConnectionState.Open) { sqlConnection = Data.GetConnection(sqlConnection.ConnectionString); } if (sqlConnection.State == ConnectionState.Open) { SqlCommand sqlCommand = new SqlCommand("", sqlConnection); Int64 thissdid = 0; if (sysLogMessage.SD != "-") { foreach (DictionaryEntry de in sysLogMessage.sdParams) { string tsdid = ((string[])de.Key)[0]; string tsdpn = ((string[])de.Key)[1]; string tsdpv = (string)de.Value; if (thissdid == 0) { sqlCommand.CommandText = "NewSD"; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.Clear(); sqlCommand.Parameters.AddWithValue("@sdid", tsdid); sqlCommand.Parameters.AddWithValue("@sdpn", tsdpn); sqlCommand.Parameters.AddWithValue("@sdpv", tsdpv); thissdid = (Int64)sqlCommand.ExecuteScalar(); } else { sqlCommand.CommandText = "NextSD"; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.Clear(); sqlCommand.Parameters.AddWithValue("@nsdid", thissdid); sqlCommand.Parameters.AddWithValue("@sdid", tsdid); sqlCommand.Parameters.AddWithValue("@sdpn", tsdpn); sqlCommand.Parameters.AddWithValue("@sdpv", tsdpv); sqlCommand.ExecuteScalar(); } } } sqlCommand.CommandText = "NewLog"; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.Clear(); sqlCommand.Parameters.AddWithValue("@timestamp", sysLogMessage.received.UtcDateTime); sqlCommand.Parameters.AddWithValue("@sourceip", sysLogMessage.senderIP); sqlCommand.Parameters.AddWithValue("@sourcename", sysLogMessage.sender.HostName); sqlCommand.Parameters.AddWithValue("@severity", sysLogMessage.severity); sqlCommand.Parameters.AddWithValue("@facility", sysLogMessage.facility); sqlCommand.Parameters.AddWithValue("@version", sysLogMessage.version); sqlCommand.Parameters.AddWithValue("@hostname", sysLogMessage.hostname); sqlCommand.Parameters.AddWithValue("@appname", sysLogMessage.appName); sqlCommand.Parameters.AddWithValue("@procid", sysLogMessage.procID); sqlCommand.Parameters.AddWithValue("@msgid", sysLogMessage.msgID); sqlCommand.Parameters.AddWithValue("@msgtimestamp", sysLogMessage.timestamp.UtcDateTime); sqlCommand.Parameters.AddWithValue("@msgoffset", sysLogMessage.timestamp.Offset.ToString()); if (thissdid == 0) { sqlCommand.Parameters.AddWithValue("@sdid", null); } else { sqlCommand.Parameters.AddWithValue("@sdid", thissdid); } sqlCommand.Parameters.AddWithValue("@msg", sysLogMessage.msg); sqlCommand.ExecuteNonQuery(); } }