Skip to content

Commit 8382020

Browse files
committed
feat: 新增慢連接日誌攔截器並調整相關方法參數
1 parent 00887f3 commit 8382020

File tree

4 files changed

+77
-30
lines changed

4 files changed

+77
-30
lines changed

src/Netcorext.EntityFramework.UserIdentityPattern.AspNetCore/Extensions/ServiceCollectionExtension.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore.Internal;
33
using Microsoft.Extensions.DependencyInjection.Extensions;
4-
using Netcorext.Contracts;
54
using Netcorext.EntityFramework.UserIdentityPattern.Interceptors;
65

76
namespace Netcorext.EntityFramework.UserIdentityPattern.AspNetCore;
87

98
public static class ServiceCollectionExtension
109
{
11-
private const long DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD = 1000;
10+
private const long DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD = 100;
11+
private const long DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD = 100;
1212

13-
public static IServiceCollection AddIdentityDbContext(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
14-
=> AddIdentityDbContext<IdentityReplicaDbContext<MasterContext>>(services, null, lifetime, slowCommandLoggingThreshold);
13+
public static IServiceCollection AddIdentityDbContext(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
14+
=> AddIdentityDbContext<IdentityReplicaDbContext<MasterContext>>(services, null, lifetime, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
1515

16-
public static IServiceCollection AddIdentityDbContext(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
17-
=> AddIdentityDbContext<IdentityReplicaDbContext<MasterContext>>(services, optionsAction, lifetime, slowCommandLoggingThreshold);
16+
public static IServiceCollection AddIdentityDbContext(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
17+
=> AddIdentityDbContext<IdentityReplicaDbContext<MasterContext>>(services, optionsAction, lifetime, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
1818

19-
public static IServiceCollection AddIdentitySlaveDbContext(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
20-
=> AddIdentityDbContext<IdentityReplicaDbContext<SlaveContext>>(services, null, lifetime, slowCommandLoggingThreshold);
19+
public static IServiceCollection AddIdentitySlaveDbContext(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
20+
=> AddIdentityDbContext<IdentityReplicaDbContext<SlaveContext>>(services, null, lifetime, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
2121

22-
public static IServiceCollection AddIdentitySlaveDbContext(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
23-
=> AddIdentityDbContext<IdentityReplicaDbContext<SlaveContext>>(services, optionsAction, lifetime, slowCommandLoggingThreshold);
22+
public static IServiceCollection AddIdentitySlaveDbContext(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
23+
=> AddIdentityDbContext<IdentityReplicaDbContext<SlaveContext>>(services, optionsAction, lifetime, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
2424

25-
public static IServiceCollection AddIdentityDbContext<TContext>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
25+
public static IServiceCollection AddIdentityDbContext<TContext>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
2626
where TContext : DatabaseContext =>
27-
AddIdentityDbContext<TContext>(services, null, lifetime, slowCommandLoggingThreshold);
27+
AddIdentityDbContext<TContext>(services, null, lifetime, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
2828

29-
public static IServiceCollection AddIdentityDbContext<TContext>(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
29+
public static IServiceCollection AddIdentityDbContext<TContext>(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime lifetime = ServiceLifetime.Scoped, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
3030
where TContext : DatabaseContext
3131
{
3232
services.AddContextState();
3333

3434
services.AddDbContext<TContext>((provider, builder) =>
3535
{
3636
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
37-
builder.AddInterceptors(new SlowCommandLoggingInterceptor(loggerFactory, slowCommandLoggingThreshold));
37+
38+
builder.AddInterceptors(
39+
new SlowConnectionLoggingInterceptor(loggerFactory, slowConnectionLoggingThreshold),
40+
new SlowCommandLoggingInterceptor(loggerFactory, slowCommandLoggingThreshold)
41+
);
42+
3843
optionsAction?.Invoke(provider, builder);
3944
}, lifetime);
4045

@@ -47,31 +52,36 @@ public static IServiceCollection AddIdentityDbContext<TContext>(this IServiceCol
4752
return services;
4853
}
4954

50-
public static IServiceCollection AddIdentityDbContextPool(this IServiceCollection services, int poolSize = 1024, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
51-
=> AddIdentityDbContextPool<IdentityReplicaDbContext<MasterContext>>(services, null, poolSize, slowCommandLoggingThreshold);
55+
public static IServiceCollection AddIdentityDbContextPool(this IServiceCollection services, int poolSize = 1024, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
56+
=> AddIdentityDbContextPool<IdentityReplicaDbContext<MasterContext>>(services, null, poolSize, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
5257

53-
public static IServiceCollection AddIdentityDbContextPool(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, int poolSize = 1024, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
54-
=> AddIdentityDbContextPool<IdentityReplicaDbContext<MasterContext>>(services, optionsAction, poolSize, slowCommandLoggingThreshold);
58+
public static IServiceCollection AddIdentityDbContextPool(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, int poolSize = 1024, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
59+
=> AddIdentityDbContextPool<IdentityReplicaDbContext<MasterContext>>(services, optionsAction, poolSize, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
5560

56-
public static IServiceCollection AddIdentitySlaveDbContextPool(this IServiceCollection services, int poolSize = 1024, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
57-
=> AddIdentityDbContextPool<IdentityReplicaDbContext<SlaveContext>>(services, null, poolSize, slowCommandLoggingThreshold);
61+
public static IServiceCollection AddIdentitySlaveDbContextPool(this IServiceCollection services, int poolSize = 1024, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
62+
=> AddIdentityDbContextPool<IdentityReplicaDbContext<SlaveContext>>(services, null, poolSize, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
5863

59-
public static IServiceCollection AddIdentitySlaveDbContextPool(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, int poolSize = 1024, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
60-
=> AddIdentityDbContextPool<IdentityReplicaDbContext<SlaveContext>>(services, optionsAction, poolSize, slowCommandLoggingThreshold);
64+
public static IServiceCollection AddIdentitySlaveDbContextPool(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, int poolSize = 1024, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
65+
=> AddIdentityDbContextPool<IdentityReplicaDbContext<SlaveContext>>(services, optionsAction, poolSize, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
6166

62-
public static IServiceCollection AddIdentityDbContextPool<TContext>(this IServiceCollection services, int poolSize = 1024, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
67+
public static IServiceCollection AddIdentityDbContextPool<TContext>(this IServiceCollection services, int poolSize = 1024, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
6368
where TContext : DatabaseContext =>
64-
AddIdentityDbContextPool<TContext>(services, null, poolSize, slowCommandLoggingThreshold);
69+
AddIdentityDbContextPool<TContext>(services, null, poolSize, slowConnectionLoggingThreshold, slowCommandLoggingThreshold);
6570

66-
public static IServiceCollection AddIdentityDbContextPool<TContext>(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, int poolSize = 1024, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
71+
public static IServiceCollection AddIdentityDbContextPool<TContext>(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, int poolSize = 1024, long slowConnectionLoggingThreshold = DEFAULT_SLOW_CONNECTION_LOGGING_THRESHOLD, long slowCommandLoggingThreshold = DEFAULT_SLOW_COMMAND_LOGGING_THRESHOLD)
6772
where TContext : DatabaseContext
6873
{
6974
services.AddContextState();
7075

7176
services.AddDbContextPool<TContext>((provider, builder) =>
7277
{
7378
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
74-
builder.AddInterceptors(new SlowCommandLoggingInterceptor(loggerFactory, slowCommandLoggingThreshold));
79+
80+
builder.AddInterceptors(
81+
new SlowConnectionLoggingInterceptor(loggerFactory, slowConnectionLoggingThreshold),
82+
new SlowCommandLoggingInterceptor(loggerFactory, slowCommandLoggingThreshold)
83+
);
84+
7585
optionsAction?.Invoke(provider, builder);
7686
}, poolSize);
7787

src/Netcorext.EntityFramework.UserIdentityPattern/Interceptors/SlowCommandLoggingInterceptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void LogSlowCommand(string commandText, TimeSpan duration)
6161
{
6262
if (duration.TotalMilliseconds > _slowCommandLoggingThreshold)
6363
{
64-
_logger.LogWarning("Slow DbCommand ({Duration})\n{CommandText} ()", duration, commandText);
64+
_logger.LogWarning("Detected slow SQL command: ({Duration})\n{CommandText} ()", duration, commandText);
6565
}
6666
}
67-
}
67+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Data.Common;
2+
using Microsoft.EntityFrameworkCore.Diagnostics;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace Netcorext.EntityFramework.UserIdentityPattern.Interceptors;
6+
7+
public class SlowConnectionLoggingInterceptor : DbConnectionInterceptor
8+
{
9+
private readonly ILogger<SlowConnectionLoggingInterceptor> _logger;
10+
private readonly long _slowConnectionLoggingThreshold;
11+
12+
public SlowConnectionLoggingInterceptor(ILoggerFactory loggerFactory, long slowConnectionLoggingThreshold)
13+
{
14+
_logger = loggerFactory.CreateLogger<SlowConnectionLoggingInterceptor>();
15+
_slowConnectionLoggingThreshold = slowConnectionLoggingThreshold;
16+
}
17+
18+
public override void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData)
19+
{
20+
LogSlowConnection(connection.DataSource, connection.Database, eventData.Duration);
21+
base.ConnectionOpened(connection, eventData);
22+
}
23+
24+
public override Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, CancellationToken cancellationToken = new CancellationToken())
25+
{
26+
LogSlowConnection(connection.DataSource, connection.Database, eventData.Duration);
27+
return base.ConnectionOpenedAsync(connection, eventData, cancellationToken);
28+
}
29+
30+
private void LogSlowConnection(string dataSource, string database, TimeSpan duration)
31+
{
32+
if (duration.TotalMilliseconds > _slowConnectionLoggingThreshold)
33+
{
34+
_logger.LogWarning("Detected slow database connection: DataSource={dataSource}, Database={database} ({Duration})", duration, dataSource, database);
35+
}
36+
}
37+
}

src/Versioning.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<LangVersion>latest</LangVersion>
44
<VersionMajor>1</VersionMajor>
55
<VersionMinor>0</VersionMinor>
6-
<VersionPatch>0</VersionPatch>
6+
<VersionPatch>1</VersionPatch>
77
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
88
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
99
<FileVersion>$(VersionMajor).$([System.DateTime]::UtcNow.ToString(yy)).$([System.DateTime]::UtcNow.ToString(MMdd)).$([System.DateTime]::UtcNow.ToString(HHmm))</FileVersion>
@@ -27,4 +27,4 @@
2727
</ItemGroup>
2828
</Target>
2929

30-
</Project>
30+
</Project>

0 commit comments

Comments
 (0)