Skip to content

Commit

Permalink
Change DisableLogging to ConfigureLogging and don't use negative …
Browse files Browse the repository at this point in the history
…names
  • Loading branch information
VahidN committed Apr 5, 2024
1 parent 5445102 commit b549903
Show file tree
Hide file tree
Showing 12 changed files with 492 additions and 480 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
public void ConfigureServices(IServiceCollection services)
{
services.AddEFSecondLevelCache(options =>
options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))

Expand Down Expand Up @@ -92,7 +92,7 @@ namespace EFSecondLevelCache.Core.AspNetCoreSample
{
const string providerName1 = "InMemory1";
services.AddEFSecondLevelCache(options =>
options.UseEasyCachingCoreProvider(providerName1, isHybridCache: false).DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseEasyCachingCoreProvider(providerName1, isHybridCache: false).ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
);
Expand Down Expand Up @@ -151,7 +151,7 @@ namespace EFSecondLevelCache.Core.AspNetCoreSample
{
const string providerName1 = "Redis1";
services.AddEFSecondLevelCache(options =>
options.UseEasyCachingCoreProvider(providerName1, isHybridCache: false).DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseEasyCachingCoreProvider(providerName1, isHybridCache: false).ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails (for example, if Redis is down).
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
);
Expand Down Expand Up @@ -205,7 +205,7 @@ services.AddEFSecondLevelCache(options =>
isHybridCache: false)
// `Or` you can set the cache key prefix per tenant dynamically
.UseCacheKeyPrefix(serviceProvider => "EF_" + serviceProvider.GetRequiredService<IHttpContextAccesor>().HttpContext.Request.Headers["tenant-id"])
.DisableLogging(true)
.ConfigureLogging(true)
.UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
Expand Down Expand Up @@ -236,7 +236,7 @@ namespace EFSecondLevelCache.Core.AspNetCoreSample
public void ConfigureServices(IServiceCollection services)
{
services.AddEFSecondLevelCache(options =>
options.UseCacheManagerCoreProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseCacheManagerCoreProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
);
Expand Down Expand Up @@ -288,7 +288,7 @@ services.AddSingleton(typeof(ICacheManagerConfiguration),
services.AddSingleton(typeof(ICacheManager<>), typeof(BaseCacheManager<>));

services.AddEFSecondLevelCache(options =>
options.UseCacheManagerCoreProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseCacheManagerCoreProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
);
Expand Down Expand Up @@ -365,7 +365,7 @@ NOTE: It doesn't matter where the `Cacheable` method is located in this expressi
Also it's possible to set the `Cacheable()` method's settings globally:

```csharp
services.AddEFSecondLevelCache(options => options.UseMemoryCacheProvider(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(5)).DisableLogging(true)
services.AddEFSecondLevelCache(options => options.UseMemoryCacheProvider(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(5)).ConfigureLogging(true)
.UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
Expand Down Expand Up @@ -397,7 +397,7 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_");
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_");
options.CacheAllQueries(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30));
// Fallback on db if the caching provider fails.
options.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1));
Expand All @@ -421,7 +421,7 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
/*.CacheQueriesContainingTypes(
Expand Down Expand Up @@ -452,7 +452,7 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
// How to skip caching specific commands
Expand All @@ -475,7 +475,7 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
// Don't cache null values. Remove this optional setting if it's not necessary.
Expand All @@ -498,7 +498,7 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
/*.CacheAllQueriesExceptContainingTypes(
Expand Down Expand Up @@ -529,7 +529,7 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider().DisableLogging(true).UseCacheKeyPrefix("EF_")
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
.SkipCacheInvalidationCommands(commandText =>
Expand Down Expand Up @@ -563,11 +563,11 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
## Does it work?!

You should enable the logging system to see the behind the scene of the caching interceptor.
First set the `DisableLogging(false)`:
First set the `ConfigureLogging(true)`:

```c#
services.AddEFSecondLevelCache(options =>
options.UseMemoryCacheProvider().DisableLogging(false).UseCacheKeyPrefix("EF_")
options.UseMemoryCacheProvider().ConfigureLogging(true).UseCacheKeyPrefix("EF_")
// Fallback on db if the caching provider fails.
.UseDbCallsIfCachingProviderIsDown(TimeSpan.FromMinutes(1))
```
Expand All @@ -587,11 +587,11 @@ And then change the log level to `Debug` in your `appsettings.json` file:
}
```

Or ... you can use the second optional parameter of the `DisableLogging` method to access the published events of this library more easily:
Or ... you can use the second optional parameter of the `ConfigureLogging` method to access the published events of this library more easily:


```c#
.DisableLogging(disable: !environment.IsDevelopment(), cacheableEvent: args =>
.ConfigureLogging(enable: environment.IsDevelopment(), cacheableEvent: args =>
{
switch (args.EventId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,19 @@ public EFCoreSecondLevelCacheOptions UseCacheKeyPrefix(string prefix)
}

/// <summary>
/// Should the debug level logging be disabled?
/// Set it to true for maximum performance.
/// Should the debug level logging be enabled?
/// Set it to false for maximum performance.
/// </summary>
/// <param name="disable">Set it to false, to enable logging</param>
/// <param name="enable">Set it to true, to enable logging</param>
/// <param name="cacheableEvent">
/// If you set DisableLogging to false, this delegate will give you the internal caching
/// If you set EnableLogging to true, this delegate will give you the internal caching
/// events of the library.
/// </param>
/// <returns></returns>
public EFCoreSecondLevelCacheOptions DisableLogging(bool disable = false,
public EFCoreSecondLevelCacheOptions ConfigureLogging(bool enable = false,
Action<(CacheableLogEventId EventId, string Message)>? cacheableEvent = null)
{
Settings.DisableLogging = disable;
Settings.EnableLogging = enable;
Settings.CacheableEvent = cacheableEvent;

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public class EFCoreSecondLevelCacheSettings
public SkipCacheSpecificQueriesOptions SkipCacheSpecificQueriesOptions { get; set; } = new(null);

/// <summary>
/// Should the debug level logging be disabled?
/// Should the debug level logging be enabled?
/// </summary>
public bool DisableLogging { set; get; }
public bool EnableLogging { set; get; }

/// <summary>
/// Fallback on db if the caching provider (redis) is down.
Expand Down
4 changes: 2 additions & 2 deletions src/EFCoreSecondLevelCacheInterceptor/EFDebugLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public EFDebugLogger(IOptions<EFCoreSecondLevelCacheSettings> cacheSettings, ILo
throw new ArgumentNullException(nameof(logger));
}

var disableLogging = cacheSettings.Value.DisableLogging;
var enableLogging = cacheSettings.Value.EnableLogging;
_cacheableEvent = cacheSettings.Value.CacheableEvent;
IsLoggerEnabled = !disableLogging && (_cacheableEvent is not null || logger.IsEnabled(LogLevel.Debug));
IsLoggerEnabled = enableLogging && (_cacheableEvent is not null || logger.IsEnabled(LogLevel.Debug));

if (IsLoggerEnabled)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
using System;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer.Entities;
using System;

namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample
namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample;

public class Startup
{
public class Startup
private readonly string _contentRootPath;
private readonly IWebHostEnvironment _env;

public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
private readonly string _contentRootPath;
_env = env;
_contentRootPath = env.ContentRootPath;
Configuration = configuration;
}

public Startup(IConfiguration configuration, IWebHostEnvironment env)
public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddEFSecondLevelCache(options =>
{
_contentRootPath = env.ContentRootPath;
Configuration = configuration;
}
options.UseMemoryCacheProvider(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30))
.ConfigureLogging(_env.IsDevelopment())

//.CacheAllQueries(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30)
/*.CacheQueriesContainingTypes(
CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30),
typeof(Post), typeof(Product), typeof(User)
)*/
.CacheQueriesContainingTableNames(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30),
TableNameComparison.ContainsOnly, "posts", "products", "users").SkipCachingCommands(commandText =>

// How to skip caching specific commands
commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase))

// Don't cache null values. Remove this optional setting if it's not necessary.
.SkipCachingResults(result
=> result.Value == null || (result.Value is EFTableRows rows && rows.RowsCount == 0))
.SkipCacheInvalidationCommands(commandText =>

public IConfiguration Configuration { get; }
// How to skip invalidating the related cache entries of this query
commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase));
});

public void ConfigureServices(IServiceCollection services)
var connectionString = Configuration["ConnectionStrings:ApplicationDbContextConnection"];

if (connectionString.Contains("%CONTENTROOTPATH%"))
{
services.AddEFSecondLevelCache(options =>
{
options.UseMemoryCacheProvider(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30))
.DisableLogging(false)
//.CacheAllQueries(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30)
/*.CacheQueriesContainingTypes(
CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30),
typeof(Post), typeof(Product), typeof(User)
)*/
.CacheQueriesContainingTableNames(
CacheExpirationMode.Absolute, TimeSpan.FromMinutes(30),
TableNameComparison.ContainsOnly,
"posts", "products", "users"
)
.SkipCachingCommands(commandText =>
// How to skip caching specific commands
commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase))
// Don't cache null values. Remove this optional setting if it's not necessary.
.SkipCachingResults(result =>
result.Value == null || (result.Value is EFTableRows rows && rows.RowsCount == 0))
.SkipCacheInvalidationCommands(commandText =>
// How to skip invalidating the related cache entries of this query
commandText.Contains("NEWID()", StringComparison.InvariantCultureIgnoreCase));
});

var connectionString = Configuration["ConnectionStrings:ApplicationDbContextConnection"];
if (connectionString.Contains("%CONTENTROOTPATH%"))
{
connectionString = connectionString.Replace("%CONTENTROOTPATH%", _contentRootPath);
}
services.AddConfiguredMsSqlDbContext(connectionString);

services.AddControllersWithViews();
connectionString = connectionString.Replace("%CONTENTROOTPATH%", _contentRootPath);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
IServiceScopeFactory scopeFactory)
services.AddConfiguredMsSqlDbContext(connectionString);

services.AddControllersWithViews();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceScopeFactory scopeFactory)
{
scopeFactory.Initialize();
scopeFactory.SeedData();

if (env.IsDevelopment())
{
scopeFactory.Initialize();
scopeFactory.SeedData();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
}
Loading

1 comment on commit b549903

@petermorlion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is in fact a breaking change and would require you to bump the version to 5.x.x. It's probably too late for that now, but it would be useful to update the docs, which still mention DisableLogging.

Please sign in to comment.