public TestMainDatabaseFactory() { try { var logger = new DebugLoggerProvider(); try { this.logger = new LoggerFactory(new[] { logger }); } catch { logger.Dispose(); throw; } this.connection = new SqliteConnection("DataSource=:memory:"); this.connection.Open(); } catch { this.connection?.Dispose(); this.logger?.Dispose(); throw; } }
public void ProjectionExpressionTest() { MemoryCacheProvider.ClearCache(); var loggerProvider = new DebugLoggerProvider(); var loggerFactory = new LoggerFactory(new[] { loggerProvider }); var options = new DbContextOptionsBuilder <BloggingContext>() .UseLoggerFactory(loggerFactory) .UseInMemoryDatabase(databaseName: "ProjectionExpressionTest") .Options; // create test entries using (var initContext = new BloggingContext(options)) { initContext.Blogs.Add(new Blog { BlogId = 1, Url = "http://sample.com/cats" }); initContext.Blogs.Add(new Blog { BlogId = 2, Url = "http://sample.com/catfish" }); initContext.Blogs.Add(new Blog { BlogId = 3, Url = "http://sample.com/dogs" }); initContext.SaveChanges(); } using (var projectionContext = new BloggingContext(options)) { // shoud not hit cache, because first execution var result = projectionContext.Blogs .Where(d => d.BlogId > 1) .Select(d => new { d.BlogId, d.Rating }) .Cacheable(TimeSpan.FromMinutes(5)) .ToList(); // shoud hit cache, because second execution var cachedResult = projectionContext.Blogs .Where(d => d.BlogId > 1) .Select(d => new { d.BlogId, d.Rating }) .Cacheable(TimeSpan.FromMinutes(5)) .ToList(); Assert.AreEqual(result.Count, cachedResult.Count); } // find "cache hit" log entries var logs = loggerProvider.Entries.Where(e => e.EventId == CacheableEventId.CacheHit); // cache should hit one time Assert.IsTrue(logs.Count() == 1); }
public void GlobalQueryFilterTest() { MemoryCacheProvider.ClearCache(); var loggerProvider = new DebugLoggerProvider(); var loggerFactory = new LoggerFactory(new[] { loggerProvider }); var options = new DbContextOptionsBuilder <AgnosticBloggingContext>() .UseLoggerFactory(loggerFactory) .UseInMemoryDatabase(databaseName: "GlobalQueryFilterTest") .UseSecondLevelCache() .Options; // create test entries using (var initContext = new AgnosticBloggingContext(options)) { initContext.Blogs.Add(new Blog { BlogId = 1, Url = "http://sample.com/cats" }); initContext.Blogs.Add(new Blog { BlogId = 2, Url = "http://sample.com/catfish" }); initContext.Blogs.Add(new Blog { BlogId = 3, Url = "http://sample.com/dogs" }); initContext.SaveChanges(); } using (var constantContext = new AgnosticBloggingContext(options, minBlogId: 2)) { // shoud not hit cache, because no Cacheable call var rawResult = constantContext.Blogs .Count(); // shoud not hit cache, because first execution var result = constantContext.Blogs .Cacheable(TimeSpan.FromMinutes(5)) .Count(); // shoud hit cache, because second execution var cachedResult = constantContext.Blogs .Cacheable(TimeSpan.FromMinutes(5)) .Count(); Assert.AreEqual(result, cachedResult); } // find "cache hit" log entries var logs = loggerProvider.Entries.Where(e => e.EventId == CacheableEventId.CacheHit); // cache should hit one time Assert.IsTrue(logs.Count() == 1); }
static void Main() { using var provider = new DebugLoggerProvider(); var logger = provider.CreateLogger("LoggingExample"); // Approach #1: Extension method on ILogger logger.CouldNotOpenSocket("microsoft.com"); // Approach #2: wrapper type around ILogger var d = logger.Wrap(); d.CouldNotOpenSocket("microsoft.com"); }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=(LocalDB)\BABUSQL;Database=Customer;Trusted_Connection=True;"); LoggerFactory program_logger = new LoggerFactory(); // Console logger: Logs to Console ConsoleLoggerOptions console_options = new ConsoleLoggerOptions(); console_options.DisableColors = false; console_options.IncludeScopes = false; ConsoleLoggerProvider console_logger = new ConsoleLoggerProvider((category, level) => level >= LogLevel.Trace, true, false); program_logger.AddProvider(console_logger); DebugLoggerProvider debug_logger = new DebugLoggerProvider(); program_logger.AddProvider(debug_logger); optionsBuilder.UseLoggerFactory(program_logger).EnableSensitiveDataLogging(); }
public FakeDbContextFactory() { try { var logger = new DebugLoggerProvider(); try { this.logger = new LoggerFactory(new[] { logger }); } catch { logger.Dispose(); throw; } this.connection = new SqliteConnection("DataSource=:memory:"); } catch { this.logger?.Dispose(); throw; } }
public void PerformanceTest() { MemoryCacheProvider.ClearCache(); decimal loopCount = 1000; var loggerProvider = new DebugLoggerProvider(); var loggerFactory = new LoggerFactory(new[] { loggerProvider }); var options = new DbContextOptionsBuilder <AgnosticBloggingContext>() .UseLoggerFactory(loggerFactory) .UseInMemoryDatabase(databaseName: "PerformanceTest") .UseSecondLevelCache() .Options; // create test entries using (var initContext = new AgnosticBloggingContext(options)) { initContext.ChangeTracker.AutoDetectChangesEnabled = false; for (int i = 0; i < 100000; i++) { initContext.Blogs.Add(new Blog { Url = $"http://sample.com/cat{i}", Posts = new List <Post> { { new Post { Title = $"Post{1}" } } } }); } initContext.SaveChanges(); } var rawOptions = new DbContextOptionsBuilder <AgnosticBloggingContext>() .UseLoggerFactory(loggerFactory) .UseInMemoryDatabase(databaseName: "PerformanceTest") .Options; using (var performanceContext = new AgnosticBloggingContext(rawOptions)) { Stopwatch watch = new Stopwatch(); watch.Start(); // raw queries for (int i = 0; i < loopCount; i++) { var result = performanceContext.Blogs .Where(d => d.BlogId >= 0) .Take(100) .ToList(); } var rawTimeSpan = watch.Elapsed; Debug.WriteLine($"Average default context database query duration [+{TimeSpan.FromTicks((long)(rawTimeSpan.Ticks / loopCount))}]."); } using (var performanceContext = new AgnosticBloggingContext(options)) { Stopwatch watch = new Stopwatch(); watch.Start(); // uncached queries for (int i = 0; i < loopCount; i++) { var result = performanceContext.Blogs .Where(d => d.BlogId >= 0) .Take(100) .ToList(); } var uncachedTimeSpan = watch.Elapsed; // caching query result performanceContext.Blogs .Where(d => d.BlogId >= 0) .Cacheable(TimeSpan.FromMinutes(10)) .Take(100) .ToList(); watch.Restart(); // cached queries for (int i = 0; i < loopCount; i++) { var result = performanceContext.Blogs .Where(d => d.BlogId >= 0) .Cacheable(TimeSpan.FromMinutes(10)) .Take(100) .ToList(); } var cachedTimeSpan = watch.Elapsed; // find log entries var queryResultsCachedCount = loggerProvider.Entries.Where(e => e.EventId == CacheableEventId.QueryResultCached).Count(); var cacheHitsCount = loggerProvider.Entries.Where(e => e.EventId == CacheableEventId.CacheHit).Count(); // check cache event counts Assert.IsTrue(queryResultsCachedCount == 1); Assert.IsTrue(cacheHitsCount == loopCount); Debug.WriteLine($"Average database query duration [+{TimeSpan.FromTicks((long)(uncachedTimeSpan.Ticks / loopCount))}]."); Debug.WriteLine($"Average cache query duration [+{TimeSpan.FromTicks((long)(cachedTimeSpan.Ticks / loopCount))}]."); Debug.WriteLine($"Cached queries are x{((Decimal)uncachedTimeSpan.Ticks / (Decimal)cachedTimeSpan.Ticks) - 1:N2} times faster."); Assert.IsTrue(cachedTimeSpan < uncachedTimeSpan); } }
/// <summary> /// Testing cache expiration functionality. /// </summary> //[TestMethod] public void ExpirationTest() { MemoryCacheProvider.ClearCache(); var loggerProvider = new DebugLoggerProvider(); var loggerFactory = new LoggerFactory(new[] { loggerProvider }); var options = new DbContextOptionsBuilder <AgnosticBloggingContext>() .UseLoggerFactory(loggerFactory) .UseInMemoryDatabase(databaseName: "ExpirationTest") .UseSecondLevelCache() .Options; // create test entries using (var initContext = new AgnosticBloggingContext(options)) { initContext.Blogs.Add(new Blog { BlogId = 1, Url = "http://sample.com/cats" }); initContext.Blogs.Add(new Blog { BlogId = 2, Url = "http://sample.com/catfish" }); initContext.Blogs.Add(new Blog { BlogId = 3, Url = "http://sample.com/dogs" }); initContext.SaveChanges(); } using (var expirationContext = new AgnosticBloggingContext(options)) { // shoud not hit cache, because first execution var result = expirationContext.Blogs .Where(d => d.BlogId == 1) .Cacheable(TimeSpan.FromSeconds(5)) .ToList(); // shoud hit cache, because second execution result = expirationContext.Blogs .Where(d => d.BlogId == 1) .Cacheable(TimeSpan.FromSeconds(5)) .ToList(); // shoud not hit cache, because different parameter result = expirationContext.Blogs .Where(d => d.BlogId == 2) .Cacheable(TimeSpan.FromSeconds(5)) .ToList(); Thread.Sleep(TimeSpan.FromSeconds(10)); // shoud not hit cache, because expiration result = expirationContext.Blogs .Where(d => d.BlogId == 1) .Cacheable(TimeSpan.FromSeconds(5)) .ToList(); } // find "cache hit" log entries var logs = loggerProvider.Entries.Where(e => e.EventId == CacheableEventId.CacheHit); // cache should hit one time Assert.IsTrue(logs.Count() == 1); }