public async Task TestAddLogRecord() { var utcnow = DateTime.UtcNow.Date; var mysqlLogStore = new MySqlLogStore(() => utcnow); const string appPath = "###rather_not_existing_application_path###"; var hash = BitConverter.ToString(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(appPath))).Replace("-", String.Empty); var logrec = new LogRecord { LoggerName = "TestLogger", ApplicationPath = appPath, LogLevel = LogRecord.ELogLevel.Error, TimeUtc = DateTime.UtcNow, ProcessId = 123, ThreadId = 456, Server = "TestServer", Identity = "TestIdentity", CorrelationId = Guid.NewGuid().ToString(), Message = "Test log message to store in the log", ExceptionMessage = "Test exception log message", ExceptionType = "TestException", ExceptionAdditionalInfo = "Additinal info for the test exception", AdditionalFields = new Dictionary <String, Object> { { "Host", "testhost.com" }, { "LoggedUser", "testloggeduser" }, { "HttpStatusCode", "200.1" }, { "Url", "http://testhost.com" }, { "Referer", "http://prevtesthost.com" }, { "ClientIP", null }, { "RequestData", "test test test" }, { "ResponseData", null }, { "ServiceName", "TestService" }, { "ServiceDisplayName", "Test service generating logs" }, { "NotExisting", null } }, PerformanceData = new Dictionary <String, float> { { "CPU", 2.0f }, { "Memory", 20000000f } } }; // add log await mysqlLogStore.AddLogRecordAsync(logrec); using (var conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["mysqlconn"].ConnectionString)) { conn.Open(); // check if app tables were created var tables = conn.Query <String>("select table_name from information_schema.tables where table_schema = @db", new { db = conn.Database }).ToArray(); Assert.Contains(MySqlLogStore.AppLogTablePrefix + hash, tables, StringComparer.OrdinalIgnoreCase); // check partitions var expectedPartitionNames = new[] { String.Format("{0}{1:yyyyMMdd}", Partition.PartitionPrefix, utcnow.Date.AddDays(1)), String.Format("{0}{1:yyyyMMdd}", Partition.PartitionPrefix, utcnow.Date.AddDays(2)) }; foreach (var prefix in new[] { MySqlLogStore.AppLogTablePrefix }) { var partitions = conn.Query <String>("select partition_name From information_schema.partitions where table_name = @tableName and table_schema = @db order by partition_name", new { tableName = prefix + hash, db = conn.Database }); Assert.Equal(expectedPartitionNames, partitions); } // check logs content var dbLogRecs = conn.Query <DbAppLogRecord>("select * from " + MySqlLogStore.AppLogTablePrefix + hash).ToArray(); Assert.True(dbLogRecs.Length == 1); var dbLogRec = dbLogRecs[0]; Assert.Equal(logrec.LoggerName, dbLogRec.LoggerName); Assert.Equal(logrec.ApplicationPath, dbLogRec.ApplicationPath); Assert.Equal(logrec.LogLevel, dbLogRec.LogLevel); Assert.Equal(logrec.TimeUtc.ToShortDateString(), dbLogRec.TimeUtc.ToShortDateString()); Assert.Equal(logrec.ProcessId, dbLogRec.ProcessId); Assert.Equal(logrec.ThreadId, dbLogRec.ThreadId); Assert.Equal(logrec.Server, dbLogRec.Server); Assert.Equal(logrec.Identity, dbLogRec.Identity); Assert.Equal(logrec.CorrelationId, dbLogRec.CorrelationId); Assert.Equal(logrec.Message, dbLogRec.Message); Assert.Equal(logrec.ExceptionMessage, dbLogRec.ExceptionMessage); Assert.Equal(logrec.ExceptionType, dbLogRec.ExceptionType); Assert.Equal(logrec.ExceptionAdditionalInfo, dbLogRec.ExceptionAdditionalInfo); Assert.Equal((String)logrec.AdditionalFields["Host"], dbLogRec.Host); Assert.Equal((String)logrec.AdditionalFields["LoggedUser"], dbLogRec.LoggedUser); Assert.Equal((String)logrec.AdditionalFields["HttpStatusCode"], dbLogRec.HttpStatusCode); Assert.Equal((String)logrec.AdditionalFields["Url"], dbLogRec.Url); Assert.Equal((String)logrec.AdditionalFields["Referer"], dbLogRec.Referer); Assert.Equal((String)logrec.AdditionalFields["ClientIP"], dbLogRec.ClientIP); Assert.Equal((String)logrec.AdditionalFields["RequestData"], dbLogRec.RequestData); Assert.Equal((String)logrec.AdditionalFields["ResponseData"], dbLogRec.ResponseData); Assert.Equal((String)logrec.AdditionalFields["ServiceName"], dbLogRec.ServiceName); Assert.Equal((String)logrec.AdditionalFields["ServiceDisplayName"], dbLogRec.ServiceDisplayName); var dbPerfLogs = JsonConvert.DeserializeObject <IDictionary <String, float> >(dbLogRec.PerfData); Assert.True(dbPerfLogs.Count == 2); float r; Assert.True(dbPerfLogs.TryGetValue("CPU", out r)); Assert.Equal(r, logrec.PerformanceData["CPU"]); Assert.True(dbPerfLogs.TryGetValue("Memory", out r)); Assert.Equal(r, logrec.PerformanceData["Memory"]); } }
public async Task LogFilteringTest() { // add a test log record var utcnow = DateTime.UtcNow.Date; var mysqlLogStore = new MySqlLogStore(() => utcnow); const string appPath = "###rather_not_existing_application_path2###"; var logrec = new LogRecord { LoggerName = "TestLogger", ApplicationPath = appPath, LogLevel = LogRecord.ELogLevel.Error, TimeUtc = DateTime.UtcNow, ProcessId = 123, ThreadId = 456, Server = "TestServer", Identity = "TestIdentity", CorrelationId = Guid.NewGuid().ToString(), Message = "Test log message to store in the log", ExceptionMessage = "Test exception log message", ExceptionType = "TestException", ExceptionAdditionalInfo = "Additinal info for the test exception", AdditionalFields = new Dictionary <String, Object> { { "Host", "testhost.com" }, { "LoggedUser", "testloggeduser" }, { "HttpStatusCode", "200.1" }, { "Url", "http://testhost.com" }, { "Referer", "http://prevtesthost.com" }, { "ClientIP", null }, { "RequestData", "test test test" }, { "ResponseData", null }, { "ServiceName", "TestService" }, { "ServiceDisplayName", "Test service generating logs" }, { "NotExisting", null } }, PerformanceData = new Dictionary <String, float> { { "CPU", 2.0f }, { "Memory", 20000000f } } }; // add log await mysqlLogStore.AddLogRecordAsync(logrec); var searchResults = await mysqlLogStore.FilterLogsAsync(new LogSearchCriteria { FromUtc = DateTime.UtcNow.AddMinutes(-10), ToUtc = DateTime.UtcNow.AddMinutes(10), ApplicationPath = appPath, Levels = new[] { LogRecord.ELogLevel.Error, LogRecord.ELogLevel.Info }, Limit = 10, Offset = 0, Server = "TestServer", Keywords = new KeywordsParsed() { Url = "http://testhost.com" } }); Assert.NotNull(searchResults.FoundItems); var foundItems = searchResults.FoundItems.ToArray(); Assert.True(foundItems.Length == 1); var logrec2 = foundItems[0]; Assert.Equal(logrec.LoggerName, logrec2.LoggerName); Assert.Equal(logrec.ApplicationPath, logrec2.ApplicationPath); Assert.Equal(logrec.LogLevel, logrec2.LogLevel); Assert.Equal(logrec.TimeUtc.ToShortDateString(), logrec2.TimeUtc.ToShortDateString()); Assert.Equal(logrec.ProcessId, logrec2.ProcessId); Assert.Equal(logrec.ThreadId, logrec2.ThreadId); Assert.Equal(logrec.Server, logrec2.Server); Assert.Equal(logrec.Identity, logrec2.Identity); Assert.Equal(logrec.CorrelationId, logrec2.CorrelationId); Assert.Equal(logrec.Message, logrec2.Message); Assert.Equal(logrec.ExceptionMessage, logrec2.ExceptionMessage); Assert.Equal(logrec.ExceptionType, logrec2.ExceptionType); Assert.Equal(logrec.ExceptionAdditionalInfo, logrec2.ExceptionAdditionalInfo); Assert.Equal(logrec.AdditionalFields["Host"], logrec2.AdditionalFields["Host"]); Assert.Equal(logrec.AdditionalFields["LoggedUser"], logrec2.AdditionalFields["LoggedUser"]); Assert.Equal(logrec.AdditionalFields["HttpStatusCode"], logrec2.AdditionalFields["HttpStatusCode"]); Assert.Equal(logrec.AdditionalFields["Url"], logrec2.AdditionalFields["Url"]); Assert.Equal(logrec.AdditionalFields["Referer"], logrec2.AdditionalFields["Referer"]); Assert.Equal(logrec.AdditionalFields["RequestData"], logrec2.AdditionalFields["RequestData"]); Assert.Equal(logrec.AdditionalFields["ServiceName"], logrec2.AdditionalFields["ServiceName"]); Assert.Equal(logrec.AdditionalFields["ServiceDisplayName"], logrec2.AdditionalFields["ServiceDisplayName"]); }