Skip to content

Commit f92367a

Browse files
committed
more small fixes
1 parent b42701f commit f92367a

File tree

6 files changed

+53
-60
lines changed

6 files changed

+53
-60
lines changed

src/FluentCommand.Caching/DistributedDataCache.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,15 @@ public async Task RemoveAsync(string key, CancellationToken cancellationToken =
196196
}
197197

198198

199-
[LoggerMessage(0, LogLevel.Information, "Cache Hit; Key: '{cacheKey}'")]
199+
[LoggerMessage(0, LogLevel.Debug, "Cache Hit; Key: '{cacheKey}'")]
200200
static partial void LogCacheHit(ILogger logger, string cacheKey);
201201

202-
[LoggerMessage(1, LogLevel.Information, "Cache Miss; Key: '{cacheKey}'")]
202+
[LoggerMessage(1, LogLevel.Debug, "Cache Miss; Key: '{cacheKey}'")]
203203
static partial void LogCacheMiss(ILogger logger, string cacheKey);
204204

205-
[LoggerMessage(2, LogLevel.Information, "Cache Insert; Key: '{cacheKey}'")]
205+
[LoggerMessage(2, LogLevel.Debug, "Cache Insert; Key: '{cacheKey}'")]
206206
static partial void LogCacheInsert(ILogger logger, string cacheKey);
207207

208-
[LoggerMessage(3, LogLevel.Information, "Cache Remove; Key: '{cacheKey}'")]
208+
[LoggerMessage(3, LogLevel.Debug, "Cache Remove; Key: '{cacheKey}'")]
209209
static partial void LogCacheRemove(ILogger logger, string cacheKey);
210-
211-
212210
}

src/FluentCommand/DataCommand.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Data;
22
using System.Data.Common;
3+
using System.Diagnostics;
34

45
using FluentCommand.Extensions;
56
using FluentCommand.Internal;
@@ -556,8 +557,7 @@ private TResult QueryFactory<TResult>(Func<TResult> query, bool supportCache)
556557

557558
AssertDisposed();
558559

559-
var watch = SharedStopwatch.StartNew();
560-
var logged = false;
560+
var startingTimestamp = Stopwatch.GetTimestamp();
561561

562562
try
563563
{
@@ -579,16 +579,14 @@ private TResult QueryFactory<TResult>(Func<TResult> query, bool supportCache)
579579
}
580580
catch (Exception ex)
581581
{
582-
LogCommand(watch.Elapsed, ex);
583-
logged = true;
582+
LogCommand(startingTimestamp, ex);
583+
startingTimestamp = 0;
584584

585585
throw;
586586
}
587587
finally
588588
{
589-
// if catch block didn't already log
590-
if (!logged)
591-
LogCommand(watch.Elapsed);
589+
LogCommand(startingTimestamp);
592590

593591
_dataSession.ReleaseConnection();
594592
Dispose();
@@ -605,8 +603,7 @@ private async Task<TResult> QueryFactoryAsync<TResult>(
605603

606604
AssertDisposed();
607605

608-
var watch = SharedStopwatch.StartNew();
609-
var logged = false;
606+
var startingTimestamp = Stopwatch.GetTimestamp();
610607

611608
try
612609
{
@@ -628,16 +625,14 @@ private async Task<TResult> QueryFactoryAsync<TResult>(
628625
}
629626
catch (Exception ex)
630627
{
631-
LogCommand(watch.Elapsed, ex);
632-
logged = true;
628+
LogCommand(startingTimestamp, ex);
629+
startingTimestamp = 0;
633630

634631
throw;
635632
}
636633
finally
637634
{
638-
// if catch block didn't already log
639-
if (!logged)
640-
LogCommand(watch.Elapsed);
635+
LogCommand(startingTimestamp);
641636

642637
#if NETCOREAPP3_0_OR_GREATER
643638

@@ -751,8 +746,22 @@ await cache
751746
}
752747

753748

754-
private void LogCommand(TimeSpan duration, Exception exception = null)
749+
private void LogCommand(long startingTimestamp, Exception exception = null)
755750
{
751+
// indicates already logged
752+
if (startingTimestamp == 0)
753+
return;
754+
755+
var endingTimestamp = Stopwatch.GetTimestamp();
756+
757+
#if NET7_0_OR_GREATER
758+
var duration = Stopwatch.GetElapsedTime(startingTimestamp, endingTimestamp);
759+
#else
760+
var duration = new TimeSpan((long)((endingTimestamp - startingTimestamp) * _tickFrequency));
761+
#endif
762+
756763
_dataSession.QueryLogger?.LogCommand(Command, duration, exception, _logState);
757764
}
765+
766+
private static readonly double _tickFrequency = (double)TimeSpan.TicksPerSecond / Stopwatch.Frequency;
758767
}

src/FluentCommand/Internal/SharedStopwatch.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/FluentCommand.PostgreSQL.Tests/DatabaseFixture.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.DependencyInjection.Extensions;
66
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
78

89
using Npgsql;
910

@@ -13,9 +14,15 @@ namespace FluentCommand.PostgreSQL.Tests;
1314

1415
public class DatabaseFixture : TestHostFixture
1516
{
17+
protected override void ConfigureLogging(HostBuilderContext context, ILoggingBuilder builder)
18+
{
19+
base.ConfigureLogging(context, builder);
20+
builder.SetMinimumLevel(LogLevel.Debug);
21+
}
22+
1623
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
1724
{
18-
var trackerConnnection = context.Configuration.GetConnectionString("Tracker");
25+
var trackerConnection = context.Configuration.GetConnectionString("Tracker");
1926
var cacheConnection = context.Configuration.GetConnectionString("DistributedCache");
2027

2128
services.AddHostedService<DatabaseInitializer>();
@@ -26,7 +33,7 @@ protected override void ConfigureServices(HostBuilderContext context, IServiceCo
2633
services.TryAddSingleton<IDataConfiguration>(sp =>
2734
new DataConfiguration(
2835
NpgsqlFactory.Instance,
29-
trackerConnnection,
36+
trackerConnection,
3037
sp.GetService<IDataCache>(),
3138
sp.GetService<IQueryGenerator>(),
3239
sp.GetService<IDataQueryLogger>()

test/FluentCommand.SQLite.Tests/DatabaseFixture.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.DependencyInjection.Extensions;
88
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
910

1011
using XUnit.Hosting;
1112

1213
namespace FluentCommand.SQLite.Tests;
1314

1415
public class DatabaseFixture : TestHostFixture
1516
{
17+
protected override void ConfigureLogging(HostBuilderContext context, ILoggingBuilder builder)
18+
{
19+
base.ConfigureLogging(context, builder);
20+
builder.SetMinimumLevel(LogLevel.Debug);
21+
}
22+
1623
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
1724
{
18-
var trackerConnnection = context.Configuration.GetConnectionString("Tracker");
25+
var trackerConnection = context.Configuration.GetConnectionString("Tracker");
1926
var cacheConnection = context.Configuration.GetConnectionString("DistributedCache");
2027

2128
services.AddHostedService<DatabaseInitializer>();
@@ -26,7 +33,7 @@ protected override void ConfigureServices(HostBuilderContext context, IServiceCo
2633
services.TryAddSingleton<IDataConfiguration>(sp =>
2734
new DataConfiguration(
2835
SqliteFactory.Instance,
29-
trackerConnnection,
36+
trackerConnection,
3037
sp.GetService<IDataCache>(),
3138
sp.GetService<IQueryGenerator>(),
3239
sp.GetService<IDataQueryLogger>()

test/FluentCommand.SqlServer.Tests/DatabaseFixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.DependencyInjection.Extensions;
88
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
910

1011
using XUnit.Hosting;
1112

1213
namespace FluentCommand.SqlServer.Tests;
1314

1415
public class DatabaseFixture : TestHostFixture
1516
{
17+
protected override void ConfigureLogging(HostBuilderContext context, ILoggingBuilder builder)
18+
{
19+
base.ConfigureLogging(context, builder);
20+
builder.SetMinimumLevel(LogLevel.Debug);
21+
}
22+
1623
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
1724
{
1825
var trackerConnection = context.Configuration.GetConnectionString("Tracker");

0 commit comments

Comments
 (0)