Skip to content

Commit ebb6ccd

Browse files
committed
#35050 Snippets and improvements.
1 parent 94f68d8 commit ebb6ccd

4 files changed

Lines changed: 44 additions & 40 deletions

File tree

src/aspire/logging-metalama/TodoList/TodoList.Api/Services/TodoService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ public partial class TodoService( ApplicationDbContext db )
88
public async Task<IEnumerable<Todo>> GetTodosAsync( CancellationToken cancellationToken = default )
99
=> await db.Todos.ToArrayAsync( cancellationToken );
1010

11+
// <snippet> LogUsingAttribute
12+
[Log]
1113
public async Task<Todo?> GetTodoAsync( int id, CancellationToken cancellationToken = default )
1214
=> await db.Todos.FindAsync( id );
15+
// <endsnippet> LogUsingAttribute
1316

1417
public async Task<Todo> AddTodoAsync( Todo todo, CancellationToken cancellationToken = default )
1518
{

src/aspire/logging-metalama/TodoList/TodoList.Aspects/Fabric.cs renamed to src/aspire/logging-metalama/TodoList/TodoList.Aspects/LogAllPublicMethodsFabric.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Metalama.Framework.Fabrics;
22
using Metalama.Framework.Code;
33

4-
internal class Fabric : TransitiveProjectFabric
4+
// <snippet> Fabric
5+
internal class LogAllPublicMethodsFabric : TransitiveProjectFabric
56
{
67
public override void AmendProject( IProjectAmender amender ) =>
78
amender
@@ -10,4 +11,5 @@ public override void AmendProject( IProjectAmender amender ) =>
1011
.SelectMany( type => type.Methods )
1112
.Where( method => method.Accessibility == Accessibility.Public && method.Name != "ToString" )
1213
.AddAspectIfEligible<LogAttribute>();
13-
}
14+
}
15+
// <endsnippet> Fabric

src/aspire/logging-metalama/TodoList/TodoList.Aspects/LogAttribute.cs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public override void BuildEligibility(IEligibilityBuilder<IMethod> builder)
2828
// Write entry message.
2929
if ( isTracingEnabled )
3030
{
31-
var entryMessage = BuildInterpolatedString();
32-
var arguments = BuildArguments();
33-
entryMessage.AddText( " started." );
34-
3531
using ( var guard = LoggingRecursionGuard.Begin() )
3632
{
3733
if ( guard.CanLog )
3834
{
35+
var entryMessage = BuildInterpolatedString();
36+
var arguments = BuildArguments();
37+
entryMessage.AddText( " started." );
38+
3939
this._logger.LogTrace( (string) entryMessage.ToValue(), (object?[]) arguments.ToValue()! );
4040
}
4141
}
@@ -48,36 +48,35 @@ public override void BuildEligibility(IEligibilityBuilder<IMethod> builder)
4848

4949
if ( isTracingEnabled )
5050
{
51-
// Display the success message. The message is different when the method is void.
52-
var successMessage = BuildInterpolatedString( true );
53-
ExpressionBuilder arguments;
54-
var isVoid = meta.Target.Method.GetAsyncInfo().ResultType.Is( typeof( void ) );
55-
56-
if ( isVoid )
57-
{
58-
// When the method is void, display a constant text.
59-
successMessage.AddText(" succeeded.");
60-
arguments = BuildArguments(true);
61-
}
62-
else
63-
{
64-
// When the method has a return value, add it to the message.
65-
successMessage.AddText(" returned {result}.");
66-
67-
if ( SensitiveParameterFilter.IsSensitive( meta.Target.Method.ReturnParameter ) )
68-
{
69-
arguments = BuildArguments(true, true, true);
70-
}
71-
else
72-
{
73-
arguments = BuildArguments(true, true);
74-
}
75-
}
76-
7751
using ( var guard = LoggingRecursionGuard.Begin() )
7852
{
7953
if ( guard.CanLog )
8054
{
55+
// Display the success message. The message is different when the method is void.
56+
var successMessage = BuildInterpolatedString( true );
57+
ExpressionBuilder arguments;
58+
var isVoid = meta.Target.Method.GetAsyncInfo().ResultType.Is( typeof( void ) );
59+
60+
if ( isVoid )
61+
{
62+
// When the method is void, display a constant text.
63+
successMessage.AddText( " succeeded." );
64+
arguments = BuildArguments( true );
65+
}
66+
else
67+
{
68+
// When the method has a return value, add it to the message.
69+
successMessage.AddText( " returned {result}." );
70+
71+
if ( SensitiveParameterFilter.IsSensitive( meta.Target.Method.ReturnParameter ) )
72+
{
73+
arguments = BuildArguments( true, true, true );
74+
}
75+
else
76+
{
77+
arguments = BuildArguments( true, true );
78+
}
79+
}
8180
this._logger.LogTrace( (string) successMessage.ToValue(), (object?[]) arguments.ToValue()! );
8281
}
8382
}
@@ -87,17 +86,18 @@ public override void BuildEligibility(IEligibilityBuilder<IMethod> builder)
8786
}
8887
catch ( Exception e ) when ( this._logger.IsEnabled( LogLevel.Warning ) )
8988
{
90-
// Display the failure message.
91-
var failureMessage = BuildInterpolatedString();
92-
var arguments = BuildArguments();
93-
failureMessage.AddText( " failed: " );
94-
failureMessage.AddExpression( e.Message );
95-
9689
using ( var guard = LoggingRecursionGuard.Begin() )
9790
{
9891
if ( guard.CanLog )
9992
{
100-
this._logger.LogWarning( (string) failureMessage.ToValue(), (object?[])arguments.ToValue()! );
93+
// Display the failure message.
94+
var failureMessage = BuildInterpolatedString();
95+
var arguments = BuildArguments();
96+
failureMessage.AddText( " failed: " );
97+
failureMessage.AddExpression( e.Message );
98+
99+
100+
this._logger.LogWarning( (string) failureMessage.ToValue(), (object?[]) arguments.ToValue()! );
101101
}
102102
}
103103

src/aspire/logging-metalama/TodoList/TodoList.ServiceDefaults/Extensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.Extensions.Logging;
66
using OpenTelemetry;
77
using OpenTelemetry.Metrics;
8-
using OpenTelemetry.Resources;
98
using OpenTelemetry.Trace;
109

1110
namespace Microsoft.Extensions.Hosting;

0 commit comments

Comments
 (0)