Skip to content

Commit 9fd3fc7

Browse files
author
Bart Koelman
authored
Cleanup of public overloads for ILoggerFactory (#674)
* Cleanup of public overloads for `ILoggerFactory` Ensured consistent passing along of `ILoggerFactory` parameter downwards to base classes and dependencies. I ended up making them all non-optional, because it is easy to forget passing it (compiler does not warn). Callers can still pass `null` as a value, but that should be very rare, since ASP.NET Core includes built-in logging that is automatically registered. In the process, I added some missing namespaces in examples (types were in global namespace). `BaseJsonApiController<T>` and `BaseJsonApiController<T, TId>`: - Made types abstract with protected constructors, fixed related tests - Ensure all overloads have `ILoggerFactory` parameter and its value is passed downwards - Have overloads call each other, so that fields assignment occurs only once `JsonApiCmdController<T>` and `JsonApiCmdController<T, TId>`, `JsonApiQueryController<T>` and `JsonApiQueryController<T, TId>`: - Ensure all overloads have `ILoggerFactory` parameter and its value is passed downwards - Changed `IResourceService<>` parameter into `IResourceQueryService<>` / `IResourceCmdService` (still allows to pass `IResourceService<>`, because it implements both) - Aligned parameter names with base class `JsonApiController<T>` and `JsonApiController<T, TId>`: - Bugfix where loggerFactory was assigned the value `null` while passing it downwards! - Ensure all overloads have `ILoggerFactory` parameter and its value is passed downwards `DefaultResourceRepository<TResource>` and `DefaultResourceRepository<TResource, TId>`: - Removed unneeded constructor overload `DefaultExceptionFilter`: - Added null conditional operator, because at logging time the logger is assumed to be possibly `null`. All unit tests succeed with these changes. * Fixed broken build after merge * Fix broken logging configuration After removing all custom log setup, logging started to actually work, in Kestrel Console window and VS Debug Output. Console and Debug writers are added automatically by the runtime. And levels from appsettings.json are also picked up automatically. * Fixed: do not create logger when no factory is available. * Revert style changes * - Changed ILoggerFactory into a required parameter. - Added single trace-level log message to controller, service and repository (to make sure we have no crashing tests because they incorrectly pass null). - Removed conditional logging statement
1 parent c9f5eb4 commit 9fd3fc7

37 files changed

+304
-325
lines changed

src/Examples/GettingStarted/Controllers/ArticlesController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
using JsonApiDotNetCore.Configuration;
33
using JsonApiDotNetCore.Controllers;
44
using JsonApiDotNetCore.Services;
5+
using Microsoft.Extensions.Logging;
56

67
namespace GettingStarted
78
{
89
public class ArticlesController : JsonApiController<Article>
910
{
1011
public ArticlesController(
1112
IJsonApiOptions jsonApiOptions,
13+
ILoggerFactory loggerFactory,
1214
IResourceService<Article> resourceService)
13-
: base(jsonApiOptions, resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
1416
{ }
1517
}
1618
}

src/Examples/GettingStarted/Controllers/PeopleController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
using JsonApiDotNetCore.Configuration;
33
using JsonApiDotNetCore.Controllers;
44
using JsonApiDotNetCore.Services;
5+
using Microsoft.Extensions.Logging;
56

67
namespace GettingStarted
78
{
89
public class PeopleController : JsonApiController<Person>
910
{
1011
public PeopleController(
1112
IJsonApiOptions jsonApiOptions,
13+
ILoggerFactory loggerFactory,
1214
IResourceService<Person> resourceService)
13-
: base(jsonApiOptions, resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
1416
{ }
1517
}
1618
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
33
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
45

56
namespace GettingStarted.ResourceDefinitionExample
67
{
78
public class ModelsController : JsonApiController<Model>
89
{
910
public ModelsController(
1011
IJsonApiOptions jsonApiOptions,
12+
ILoggerFactory loggerFactory,
1113
IResourceService<Model> resourceService)
12-
: base(jsonApiOptions, resourceService)
14+
: base(jsonApiOptions, loggerFactory, resourceService)
1315
{ }
1416
}
1517
}

src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
using JsonApiDotNetCore.Controllers;
33
using JsonApiDotNetCore.Services;
44
using JsonApiDotNetCoreExample.Models;
5+
using Microsoft.Extensions.Logging;
56

67
namespace JsonApiDotNetCoreExample.Controllers
78
{
89
public class ArticlesController : JsonApiController<Article>
910
{
1011
public ArticlesController(
1112
IJsonApiOptions jsonApiOptions,
12-
IResourceService<Article> resourceService)
13-
: base(jsonApiOptions, resourceService)
13+
ILoggerFactory loggerFactory,
14+
IResourceService<Article> resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
1416
{ }
1517
}
1618
}

src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class KebabCasedModelsController : JsonApiController<KebabCasedModel>
1010
{
1111
public KebabCasedModelsController(
1212
IJsonApiOptions jsonApiOptions,
13-
IResourceService<KebabCasedModel> resourceService,
14-
ILoggerFactory loggerFactory)
15-
: base(jsonApiOptions, resourceService, loggerFactory)
13+
ILoggerFactory loggerFactory,
14+
IResourceService<KebabCasedModel> resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
1616
{ }
1717
}
1818
}

src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace JsonApiDotNetCoreExample.Controllers
88
{
99
public class PassportsController : JsonApiController<Passport>
1010
{
11-
public PassportsController(IJsonApiOptions jsonApiOptions,
12-
IResourceService<Passport, int> resourceService,
13-
ILoggerFactory loggerFactory = null)
14-
: base(jsonApiOptions, resourceService, loggerFactory)
15-
{
16-
}
11+
public PassportsController(
12+
IJsonApiOptions jsonApiOptions,
13+
ILoggerFactory loggerFactory,
14+
IResourceService<Passport, int> resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
16+
{ }
1717
}
1818
}

src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class PeopleController : JsonApiController<Person>
1010
{
1111
public PeopleController(
1212
IJsonApiOptions jsonApiOptions,
13-
IResourceService<Person> resourceService,
14-
ILoggerFactory loggerFactory)
15-
: base(jsonApiOptions, resourceService, loggerFactory)
13+
ILoggerFactory loggerFactory,
14+
IResourceService<Person> resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
1616
{ }
1717
}
1818
}

src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class PersonRolesController : JsonApiController<PersonRole>
1010
{
1111
public PersonRolesController(
1212
IJsonApiOptions jsonApiOptions,
13-
IResourceService<PersonRole> resourceService,
14-
ILoggerFactory loggerFactory)
15-
: base(jsonApiOptions, resourceService, loggerFactory)
13+
ILoggerFactory loggerFactory,
14+
IResourceService<PersonRole> resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
1616
{ }
1717
}
1818
}

src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class TagsController : JsonApiController<Tag>
1010
{
1111
public TagsController(
1212
IJsonApiOptions jsonApiOptions,
13-
IResourceService<Tag> resourceService,
14-
ILoggerFactory loggerFactory)
15-
: base(jsonApiOptions, resourceService, loggerFactory)
13+
ILoggerFactory loggerFactory,
14+
IResourceService<Tag, int> resourceService)
15+
: base(jsonApiOptions, loggerFactory, resourceService)
1616
{ }
1717
}
1818
}

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using JsonApiDotNetCore.Configuration;
55
using JsonApiDotNetCore.Controllers;
66
using JsonApiDotNetCore.Data;
7-
using JsonApiDotNetCore.Internal.Contracts;
87
using JsonApiDotNetCore.Services;
98
using JsonApiDotNetCoreExample.Models;
109
using Microsoft.AspNetCore.Mvc;
@@ -18,11 +17,11 @@ public class TodoCollectionsController : JsonApiController<TodoItemCollection, G
1817
readonly IDbContextResolver _dbResolver;
1918

2019
public TodoCollectionsController(
21-
IJsonApiOptions jsonApiOptions,
22-
IDbContextResolver contextResolver,
23-
IResourceService<TodoItemCollection, Guid> resourceService,
24-
ILoggerFactory loggerFactory)
25-
: base(jsonApiOptions, resourceService, loggerFactory)
20+
IJsonApiOptions jsonApiOptions,
21+
ILoggerFactory loggerFactory,
22+
IDbContextResolver contextResolver,
23+
IResourceService<TodoItemCollection, Guid> resourceService)
24+
: base(jsonApiOptions, loggerFactory, resourceService)
2625
{
2726
_dbResolver = contextResolver;
2827
}

0 commit comments

Comments
 (0)