Skip to content

Commit 647fe4a

Browse files
committed
log(output-formatter): add basic logging
1 parent 001e56f commit 647fe4a

File tree

4 files changed

+18
-31
lines changed

4 files changed

+18
-31
lines changed

README.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
11
[![build status](https://gitlab.cmh.edu/jcnance/JsonApiDotnetCore/badges/master/build.svg)](https://gitlab.cmh.edu/jcnance/JsonApiDotnetCore/commits/master)
22

3-
# Task List
4-
5-
- [x] Add inbound serialization formatting
6-
- [x] Add POST
7-
- [x] Add PATCH
8-
- [x] Add DELETE
9-
- [x] Add Relationships
10-
- [x] Add support for authorization/context-scoping middleware
11-
- [x] Filtering
12-
- [x] Sorting
13-
- [x] Fetching relationships
14-
- [x] Creating relationships
15-
- [x] Refactor relationships links to use an POCO that contains data and links objects
16-
- [x] Include Entities
17-
- [x] BadRequest should be 422 in POST
18-
- [x] Dasherized route support
19-
- [x] Contributing Guide
20-
- [ ] Pagination
21-
- [ ] Add integration tests to example project
22-
- [ ] Add logging
23-
- [ ] Configure Different Repositories
24-
- [ ] Build definitions
25-
- [ ] Tests
26-
- [ ] Allow for presenters with mappings
27-
- [ ] Ability to disable dasherization of names
28-
- [ ] Rename ContextEntity ??
29-
303
# Generators
314

325
- TODO: Document usage of the yeoman jadn generator

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string
128128
var entity = _jsonApiContext.RequestEntity;
129129
if(entity.Relationships.Any(r => r.RelationshipName == relationshipName))
130130
return entities.Include(relationshipName);
131-
131+
132132
throw new JsonApiException("400", "Invalid relationship",
133133
$"{entity.EntityName} does not have a relationship named {relationshipName}");
134134
}

src/JsonApiDotNetCore/Formatters/JsonApiOutputFormatter.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
2-
using System.Linq;
32
using System.Text;
43
using System.Threading.Tasks;
54
using JsonApiDotNetCore.Internal;
65
using JsonApiDotNetCore.Serialization;
76
using JsonApiDotNetCore.Services;
87
using Microsoft.AspNetCore.Mvc.Formatters;
98
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
1010
using Newtonsoft.Json;
1111

1212
namespace JsonApiDotNetCore.Formatters
@@ -28,22 +28,31 @@ public async Task WriteAsync(OutputFormatterWriteContext context)
2828
if (context == null)
2929
throw new ArgumentNullException(nameof(context));
3030

31+
var logger = GetService<ILoggerFactory>(context)?
32+
.CreateLogger<JsonApiOutputFormatter>();
33+
34+
logger?.LogInformation("Formatting response as JSONAPI");
35+
3136
var response = context.HttpContext.Response;
3237

3338
using (var writer = context.WriterFactory(response.Body, Encoding.UTF8))
3439
{
35-
var jsonApiContext = context.HttpContext.RequestServices.GetService<IJsonApiContext>();
40+
var jsonApiContext = GetService<IJsonApiContext>(context);
3641

3742
string responseContent;
3843
try
3944
{
4045
if(context.Object.GetType() == typeof(Error))
46+
{
47+
logger?.LogInformation("Response was type <Error>. Serializing as plain JSON.");
4148
responseContent = JsonConvert.SerializeObject(context.Object);
49+
}
4250
else
4351
responseContent = JsonApiSerializer.Serialize(context.Object, jsonApiContext);
4452
}
4553
catch(Exception e)
4654
{
55+
logger?.LogError("An error ocurred while formatting the response.", e);
4756
responseContent = new Error("400", e.Message).GetJson();
4857
response.StatusCode = 400;
4958
}
@@ -52,5 +61,10 @@ public async Task WriteAsync(OutputFormatterWriteContext context)
5261
await writer.FlushAsync();
5362
}
5463
}
64+
65+
private T GetService<T>(OutputFormatterWriteContext context)
66+
{
67+
return context.HttpContext.RequestServices.GetService<T>();
68+
}
5569
}
5670
}

src/JsonApiDotNetCore/Middleware/JsonApiExceptionFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public JsonApiExceptionFilter(ILoggerFactory loggerFactory)
1717

1818
public void OnException(ExceptionContext context)
1919
{
20-
_logger.LogError(new EventId(), context.Exception, "An unhandled exception occurred during the request");
20+
_logger?.LogError(new EventId(), context.Exception, "An unhandled exception occurred during the request");
2121

2222
var jsonApiException = context.Exception as JsonApiException;
2323

0 commit comments

Comments
 (0)