-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM_clsLogSaver_Database.def
236 lines (196 loc) · 6.76 KB
/
M_clsLogSaver_Database.def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'---------------------------------------------------------------------------------------
' Module : clsLogSaver_Database
' Author : K.Gundermann
' Date : 17.01.2012
' Purpose : Saves Logger Events to a Database
' The Database should have two Tables:
' - LogSessions ( LogS_ID, LogS_ComputerName, LogS_OSVersion, LogS_OSUserName, LogS_ApplicationUserName,
' LogS_PrgVersion, LogS_SessionStartTime, LogS_LoginTime, LogS_LogoutTime )
' - LogEvents ( Log_ID, Log_FK_SessionID, Log_Timestamp, Log_Category, Log_Module, Log_Procedure
' , Log_EventType, , Log_Severity, Log_MessageNumber, Log_MessageText, Log_MessageDisplayed
' , Log_AdditionalInfo )
'---------------------------------------------------------------------------------------
Option Compare Database
Option Explicit
Implements ILogSaver
Private Const DEFAULT_TABLENAME_EVENTS = "LogEvents"
Private Const DEFAULT_TABLENAME_SESSIONS = "LogSessions"
Private c_TablenameEvents As String
Private c_TablenameSessions As String
Private c_SessionID As Long
Private WithEvents c_Logger As clsLogger
Attribute c_Logger.VB_VarHelpID = -1
Private WithEvents c_Session As clsLogSession
Attribute c_Session.VB_VarHelpID = -1
Private c_Filters As clsLogFilterCollection
Private c_Name As String
Private Sub Class_Initialize()
Set c_Session = Logger.Log.Session
Set c_Filters = New clsLogFilterCollection
Call StartSession
End Sub
Private Sub Class_Terminate()
Call StopLog
End Sub
' --- Public Interface ---
Public Sub StartLog()
Set c_Logger = Logger.Log
End Sub
Public Sub StopLog()
Set c_Logger = Nothing
End Sub
Public Sub SaveToLog(ByRef objLogEntry As clsLogEntry)
If Filters.MatchFilter(objLogEntry) Then
Call SaveToMyLog(objLogEntry)
End If
End Sub
Public Function AddFilter(ByVal TheFilter As clsLogFilter) As ILogSaver
Filters.AddFilter TheFilter
Set AddFilter = Me
End Function
Public Property Get ID() As Long
ID = ObjPtr(Me)
End Property
Public Property Get Name() As String
If c_Name = vbNullString Then
Name = TypeName(Me)
Else
Name = c_Name
End If
End Property
Public Property Let Name(ByVal TheName As String)
c_Name = TheName
End Property
' --- Private Functions ---
Private Property Get Filters() As clsLogFilterCollection
Set Filters = c_Filters
End Property
' --- Event Handler for Logger/Session Class ---
Private Sub c_Logger_SaveToLog(objLogEntry As clsLogEntry)
Call Me.SaveToLog(objLogEntry)
End Sub
Private Sub c_Session_Login()
' ToDo: We have now a logged In User
Call SessionLogin
End Sub
Private Sub c_Session_Logout()
' ToDo: The user has logged out
Call SessionLogout
End Sub
' --- Implements ILogSaver ---
Private Sub ILogSaver_StartLog()
Me.StartLog
End Sub
Private Sub ILogSaver_StopLog()
Me.StopLog
End Sub
Private Function ILogSaver_AddFilter(ByVal TheFilter As clsLogFilter) As ILogSaver
Set ILogSaver_AddFilter = Me.AddFilter(TheFilter)
End Function
Private Property Get ILogSaver_ID() As Long
ILogSaver_ID = Me.ID
End Property
Private Property Get ILogSaver_Name() As String
ILogSaver_Name = Me.Name
End Property
Private Property Let ILogSaver_Name(ByVal TheName As String)
Me.Name = TheName
End Property
Private Sub ILogSaver_SaveToLog(objLogEntry As clsLogEntry)
Call Me.SaveToLog(objLogEntry)
End Sub
' ---------------------------------------------------------------------------------
' -- Specific Functions for that logger here
Public Property Get TablenameEvents() As String
If IsNullString(c_TablenameEvents) Then
TablenameEvents = DEFAULT_TABLENAME_EVENTS
Else
TablenameEvents = c_TablenameEvents
End If
End Property
Public Property Let TablenameEvents(ByVal TheValue As String)
c_TablenameEvents = TheValue
End Property
Public Property Get TablenameSessions() As String
If IsNullString(c_TablenameSessions) Then
TablenameSessions = DEFAULT_TABLENAME_SESSIONS
Else
TablenameSessions = c_TablenameSessions
End If
End Property
Public Property Let TablenameSessions(ByVal TheValue As String)
c_TablenameSessions = TheValue
End Property
Public Property Get DBSessionID() As Long
DBSessionID = c_SessionID
End Property
Private Sub StartSession()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset(Me.TablenameSessions, dbOpenDynaset, dbAppendOnly + dbSeeChanges)
With rs
.AddNew
!LogS_Computername = c_Session.HostName
!LogS_OSVersion = c_Session.OSVersion
!LogS_OSUserName = c_Session.OSUserName
!LogS_ApplicationUserName = c_Session.ApplicationUserName
!LogS_SessionStartTime = c_Session.SessionStartTime
' !LogS_LoginTime = c_Session.LoginTime
' !LogS_PrgVersion = Utils.Prg.PrgVersion
.Update
.Close
End With
Set rs = CurrentDb.OpenRecordset("SELECT @@IDENTITY")
c_SessionID = rs.Fields(0)
rs.Close
Set rs = Nothing
End Sub
Private Sub SessionLogin()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM " & Me.TablenameSessions & " WHERE LogS_ID=" & c_SessionID, dbOpenDynaset, dbSeeChanges)
With rs
If Not .EOF Then
.Edit
!LogS_ApplicationUserName = c_Session.ApplicationUserName
!LogS_LoginTime = c_Session.LoginTime
.Update
End If
.Close
End With
End Sub
Private Sub SessionLogout()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM " & Me.TablenameSessions & " WHERE LogS_ID=" & c_SessionID, dbOpenDynaset, dbSeeChanges)
With rs
If Not .EOF Then
.Edit
!LogS_LogoutTime = Now ' ?? c_Session.LogoutTime
.Update
End If
.Close
End With
End Sub
Private Sub SaveToMyLog(ByRef objLogEntry As clsLogEntry)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset(Me.TablenameEvents, dbOpenDynaset, dbAppendOnly + dbSeeChanges)
With rs
.AddNew
!Log_FK_SessionId = c_SessionID
!Log_Timestamp = objLogEntry.TimeStamp.ToDateWithTime ' Keine ms bei SQL2005 !!!
!Log_Category = objLogEntry.Category
!Log_Module = objLogEntry.Module
!Log_Procedure = objLogEntry.Procedure
!Log_EventType = objLogEntry.EventType
!Log_Severity = objLogEntry.Severity
!Log_MessageNumber = objLogEntry.MessageNumber
!Log_MessageText = objLogEntry.MessageText
!Log_MessageDisplayed = objLogEntry.MessageDisplayed
!Log_AdditionalInfo = objLogEntry.AdditionalInfo
.Update
.Close
End With
Set rs = Nothing
End Sub