Skip to content

Commit 94f68d8

Browse files
committed
#35050 Logging added.
1 parent 3a055fe commit 94f68d8

26 files changed

Lines changed: 511 additions & 85 deletions

src/aspire/logging-metalama/TodoList/TodoList.Api/Migrations/20240617131158_InitialCreate.Designer.cs renamed to src/aspire/logging-metalama/TodoList/TodoList.Api/Migrations/20240718092245_Initial.Designer.cs

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aspire/logging-metalama/TodoList/TodoList.Api/Migrations/20240617131158_InitialCreate.cs renamed to src/aspire/logging-metalama/TodoList/TodoList.Api/Migrations/20240718092245_Initial.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#nullable disable
44

5-
namespace TodoList.ApiService.Migrations
5+
namespace TodoList.Api.Migrations
66
{
77
/// <inheritdoc />
8-
public partial class InitialCreate : Migration
8+
public partial class Initial : Migration
99
{
1010
/// <inheritdoc />
1111
protected override void Up(MigrationBuilder migrationBuilder)
@@ -16,6 +16,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
1616
{
1717
Id = table.Column<int>(type: "INTEGER", nullable: false)
1818
.Annotation("Sqlite:Autoincrement", true),
19+
IsCompleted = table.Column<bool>(type: "INTEGER", nullable: false),
1920
Title = table.Column<string>(type: "TEXT", nullable: false)
2021
},
2122
constraints: table =>

src/aspire/logging-metalama/TodoList/TodoList.Api/Migrations/ApplicationDbContextModelSnapshot.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66

77
#nullable disable
88

9-
namespace TodoList.ApiService.Migrations
9+
namespace TodoList.Api.Migrations
1010
{
1111
[DbContext(typeof(ApplicationDbContext))]
1212
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
1313
{
1414
protected override void BuildModel(ModelBuilder modelBuilder)
1515
{
1616
#pragma warning disable 612, 618
17-
modelBuilder.HasAnnotation("ProductVersion", "8.0.6");
17+
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
1818

1919
modelBuilder.Entity("Todo", b =>
2020
{
2121
b.Property<int>("Id")
2222
.ValueGeneratedOnAdd()
2323
.HasColumnType("INTEGER");
2424

25+
b.Property<bool>("IsCompleted")
26+
.HasColumnType("INTEGER");
27+
2528
b.Property<string>("Title")
2629
.IsRequired()
2730
.HasColumnType("TEXT");

src/aspire/logging-metalama/TodoList/TodoList.Api/Program.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using Metalama.Patterns.Caching.Backends.Redis;
2-
using Metalama.Patterns.Caching.Building;
3-
using StackExchange.Redis;
4-
using System.Globalization;
1+
using Microsoft.Data.Sqlite;
2+
using Microsoft.EntityFrameworkCore;
53
using TodoList.ApiService.Model;
64
using TodoList.ApiService.Services;
75

@@ -10,21 +8,24 @@
108
// Add service defaults & Aspire components.
119
builder.AddServiceDefaults();
1210

13-
// [<snippet AddRedis>]
14-
builder.AddRedisClient( "cache" );
15-
// [<endsnippet AddRedis>]
11+
// Add services to the container.
1612

17-
// [<snippet AddMetalamaCaching>]
18-
builder.Services.AddMetalamaCaching(
19-
caching => caching.WithBackend( backend => backend.Redis( )) );
20-
// [<endsnippet AddMetalamaCaching>]
13+
// Keep the database connection open for the lifetime of the application, so the in-memory database is not lost after each operation.
14+
// https://github.com/dotnet/efcore/issues/9842#issuecomment-1634427346
15+
using var dbConnection = new SqliteConnection( "Data Source=:memory:" );
16+
await dbConnection.OpenAsync();
17+
builder.Services.AddDbContext<ApplicationDbContext>( dbOptions => dbOptions.UseSqlite( dbConnection ) );
2118

22-
// Add services to the container.
23-
builder.AddSqlServerDbContext<ApplicationDbContext>( "database" );
2419
builder.Services.AddScoped<TodoService>();
2520

2621
var app = builder.Build();
2722

23+
using ( var scope = app.Services.CreateScope() )
24+
{
25+
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database;
26+
db.EnsureCreated();
27+
}
28+
2829
app.MapDefaultEndpoints();
2930

3031
// Configure the HTTP request pipeline.
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
using Metalama.Patterns.Caching;
2-
using Metalama.Patterns.Caching.Aspects;
3-
using Microsoft.EntityFrameworkCore;
4-
using StackExchange.Redis;
1+
using Microsoft.EntityFrameworkCore;
52
using TodoList.ApiService.Model;
63

74
namespace TodoList.ApiService.Services;
85

96
public partial class TodoService( ApplicationDbContext db )
107
{
11-
// [<snippet Caching>]
12-
[Cache]
138
public async Task<IEnumerable<Todo>> GetTodosAsync( CancellationToken cancellationToken = default )
14-
=> await db.Todos.ToListAsync( cancellationToken );
9+
=> await db.Todos.ToArrayAsync( cancellationToken );
1510

16-
[Cache]
1711
public async Task<Todo?> GetTodoAsync( int id, CancellationToken cancellationToken = default )
1812
=> await db.Todos.FindAsync( id );
19-
// [<endsnippet Caching>]
2013

21-
[InvalidateCache( nameof(this.GetTodosAsync) )]
2214
public async Task<Todo> AddTodoAsync( Todo todo, CancellationToken cancellationToken = default )
2315
{
2416
var newEntry = db.Todos.Add( todo );
2517
await db.SaveChangesAsync( cancellationToken );
2618

27-
28-
await this._cachingService.InvalidateAsync( this.GetTodoAsync, newEntry.Entity.Id, cancellationToken,
29-
cancellationToken );
30-
3119
return newEntry.Entity;
3220
}
3321

34-
35-
// [<snippet ImperativeInvalidation>]
3622
public async Task<bool> UpdateTodoAsync( Todo todo, CancellationToken cancellationToken = default )
3723
{
3824
var existingTodo = await this.GetTodoAsync( todo.Id, cancellationToken );
@@ -47,16 +33,9 @@ await this._cachingService.InvalidateAsync( this.GetTodoAsync, newEntry.Entity.I
4733
db.Todos.Update( existingTodo );
4834
await db.SaveChangesAsync( cancellationToken );
4935

50-
// Invalidate the cache for GetTodoAsync imperatively, as the todo.Id is not directly visible from the parameter.
51-
await this._cachingService.InvalidateAsync( this.GetTodoAsync, todo.Id, cancellationToken, cancellationToken );
52-
await this._cachingService.InvalidateAsync( this.GetTodosAsync, cancellationToken, cancellationToken );
53-
5436
return true;
5537
}
56-
// [<endsnippet ImperativeInvalidation>]
5738

58-
// [<snippet DeclarativeInvalidation>]
59-
[InvalidateCache( nameof(this.GetTodosAsync), nameof(this.GetTodoAsync) )]
6039
public async Task<bool> DeleteTodoAsync( int id, CancellationToken cancellationToken = default )
6140
{
6241
var todo = await this.GetTodoAsync( id, cancellationToken );
@@ -71,5 +50,4 @@ await this._cachingService.InvalidateAsync( this.GetTodoAsync, newEntry.Entity.I
7150

7251
return true;
7352
}
74-
// [<endsnippet DeclarativeInvalidation>]
7553
}

src/aspire/logging-metalama/TodoList/TodoList.Api/TodoList.Api.csproj

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
11-
<PackageReference Include="Aspire.StackExchange.Redis" Version="8.0.2" />
1210
<PackageReference Include="Azure.Identity" Version="1.12.0" />
13-
<PackageReference Include="Metalama.Patterns.Caching.Aspects" Version="2024.2.14-rc" />
14-
<PackageReference Include="Metalama.Patterns.Caching.Backends.Redis" Version="2024.2.14-rc" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
1613
<PrivateAssets>all</PrivateAssets>
1714
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1815
</PackageReference>
1916
</ItemGroup>
2017

2118
<ItemGroup>
19+
<ProjectReference Include="..\TodoList.Aspects\TodoList.Aspects.csproj" />
2220
<ProjectReference Include="..\TodoList.DataAccess\TodoList.DataAccess.csproj" />
2321
<ProjectReference Include="..\TodoList.ServiceDefaults\TodoList.ServiceDefaults.csproj" />
2422
</ItemGroup>

src/aspire/logging-metalama/TodoList/TodoList.Api/appsettings.Development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Logging": {
33
"LogLevel": {
4-
"Default": "Information",
4+
"Default": "Trace",
55
"Microsoft.AspNetCore": "Warning"
66
}
77
}

src/aspire/logging-metalama/TodoList/TodoList.AppHost/Program.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,7 @@
22

33
var builder = DistributedApplication.CreateBuilder( args );
44

5-
var db = builder
6-
.AddSqlServer( "dbengine" )
7-
.AddDatabase( "database" );
8-
9-
// [<snippet AppHostCacheConfiguration>]
10-
var cache = builder
11-
.AddRedis( "cache" );
12-
13-
var api = builder
14-
.AddProject<Projects.TodoList_Api>( "todolist-api" )
15-
.WithReference( db )
16-
.WithReference( cache );
17-
// [<endsnippet AppHostCacheConfiguration>]
5+
var api = builder.AddProject<Projects.TodoList_Api>( "todolist-api" );
186

197
builder.AddProject<Projects.TodoList_Web>("todolist-web" )
208
.WithExternalHttpEndpoints()

src/aspire/logging-metalama/TodoList/TodoList.AppHost/TodoList.AppHost.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.0.2" />
14-
<PackageReference Include="Aspire.Hosting.Redis" Version="8.0.2" />
15-
<PackageReference Include="Aspire.Hosting.SqlServer" Version="8.0.2" />
1614
</ItemGroup>
1715

1816
<ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Metalama.Framework.Fabrics;
2+
using Metalama.Framework.Code;
3+
4+
internal class Fabric : TransitiveProjectFabric
5+
{
6+
public override void AmendProject( IProjectAmender amender ) =>
7+
amender
8+
.SelectTypes()
9+
.Where( type => type.Accessibility == Accessibility.Public )
10+
.SelectMany( type => type.Methods )
11+
.Where( method => method.Accessibility == Accessibility.Public && method.Name != "ToString" )
12+
.AddAspectIfEligible<LogAttribute>();
13+
}

0 commit comments

Comments
 (0)