diff --git a/Libs/CxAuditTrailsCrawler/AuditTrailCrawler.cs b/Libs/CxAuditTrailsCrawler/AuditTrailCrawler.cs index 51a212f5..7f3833e8 100644 --- a/Libs/CxAuditTrailsCrawler/AuditTrailCrawler.cs +++ b/Libs/CxAuditTrailsCrawler/AuditTrailCrawler.cs @@ -115,7 +115,7 @@ public static void CrawlAuditTrails (CancellationToken token) if (GetPropertyValue(field.Name, crawlInvoker._suppressions)) { - _log.Debug($"{field.Name} logging has been suppressed via configuration."); + _log.Info($"{field.Name} logging has been suppressed via configuration."); return; } diff --git a/Libs/CxAuditTrailsDB/DbAccess.cs b/Libs/CxAuditTrailsDB/DbAccess.cs index b7e345ef..481fae38 100644 --- a/Libs/CxAuditTrailsDB/DbAccess.cs +++ b/Libs/CxAuditTrailsDB/DbAccess.cs @@ -12,6 +12,40 @@ public class DbAccess private static CxAuditDBConnection ConConfig => Configuration.Impls.Config.GetConfig(); + + public class FetchResults : IDisposable + { + private FetchResults() { } + + private SqlConnection _con; + public SqlDataReader DataReader { get; private set; } + + internal FetchResults (SqlConnection con, SqlDataReader reader) + { + _con = con; + DataReader = reader; + } + + + public void Dispose() + { + if (DataReader != null) + { + DataReader.Close(); + DataReader.Dispose(); + DataReader = null; + } + + if (_con != null) + { + _con.Close(); + _con.Dispose(); + _con = null; + } + } + } + + public bool IsDisabled { get @@ -57,7 +91,7 @@ FROM sys.tables t return retVal; } - internal SqlDataReader FetchRecords (String db, String schema, String table, String cmdText, DateTime since) + internal FetchResults FetchRecords (String db, String schema, String table, String cmdText, DateTime since) { _log.Trace($"FetchRecords: {db}.{schema}.{table} > {since}"); @@ -85,10 +119,10 @@ internal SqlDataReader FetchRecords (String db, String schema, String table, Str _log.Debug($"{db}.{schema}.{table} query has rows [{reader.HasRows}]"); - return reader; + return new FetchResults (con, reader); } - public SqlDataReader FetchRecords_CxDB_accesscontrol_AuditTrail(DateTime since) + public FetchResults FetchRecords_CxDB_accesscontrol_AuditTrail(DateTime since) { String query = @"SELECT [Id] @@ -104,7 +138,7 @@ FROM [CxDB].[accesscontrol].[AuditTrail] return FetchRecords("CxDB", "accesscontrol", "AuditTrail", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_AuditTrail(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_AuditTrail(DateTime since) { String query = @"SELECT ActTyp.Name as [Action] @@ -125,7 +159,7 @@ FROM CxActivity.dbo.AuditTrail as AudTrl return FetchRecords("CxActivity", "dbo", "AuditTrail", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_DataRetention(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_DataRetention(DateTime since) { String query = @"SELECT [Id] @@ -140,7 +174,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_DataRetention(DateTime si return FetchRecords("CxActivity", "dbo", "Audit_DataRetention", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Logins(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_Logins(DateTime since) { String query = @"SELECT [Id] @@ -157,7 +191,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Logins(DateTime since) return FetchRecords("CxActivity", "dbo", "Audit_Logins", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Presets(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_Presets(DateTime since) { String query = @"SELECT [Id] @@ -175,7 +209,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Presets(DateTime since) return FetchRecords("CxActivity", "dbo", "Audit_Presets", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Projects(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_Projects(DateTime since) { String query = @"SELECT [Id] @@ -192,7 +226,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Projects(DateTime since) return FetchRecords("CxActivity", "dbo", "Audit_Projects", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Queries(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_Queries(DateTime since) { String query = @"SELECT [Id] @@ -226,7 +260,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Queries(DateTime since) return FetchRecords("CxActivity", "dbo", "Audit_Queries", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_QueriesActions(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_QueriesActions(DateTime since) { String query = @"SELECT [Id] @@ -243,7 +277,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_QueriesActions(DateTime s return FetchRecords("CxActivity", "dbo", "Audit_QueriesActions", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Reports(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_Reports(DateTime since) { String query = @"SELECT [ID] as Id @@ -259,7 +293,7 @@ [ID] as Id return FetchRecords("CxActivity", "dbo", "Audit_Reports", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_ScanRequests(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_ScanRequests(DateTime since) { String query = @"SELECT [Id] @@ -279,7 +313,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_ScanRequests(DateTime sin return FetchRecords("CxActivity", "dbo", "Audit_ScanRequests", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Scans(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_Scans(DateTime since) { String query = @"SELECT [Id] @@ -299,7 +333,7 @@ public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Scans(DateTime since) return FetchRecords("CxActivity", "dbo", "Audit_Scans", query, since); } - public SqlDataReader FetchRecords_CxActivity_dbo_Audit_Users(DateTime since) + public FetchResults FetchRecords_CxActivity_dbo_Audit_Users(DateTime since) { String query = @"SELECT [Id] diff --git a/Libs/CxAuditTrailsDirectDB/DBCrawler.cs b/Libs/CxAuditTrailsDirectDB/DBCrawler.cs index 5dd955a5..cec391ab 100644 --- a/Libs/CxAuditTrailsDirectDB/DBCrawler.cs +++ b/Libs/CxAuditTrailsDirectDB/DBCrawler.cs @@ -65,6 +65,7 @@ private void OutputRecords (SqlDataReader reader, IOutputTransaction trx, IRecor public void CxDB_accesscontrol_AuditTrail(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { + _log.Debug("BEGIN: CxDB_accesscontrol_AuditTrail"); Func detailsConverter = (val) => { var serializer = JsonSerializer.Create(); @@ -75,76 +76,100 @@ public void CxDB_accesscontrol_AuditTrail(DateTime sinceDate, IOutputTransaction }; using (var reader = _db.FetchRecords_CxDB_accesscontrol_AuditTrail(sinceDate)) - OutputRecords(reader, trx, record, new Dictionary> + OutputRecords(reader.DataReader, trx, record, new Dictionary> { { "Details", detailsConverter} }); - } + + _log.Debug("END: CxDB_accesscontrol_AuditTrail"); + } - public void CxActivity_dbo_AuditTrail(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_AuditTrail(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_AuditTrail(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_AuditTrail"); + using (var reader = _db.FetchRecords_CxActivity_dbo_AuditTrail(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_AuditTrail"); + } - public void CxActivity_dbo_Audit_DataRetention(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_DataRetention(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_DataRetention(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_DataRetention"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_DataRetention(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_DataRetention"); + } - public void CxActivity_dbo_Audit_Logins(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_Logins(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Logins(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_Logins"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Logins(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_Logins"); + } - public void CxActivity_dbo_Audit_Presets(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_Presets(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Presets(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_Presets"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Presets(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_Presets"); + } - public void CxActivity_dbo_Audit_Projects(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_Projects(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Projects(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_Projects"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Projects(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_Projects"); + } - public void CxActivity_dbo_Audit_Queries(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_Queries(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Queries(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_Queries"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Queries(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_Queries"); + } - public void CxActivity_dbo_Audit_QueriesActions(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_QueriesActions(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_QueriesActions(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_QueriesActions"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_QueriesActions(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_QueriesActions"); + } - public void CxActivity_dbo_Audit_Reports(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_Reports(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Reports(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_Reports"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Reports(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_Reports"); + } - public void CxActivity_dbo_Audit_ScanRequests(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_ScanRequests(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_ScanRequests(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_ScanRequests"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_ScanRequests(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_ScanRequests"); + } - public void CxActivity_dbo_Audit_Scans(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_Scans(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Scans(sinceDate)) - OutputRecords(reader, trx, record); - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_Scans"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Scans(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_Scans"); + } - public void CxActivity_dbo_Audit_Users(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) + public void CxActivity_dbo_Audit_Users(DateTime sinceDate, IOutputTransaction trx, IRecordRef record) { - using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Users(sinceDate)) - OutputRecords(reader, trx, record); - } - } + _log.Debug("BEGIN: CxActivity_dbo_Audit_Users"); + using (var reader = _db.FetchRecords_CxActivity_dbo_Audit_Users(sinceDate)) + OutputRecords(reader.DataReader, trx, record); + _log.Debug("END: CxActivity_dbo_Audit_Users"); + } + } }