Skip to content

Commit 4af4502

Browse files
committed
chore(controllers): enrich log events
1 parent 4720a4e commit 4af4502

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/Dotnet.Samples.AspNetCore.WebApi/Controllers/PlayerController.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,22 @@ public async Task<IResult> PostAsync([FromBody] PlayerRequestModel player)
4141
.Errors.Select(error => new { error.PropertyName, error.ErrorMessage })
4242
.ToArray();
4343

44-
logger.LogWarning("POST validation failed: {@Errors}", errors);
44+
logger.LogWarning("POST /players validation failed: {@Errors}", errors);
4545
return TypedResults.BadRequest(errors);
4646
}
4747

4848
if (await playerService.RetrieveByIdAsync(player.Id) != null)
4949
{
50+
logger.LogWarning(
51+
"POST /players failed: Player with ID {Id} already exists",
52+
player.Id
53+
);
5054
return TypedResults.Conflict();
5155
}
5256

5357
var result = await playerService.CreateAsync(player);
5458

59+
logger.LogInformation("POST /players created: {@Player}", result);
5560
return TypedResults.CreatedAtRoute(
5661
routeName: "GetById",
5762
routeValues: new { id = result.Id },
@@ -77,10 +82,12 @@ public async Task<IResult> GetAsync()
7782

7883
if (players.Count > 0)
7984
{
85+
logger.LogInformation("GET /players retrieved");
8086
return TypedResults.Ok(players);
8187
}
8288
else
8389
{
90+
logger.LogWarning("GET /players not found");
8491
return TypedResults.NotFound();
8592
}
8693
}
@@ -97,13 +104,14 @@ public async Task<IResult> GetAsync()
97104
public async Task<IResult> GetByIdAsync([FromRoute] long id)
98105
{
99106
var player = await playerService.RetrieveByIdAsync(id);
100-
101107
if (player != null)
102108
{
109+
logger.LogInformation("GET /players/{Id} retrieved: {@Player}", id, player);
103110
return TypedResults.Ok(player);
104111
}
105112
else
106113
{
114+
logger.LogWarning("GET /players/{Id} not found", id);
107115
return TypedResults.NotFound();
108116
}
109117
}
@@ -120,13 +128,18 @@ public async Task<IResult> GetByIdAsync([FromRoute] long id)
120128
public async Task<IResult> GetBySquadNumberAsync([FromRoute] int squadNumber)
121129
{
122130
var player = await playerService.RetrieveBySquadNumberAsync(squadNumber);
123-
124131
if (player != null)
125132
{
133+
logger.LogInformation(
134+
"GET /players/squad/{SquadNumber} retrieved: {@Player}",
135+
squadNumber,
136+
player
137+
);
126138
return TypedResults.Ok(player);
127139
}
128140
else
129141
{
142+
logger.LogWarning("GET /players/squad/{SquadNumber} not found", squadNumber);
130143
return TypedResults.NotFound();
131144
}
132145
}
@@ -151,7 +164,6 @@ public async Task<IResult> GetBySquadNumberAsync([FromRoute] int squadNumber)
151164
public async Task<IResult> PutAsync([FromRoute] long id, [FromBody] PlayerRequestModel player)
152165
{
153166
var validation = await validator.ValidateAsync(player);
154-
155167
if (!validation.IsValid)
156168
{
157169
var errors = validation
@@ -161,14 +173,13 @@ public async Task<IResult> PutAsync([FromRoute] long id, [FromBody] PlayerReques
161173
logger.LogWarning("PUT /players/{Id} validation failed: {@Errors}", id, errors);
162174
return TypedResults.BadRequest(errors);
163175
}
164-
165176
if (await playerService.RetrieveByIdAsync(id) == null)
166177
{
178+
logger.LogWarning("PUT /players/{Id} not found", id);
167179
return TypedResults.NotFound();
168180
}
169-
170181
await playerService.UpdateAsync(player);
171-
182+
logger.LogInformation("PUT /players/{Id} updated: {@Player}", id, player);
172183
return TypedResults.NoContent();
173184
}
174185

@@ -189,12 +200,13 @@ public async Task<IResult> DeleteAsync([FromRoute] long id)
189200
{
190201
if (await playerService.RetrieveByIdAsync(id) == null)
191202
{
203+
logger.LogWarning("DELETE /players/{Id} not found", id);
192204
return TypedResults.NotFound();
193205
}
194206
else
195207
{
196208
await playerService.DeleteAsync(id);
197-
209+
logger.LogInformation("DELETE /players/{Id} deleted", id);
198210
return TypedResults.NoContent();
199211
}
200212
}

0 commit comments

Comments
 (0)