#Region "Includes" Imports System.Xml Imports System.Data.SqlClient Imports Common.Settings #End Region Public Class Common Public Const SERVICE_DISPLAY_NAME As String = "EDKB10" Public Const DLL_DISPLAY_NAME As String = "EDKB10 Library" Private Const DSN_DECRYPT_PASSWORD As String = "HutterundMueller" #Region "Members" Private m_DSN As String Private m_StartJobInterval As Integer Private m_WatchJobInterval As Integer Private m_MaximalStartDuration As Integer Private m_EventLogName As String Private m_LogTarget As LogTarget Private m_MailServer As String Private m_MailFrom As String Private m_MailServerAuthMethod As MailServerAuthMethod Private m_MailServerAuthUser As String Private m_MailServerAuthPassword As String Private m_MaxStarterNotifications As Integer Private m_MaxWatcherNotifications As Integer Private m_EventLog As EventLog #End Region #Region "Constructor" 'Creates a new instance of this object Public Sub New() Try Dim reader As System.IO.StreamReader Dim s As String reader = System.IO.File.OpenText(AppDomain.CurrentDomain.BaseDirectory & "bms_conn.cfg") s = reader.ReadLine m_DSN = ZpCryptography.DsnCrypto.Decrypt(s, DSN_DECRYPT_PASSWORD) LoadSettings() Catch ex As Exception Throw ex End Try End Sub #End Region #Region "Private Methods" 'Loads all settings from xml configuration file Private Sub LoadSettings() Try Dim xmlDoc As New XmlDocument xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory & "bms_settings.xml") m_MaximalStartDuration = CInt(xmlDoc.SelectSingleNode("/root/config/MaximumStartDuration").InnerText) m_StartJobInterval = CDbl(xmlDoc.SelectSingleNode("/root/config/StartJobInterval").InnerText) * 1000 * 60 m_WatchJobInterval = CDbl(xmlDoc.SelectSingleNode("/root/config/WatchJobInterval").InnerText) * 1000 * 60 m_EventLogName = xmlDoc.SelectSingleNode("/root/config/EventLogName").InnerText m_LogTarget = CType(CInt(xmlDoc.SelectSingleNode("/root/config/LogTarget").InnerText), LogTarget) m_MailServer = xmlDoc.SelectSingleNode("/root/config/MailServer").InnerText m_MailFrom = xmlDoc.SelectSingleNode("/root/config/MailFrom").InnerText m_MailServerAuthMethod = CType(xmlDoc.SelectSingleNode("/root/config/MailServerAuth/Method").InnerText, MailServerAuthMethod) m_MailServerAuthUser = xmlDoc.SelectSingleNode("/root/config/MailServerAuth/UserName").InnerText m_MailServerAuthPassword = xmlDoc.SelectSingleNode("/root/config/MailServerAuth/Password").InnerText m_MaxStarterNotifications = CInt(xmlDoc.SelectSingleNode("/root/config/MaxStarterNotifications").InnerText) m_MaxWatcherNotifications = CInt(xmlDoc.SelectSingleNode("/root/config/MaxWatcherNotifications").InnerText) m_EventLog = New EventLog(m_EventLogName) m_EventLog.Source = "Service" Catch ex As Exception Throw ex End Try End Sub #End Region #Region "Public Methods" 'Logs an exception Public Sub Log(ByVal source As String, ByVal message As String) Try Log(source, message, EventLogEntryType.Information) Catch ex As Exception Log(SERVICE_DISPLAY_NAME, "Catastrophical error while writing to EventLog " + m_LogTarget.ToString() + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error, LogTarget.SystemEventLog) End Try End Sub 'Logs an exception Public Sub Log(ByVal ex As Exception) Try Log(ex.Source, ex.Message, EventLogEntryType.Error) Catch exNew As Exception Log(SERVICE_DISPLAY_NAME, "Catastrophical error while writing to EventLog " + m_LogTarget.ToString() + Environment.NewLine + exNew.Message + Environment.NewLine + exNew.StackTrace, EventLogEntryType.Error, LogTarget.SystemEventLog) End Try End Sub 'Logs an message to the system eventlog or database event log Public Sub Log(ByVal source As String, ByVal message As String, ByVal eventLogType As EventLogEntryType) Try Log(source, message, eventLogType, m_LogTarget) 'MsgBox(CInt("23s45")) force crash 4 debug Catch ex As Exception Log(SERVICE_DISPLAY_NAME, "Catastrophical error while writing to EventLog " + m_LogTarget.ToString() + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error, LogTarget.SystemEventLog) End Try End Sub 'Logs an event Public Sub Log(ByVal source As String, ByVal message As String, ByVal eventLogType As EventLogEntryType, ByVal logTarget As LogTarget) Try Select Case logTarget Case logTarget.SystemEventLog If Not System.Diagnostics.EventLog.SourceExists(m_EventLogName) Then System.Diagnostics.EventLog.CreateEventSource(m_EventLogName, m_EventLogName) End If 'System.Diagnostics.EventLog.WriteEntry(m_EventLogName, message, eventLogType) EventLog.WriteEntry(m_EventLogName, message, eventLogType) Case logTarget.Database Dim sqlCon As New SqlConnection(m_DSN) Dim sqlCom As New SqlCommand sqlCon.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlCon sqlCom.CommandText = "CreateEventLogEntry" sqlCom.Parameters.Add(New SqlParameter("@TypeId", CInt(eventLogType))) sqlCom.Parameters.Add(New SqlParameter("@Source", source)) sqlCom.Parameters.Add(New SqlParameter("@Computer", Environment.MachineName)) sqlCom.Parameters.Add(New SqlParameter("@EventDesc", message)) sqlCom.ExecuteNonQuery() sqlCon.Close() End Select Catch ex As Exception m_EventLog.WriteEntry("EXCEPTION ON Common.Log: " + ex.Message, EventLogEntryType.Error) 'Log(source, "EXCEPTION ON Common.Log: " + ex.Message + Environment.NewLine + Environment.NewLine + message, eventLogType, logTarget.SystemEventLog) 'Log(SERVICE_DISPLAY_NAME, "EXCEPTION ON Common.Log: " + ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error, logTarget.SystemEventLog) End Try End Sub 'Sends a mail using sql server and cdo object Public Sub SendMail(ByVal receiver As String, ByVal subject As String, ByVal message As String) Dim sqlConn As New SqlConnection(m_DSN) Try Dim sqlCom As New SqlCommand Dim da As New SqlDataAdapter Dim ds As New DataSet sqlConn.Open() sqlCom.CommandType = CommandType.StoredProcedure sqlCom.Connection = sqlConn sqlCom.CommandText = "SendMailMessage" sqlCom.Parameters.Add(New SqlParameter("@MailServer", m_MailServer)) sqlCom.Parameters.Add(New SqlParameter("@From", m_MailFrom)) sqlCom.Parameters.Add(New SqlParameter("@AuthMethod", m_MailServerAuthMethod.ToString())) sqlCom.Parameters.Add(New SqlParameter("@Username", m_MailServerAuthUser)) sqlCom.Parameters.Add(New SqlParameter("@Password", m_MailServerAuthPassword)) sqlCom.Parameters.Add(New SqlParameter("@To", receiver)) sqlCom.Parameters.Add(New SqlParameter("@Subject", subject)) sqlCom.Parameters.Add(New SqlParameter("@Body", message)) da.SelectCommand = sqlCom da.Fill(ds) sqlConn.Close() If ds.Tables.Count > 0 Then If ds.Tables(0).Rows.Count > 0 Then Throw New Exception("Mail konnte nicht versendet werden. " + ds.Tables(0).Rows(0)(0).ToString() + " " + ds.Tables(0).Rows(0)(1).ToString() + " " + ds.Tables(0).Rows(0)(2).ToString()) End If End If Catch ex As Exception If sqlConn.State = ConnectionState.Open Then sqlConn.Close() End If Throw ex End Try End Sub #End Region #Region "Properties" Public ReadOnly Property StartJobInterval() As Integer Get Return m_StartJobInterval End Get End Property Public ReadOnly Property WatchJobInterval() As Integer Get Return m_WatchJobInterval End Get End Property Public ReadOnly Property MaximalStartDuration() As Integer Get Return m_MaximalStartDuration End Get End Property Public ReadOnly Property DSN() As String Get Return m_DSN End Get End Property Public ReadOnly Property EventLogName() As String Get Return m_EventLogName End Get End Property Public ReadOnly Property MailServer() As String Get Return m_EventLogName End Get End Property Public ReadOnly Property MailFrom() As String Get Return m_EventLogName End Get End Property Public ReadOnly Property MaxStarterNotifications() As String Get Return m_MaxStarterNotifications End Get End Property Public ReadOnly Property MaxWatcherNotifications() As String Get Return m_MaxWatcherNotifications End Get End Property #End Region #Region "Enums" Enum JournalEntryType Information = 1 Warning = 2 [Error] = 3 End Enum Enum JobType WatchJob = 1 StartJob = 2 End Enum Enum JobStartType Executable = 1 WindowsService = 2 SqlQuery = 3 End Enum Enum StartParameterType Username = 1 Password = 2 ServerName = 3 FilePath = 4 ServiceName = 5 ConnectionString = 6 SQLQuery = 7 End Enum Enum LastRun Start = 1 [End] = 2 End Enum Enum NotificationType Mail = 1 File = 2 End Enum Enum LogTarget SystemEventLog = 1 Database = 2 End Enum Enum MailServerAuthMethod cdoAnonymous = 0 cdoBasic = 1 End Enum #End Region End Class