Skip to content

Commit 2c7fe52

Browse files
committed
Enable detailed logging
1 parent 0657acf commit 2c7fe52

File tree

20 files changed

+179
-55
lines changed

20 files changed

+179
-55
lines changed

src/Examples/DatabasePerTenantExample/Data/AppDbContext.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public sealed class AppDbContext : DbContext
1616

1717
public DbSet<Employee> Employees => Set<Employee>();
1818

19-
public AppDbContext(IHttpContextAccessor httpContextAccessor, IConfiguration configuration)
19+
public AppDbContext(DbContextOptions<AppDbContext> options, IHttpContextAccessor httpContextAccessor, IConfiguration configuration)
20+
: base(options)
2021
{
2122
_httpContextAccessor = httpContextAccessor;
2223
_configuration = configuration;
@@ -27,10 +28,10 @@ public void SetTenantName(string tenantName)
2728
_forcedTenantName = tenantName;
2829
}
2930

30-
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
31+
protected override void OnConfiguring(DbContextOptionsBuilder builder)
3132
{
3233
string connectionString = GetConnectionString();
33-
optionsBuilder.UseNpgsql(connectionString);
34+
builder.UseNpgsql(connectionString);
3435
}
3536

3637
private string GetConnectionString()

src/Examples/DatabasePerTenantExample/Program.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1+
using System.Diagnostics;
12
using DatabasePerTenantExample.Data;
23
using DatabasePerTenantExample.Models;
34
using JsonApiDotNetCore.Configuration;
45
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Diagnostics;
57

68
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
79

810
// Add services to the container.
911

1012
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
11-
builder.Services.AddDbContext<AppDbContext>(options => options.UseNpgsql());
13+
14+
builder.Services.AddDbContext<AppDbContext>(options => SetDbContextDebugOptions(options));
1215

1316
builder.Services.AddJsonApi<AppDbContext>(options =>
1417
{
1518
options.Namespace = "api";
1619
options.UseRelativeLinks = true;
20+
options.IncludeTotalResourceCount = true;
21+
22+
#if DEBUG
23+
options.IncludeExceptionStackTraceInErrors = true;
24+
options.IncludeRequestBodyInErrors = true;
1725
options.SerializerOptions.WriteIndented = true;
26+
#endif
1827
});
1928

2029
WebApplication app = builder.Build();
@@ -31,6 +40,14 @@
3140

3241
app.Run();
3342

43+
[Conditional("DEBUG")]
44+
static void SetDbContextDebugOptions(DbContextOptionsBuilder options)
45+
{
46+
options.EnableDetailedErrors();
47+
options.EnableSensitiveDataLogging();
48+
options.ConfigureWarnings(builder => builder.Ignore(CoreEventId.SensitiveDataLoggingEnabledWarning));
49+
}
50+
3451
static async Task CreateDatabaseAsync(string? tenantName, IServiceProvider serviceProvider)
3552
{
3653
await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope();
@@ -41,18 +58,18 @@ static async Task CreateDatabaseAsync(string? tenantName, IServiceProvider servi
4158
dbContext.SetTenantName(tenantName);
4259
}
4360

44-
await dbContext.Database.EnsureDeletedAsync();
45-
await dbContext.Database.EnsureCreatedAsync();
46-
47-
if (tenantName != null)
61+
if (await dbContext.Database.EnsureCreatedAsync())
4862
{
49-
dbContext.Employees.Add(new Employee
63+
if (tenantName != null)
5064
{
51-
FirstName = "John",
52-
LastName = "Doe",
53-
CompanyName = tenantName
54-
});
65+
dbContext.Employees.Add(new Employee
66+
{
67+
FirstName = "John",
68+
LastName = "Doe",
69+
CompanyName = tenantName
70+
});
5571

56-
await dbContext.SaveChangesAsync();
72+
await dbContext.SaveChangesAsync();
73+
}
5774
}
5875
}

src/Examples/DatabasePerTenantExample/appsettings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"Logging": {
88
"LogLevel": {
99
"Default": "Warning",
10+
// Include server startup, incoming requests and SQL commands.
1011
"Microsoft.Hosting.Lifetime": "Information",
12+
"Microsoft.AspNetCore.Hosting.Diagnostics": "Information",
1113
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
1214
}
1315
},

src/Examples/GettingStarted/Program.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1+
using System.Diagnostics;
12
using GettingStarted.Data;
23
using GettingStarted.Models;
34
using JsonApiDotNetCore.Configuration;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.EntityFrameworkCore.Diagnostics;
47

58
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
69

710
// Add services to the container.
811

9-
builder.Services.AddSqlite<SampleDbContext>("Data Source=sample.db;Pooling=False");
12+
builder.Services.AddDbContext<SampleDbContext>(options =>
13+
{
14+
options.UseSqlite("Data Source=SampleDb.db;Pooling=False");
15+
SetDbContextDebugOptions(options);
16+
});
1017

1118
builder.Services.AddJsonApi<SampleDbContext>(options =>
1219
{
1320
options.Namespace = "api";
1421
options.UseRelativeLinks = true;
1522
options.IncludeTotalResourceCount = true;
23+
24+
#if DEBUG
25+
options.IncludeExceptionStackTraceInErrors = true;
26+
options.IncludeRequestBodyInErrors = true;
1627
options.SerializerOptions.WriteIndented = true;
28+
#endif
1729
});
1830

1931
WebApplication app = builder.Build();
@@ -28,6 +40,14 @@
2840

2941
app.Run();
3042

43+
[Conditional("DEBUG")]
44+
static void SetDbContextDebugOptions(DbContextOptionsBuilder options)
45+
{
46+
options.EnableDetailedErrors();
47+
options.EnableSensitiveDataLogging();
48+
options.ConfigureWarnings(builder => builder.Ignore(CoreEventId.SensitiveDataLoggingEnabledWarning));
49+
}
50+
3151
static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
3252
{
3353
await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope();

src/Examples/GettingStarted/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14-
"launchUrl": "api/people",
14+
"launchUrl": "api/people?include=books",
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
}
1818
},
1919
"Kestrel": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22-
"launchUrl": "api/people",
22+
"launchUrl": "api/people?include=books",
2323
"applicationUrl": "http://localhost:14141",
2424
"environmentVariables": {
2525
"ASPNETCORE_ENVIRONMENT": "Development"

src/Examples/GettingStarted/appsettings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"Logging": {
33
"LogLevel": {
44
"Default": "Warning",
5+
// Include server startup, incoming requests and SQL commands.
56
"Microsoft.Hosting.Lifetime": "Information",
7+
"Microsoft.AspNetCore.Hosting.Diagnostics": "Information",
68
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
79
}
810
},

src/Examples/JsonApiDotNetCoreExample/Program.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using System.Diagnostics;
12
using System.Diagnostics.CodeAnalysis;
23
using System.Text.Json.Serialization;
34
using JsonApiDotNetCore.Configuration;
45
using JsonApiDotNetCore.Diagnostics;
56
using JsonApiDotNetCoreExample.Data;
67
using Microsoft.AspNetCore.Authentication;
78
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.EntityFrameworkCore.Diagnostics;
810
using Microsoft.Extensions.DependencyInjection.Extensions;
911

1012
[assembly: ExcludeFromCodeCoverage]
@@ -50,10 +52,7 @@ static void ConfigureServices(WebApplicationBuilder builder)
5052
string? connectionString = GetConnectionString(builder.Configuration);
5153
options.UseNpgsql(connectionString);
5254

53-
#if DEBUG
54-
options.EnableSensitiveDataLogging();
55-
options.EnableDetailedErrors();
56-
#endif
55+
SetDbContextDebugOptions(options);
5756
});
5857

5958
using (CodeTimingSessionManager.Current.Measure("AddJsonApi()"))
@@ -63,12 +62,12 @@ static void ConfigureServices(WebApplicationBuilder builder)
6362
options.Namespace = "api";
6463
options.UseRelativeLinks = true;
6564
options.IncludeTotalResourceCount = true;
66-
options.SerializerOptions.WriteIndented = true;
6765
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
6866

6967
#if DEBUG
7068
options.IncludeExceptionStackTraceInErrors = true;
7169
options.IncludeRequestBodyInErrors = true;
70+
options.SerializerOptions.WriteIndented = true;
7271
#endif
7372
}, discovery => discovery.AddCurrentAssembly());
7473
}
@@ -80,6 +79,14 @@ static void ConfigureServices(WebApplicationBuilder builder)
8079
return configuration.GetConnectionString("Default")?.Replace("###", postgresPassword);
8180
}
8281

82+
[Conditional("DEBUG")]
83+
static void SetDbContextDebugOptions(DbContextOptionsBuilder options)
84+
{
85+
options.EnableDetailedErrors();
86+
options.EnableSensitiveDataLogging();
87+
options.ConfigureWarnings(builder => builder.Ignore(CoreEventId.SensitiveDataLoggingEnabledWarning));
88+
}
89+
8390
static void ConfigurePipeline(WebApplication webApplication)
8491
{
8592
using IDisposable _ = CodeTimingSessionManager.Current.Measure("Configure pipeline");
@@ -99,8 +106,9 @@ static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
99106
await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope();
100107

101108
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
102-
await dbContext.Database.EnsureDeletedAsync();
103-
await dbContext.Database.EnsureCreatedAsync();
104109

105-
await Seeder.CreateSampleDataAsync(dbContext);
110+
if (await dbContext.Database.EnsureCreatedAsync())
111+
{
112+
await Seeder.CreateSampleDataAsync(dbContext);
113+
}
106114
}

src/Examples/JsonApiDotNetCoreExample/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
"IIS Express": {
1313
"commandName": "IISExpress",
1414
"launchBrowser": true,
15-
"launchUrl": "api/todoItems?include=tags&filter=equals(priority,'High')",
15+
"launchUrl": "api/todoItems?include=owner,assignee,tags&filter=equals(priority,'High')",
1616
"environmentVariables": {
1717
"ASPNETCORE_ENVIRONMENT": "Development"
1818
}
1919
},
2020
"Kestrel": {
2121
"commandName": "Project",
2222
"launchBrowser": true,
23-
"launchUrl": "api/todoItems?include=tags&filter=equals(priority,'High')",
23+
"launchUrl": "api/todoItems?include=owner,assignee,tags&filter=equals(priority,'High')",
2424
"applicationUrl": "https://localhost:44340;http://localhost:14140",
2525
"environmentVariables": {
2626
"ASPNETCORE_ENVIRONMENT": "Development"

src/Examples/JsonApiDotNetCoreExample/appsettings.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
"Logging": {
66
"LogLevel": {
77
"Default": "Warning",
8-
"Microsoft.Hosting.Lifetime": "Warning",
9-
"Microsoft.EntityFrameworkCore.Update": "Critical",
10-
"Microsoft.EntityFrameworkCore.Database.Command": "Critical",
11-
"JsonApiDotNetCore.Middleware.JsonApiMiddleware": "Information",
8+
// Include server startup, JsonApiDotNetCore measurements, incoming requests and SQL commands.
9+
"Microsoft.Hosting.Lifetime": "Information",
10+
"Microsoft.AspNetCore.Hosting.Diagnostics": "Information",
11+
"Microsoft.EntityFrameworkCore.Database.Command": "Information",
12+
"JsonApiDotNetCore": "Information",
1213
"JsonApiDotNetCoreExample": "Information"
1314
}
1415
},

src/Examples/MultiDbContextExample/Program.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using System.Diagnostics;
12
using JsonApiDotNetCore.Configuration;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Diagnostics;
25
using MultiDbContextExample.Data;
36
using MultiDbContextExample.Models;
47
using MultiDbContextExample.Repositories;
@@ -7,13 +10,29 @@
710

811
// Add services to the container.
912

10-
builder.Services.AddSqlite<DbContextA>("Data Source=A.db;Pooling=False");
11-
builder.Services.AddSqlite<DbContextB>("Data Source=B.db;Pooling=False");
13+
builder.Services.AddDbContext<DbContextA>(options =>
14+
{
15+
options.UseSqlite("Data Source=SampleDbA.db;Pooling=False");
16+
SetDbContextDebugOptions(options);
17+
});
18+
19+
builder.Services.AddDbContext<DbContextB>(options =>
20+
{
21+
options.UseSqlite("Data Source=SampleDbB.db;Pooling=False");
22+
SetDbContextDebugOptions(options);
23+
});
1224

1325
builder.Services.AddJsonApi(options =>
1426
{
27+
options.Namespace = "api";
28+
options.UseRelativeLinks = true;
29+
options.IncludeTotalResourceCount = true;
30+
31+
#if DEBUG
1532
options.IncludeExceptionStackTraceInErrors = true;
1633
options.IncludeRequestBodyInErrors = true;
34+
options.SerializerOptions.WriteIndented = true;
35+
#endif
1736
}, dbContextTypes: new[]
1837
{
1938
typeof(DbContextA),
@@ -35,6 +54,14 @@
3554

3655
app.Run();
3756

57+
[Conditional("DEBUG")]
58+
static void SetDbContextDebugOptions(DbContextOptionsBuilder options)
59+
{
60+
options.EnableDetailedErrors();
61+
options.EnableSensitiveDataLogging();
62+
options.ConfigureWarnings(builder => builder.Ignore(CoreEventId.SensitiveDataLoggingEnabledWarning));
63+
}
64+
3865
static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
3966
{
4067
await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope();

src/Examples/MultiDbContextExample/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
"IIS Express": {
1313
"commandName": "IISExpress",
1414
"launchBrowser": true,
15-
"launchUrl": "resourceBs",
15+
"launchUrl": "api/resourceBs",
1616
"environmentVariables": {
1717
"ASPNETCORE_ENVIRONMENT": "Development"
1818
}
1919
},
2020
"Kestrel": {
2121
"commandName": "Project",
2222
"launchBrowser": true,
23-
"launchUrl": "resourceBs",
23+
"launchUrl": "api/resourceBs",
2424
"applicationUrl": "https://localhost:44350;http://localhost:14150",
2525
"environmentVariables": {
2626
"ASPNETCORE_ENVIRONMENT": "Development"

src/Examples/MultiDbContextExample/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"Logging": {
33
"LogLevel": {
44
"Default": "Warning",
5-
"Microsoft.Hosting.Lifetime": "Warning",
6-
"Microsoft.EntityFrameworkCore.Database.Command": "Critical"
5+
// Include server startup, incoming requests and SQL commands.
6+
"Microsoft.Hosting.Lifetime": "Information",
7+
"Microsoft.AspNetCore.Hosting.Diagnostics": "Information",
8+
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
79
}
810
},
911
"AllowedHosts": "*"

0 commit comments

Comments
 (0)