You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.4 KiB

#Region "Includes"
Imports System.Data.SqlClient
Imports Common.Common
Imports System.Runtime.InteropServices
#End Region
'Class used 4 common Logging
<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class Logging
#Region "Members"
Dim m_JournalId As Integer
Dim m_JobId As Integer
Dim m_ProgrammId As Integer
Dim m_DbConnection As SqlConnection
Dim m_Common As Common.Common
Dim m_JobTyp As JobType
Dim m_IsNachLetzterAusfuerung As Boolean
Dim m_RunJob As Boolean
#End Region
#Region "Constructor"
'Gets a new JournalId for a new logging instance
'TODO: jobtype muss nicht bekannt sein, wenn ein tool als start UND watch läuft...
'Wird aber benötigt, um LastRunEnde auf Job zu setzen. Dies wiederum wird in GetFailedStartJobs
'verwendet um zu überprüfen, ob der JOB(und ebe nicht das programm) gestartet wurde
Public Sub New(ByVal programId As Integer, ByVal jobType As JobType)
Try
Dim sqlCom As New SqlCommand
Dim da As New SqlDataAdapter
Dim ds As New DataSet
m_ProgrammId = programId
m_Common = New Common.Common
m_DbConnection = New SqlConnection(m_Common.DSN)
m_DbConnection.Open()
sqlCom.CommandType = CommandType.StoredProcedure
sqlCom.Connection = m_DbConnection
sqlCom.CommandText = "CreateJournal"
sqlCom.Parameters.Add(New SqlParameter("@ProgrammId", m_ProgrammId))
sqlCom.Parameters.Add(New SqlParameter("@JobTypId", jobType))
da.SelectCommand = sqlCom
da.Fill(ds)
'One table and one row are required
If ds.Tables.Count > 0 Then
If ds.Tables(0).Rows.Count > 0 Then
m_JournalId = ds.Tables(0).Rows(0).Item("JournalId")
m_JobId = ds.Tables(0).Rows(0).Item("JobId")
m_IsNachLetzterAusfuerung = ds.Tables(0).Rows(0).Item("NachLetzterAusfuerung")
m_RunJob = ds.Tables(0).Rows(0)("RunJob")
Else
Throw New Exception("Neues Journal konnte nicht erzeugt werden oder zugehöriger Job wurde nicht gefunden")
End If
Else
Throw New Exception("Neues Journal konnte nicht erzeugt werden oder zugehöriger Job wurde nicht gefunden")
End If
If jobType = jobType.WatchJob Then
'needed for validating correct start and maximal durations
DataAccess.Job.SetJobLastRun(m_Common.DSN, m_JobId, LastRun.Start)
m_Common.Log(Common.Common.DLL_DISPLAY_NAME, "DLL Library setted LastRunStart to " + DateTime.Now.ToString("G") + " for JobId " + m_JobId.ToString)
End If
m_DbConnection.Close()
Catch ex As Exception
If m_DbConnection.State = ConnectionState.Open Then
m_DbConnection.Close()
End If
WriteEventLog(ex.Source, ex.Message, EventLogEntryType.Error)
End Try
End Sub
#End Region
#Region "Public Methods"
'Note: This Sub is necessary, because VB cannot handle constructors with parameters
'so we have to do all the "constuctor" stuff in a method...
'Public Sub InitLogging(ByVal programId As Integer, ByVal jobTyp As Integer)
'End Sub
'Writes a message with a journalEntryType to the journal
Public Sub Log(ByVal message As String, ByVal journalEintragTyp As JournalEntryType)
Try
WriteJournalEntry(message, CType(journalEintragTyp, JournalEntryType))
'in an error case, service should do something
If journalEintragTyp = JournalEntryType.Error Then
Dim ds As New DataSet
DataAccess.Job.GetNotifications(m_Common, m_ProgrammId, ds)
DataAccess.Job.SendNotification(m_Common, m_ProgrammId, ds, message, m_JobId, JobType.WatchJob)
End If
Catch ex As Exception
WriteEventLog(ex.Source, ex.Message, EventLogEntryType.Error)
End Try
End Sub
'Writes a message to the journal
Public Sub Log(ByVal message As String)
Try
Log(message, JournalEntryType.Information)
Catch ex As Exception
WriteEventLog(ex.Source, ex.Message, EventLogEntryType.Error)
End Try
End Sub
'Writes to journal, that the program has started
Public Sub Start()
Try
DataAccess.Job.SetIsRunning(m_Common.DSN, m_ProgrammId, True)
WriteJournalEntry("Start", JournalEntryType.Information)
Catch ex As Exception
DataAccess.Job.SetIsRunning(m_Common.DSN, m_ProgrammId, False)
WriteEventLog(ex.Source, ex.Message, EventLogEntryType.Error)
End Try
End Sub
'Writes to journal, that the program has ended
Public Sub Ende()
Try
DataAccess.Job.SetIsRunning(m_Common.DSN, m_ProgrammId, False)
DataAccess.Job.SetJobLastRun(m_Common.DSN, m_JobId, LastRun.End)
'calc next start time JUST if its a "NachLetzterAusfuerung" Job
If m_IsNachLetzterAusfuerung Then
DataAccess.Job.SetNextExecDateTime(m_Common.DSN, m_JobId)
End If
WriteJournalEntry("Ende", JournalEntryType.Information)
Catch ex As Exception
WriteEventLog(ex.Source, ex.Message, EventLogEntryType.Error)
End Try
End Sub
#End Region
#Region "Private Methods"
'Writes an nice formatted error message to windows eventlog
Private Sub WriteEventLog(ByVal source As String, ByVal errorMessage As String, ByVal eventLogType As EventLogEntryType)
m_Common.Log(source, "Quelle: " & source & Environment.NewLine & "Meldung: " & errorMessage, eventLogType)
End Sub
'Writes an entry to the BMS journal
Private Sub WriteJournalEntry(ByVal message As String, ByVal journalEntryType As JournalEntryType)
Try
Dim sqlCom As New SqlCommand
m_DbConnection.Open()
sqlCom.CommandType = CommandType.StoredProcedure
sqlCom.Connection = m_DbConnection
sqlCom.CommandText = "CreateJournalEntry"
sqlCom.Parameters.Add(New SqlParameter("@JournalId", m_JournalId))
sqlCom.Parameters.Add(New SqlParameter("@EintragDesc", message))
sqlCom.Parameters.Add(New SqlParameter("@JournalEintragTypId", CInt(journalEntryType)))
sqlCom.ExecuteNonQuery()
m_DbConnection.Close()
Catch
If m_DbConnection.State = ConnectionState.Open Then
m_DbConnection.Close()
End If
Throw
End Try
End Sub
#End Region
End Class