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
}

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs

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

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public abstract class AbstractTodoItemsController<T>
1313
{
1414
protected AbstractTodoItemsController(
1515
IJsonApiOptions jsonApiOptions,
16-
IResourceService<T, int> service,
17-
ILoggerFactory loggerFactory)
18-
: base(jsonApiOptions, service, loggerFactory)
16+
ILoggerFactory loggerFactory,
17+
IResourceService<T, int> service)
18+
: base(jsonApiOptions, loggerFactory, service)
1919
{ }
2020
}
2121

@@ -24,9 +24,9 @@ public class TodoItemsTestController : AbstractTodoItemsController<TodoItem>
2424
{
2525
public TodoItemsTestController(
2626
IJsonApiOptions jsonApiOptions,
27-
IResourceService<TodoItem> service,
28-
ILoggerFactory loggerFactory)
29-
: base(jsonApiOptions, service, loggerFactory)
27+
ILoggerFactory loggerFactory,
28+
IResourceService<TodoItem> service)
29+
: base(jsonApiOptions, loggerFactory, service)
3030
{ }
3131
}
3232
}

src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs

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

1919
public class SuperUsersController : JsonApiController<SuperUser>
2020
{
2121
public SuperUsersController(
2222
IJsonApiOptions jsonApiOptions,
23-
IResourceService<SuperUser> resourceService,
24-
ILoggerFactory loggerFactory)
25-
: base(jsonApiOptions, resourceService, loggerFactory)
23+
ILoggerFactory loggerFactory,
24+
IResourceService<SuperUser> resourceService)
25+
: base(jsonApiOptions, loggerFactory, resourceService)
2626
{ }
2727
}
2828
}

src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ namespace JsonApiDotNetCoreExample.Services
1414
{
1515
public class CustomArticleService : DefaultResourceService<Article>
1616
{
17-
public CustomArticleService(IEnumerable<IQueryParameterService> queryParameters,
18-
IJsonApiOptions options,
19-
IResourceRepository<Article, int> repository,
20-
IResourceContextProvider provider,
21-
IResourceHookExecutor hookExecutor = null,
22-
ILoggerFactory loggerFactory = null)
23-
: base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { }
17+
public CustomArticleService(
18+
IEnumerable<IQueryParameterService> queryParameters,
19+
IJsonApiOptions options,
20+
ILoggerFactory loggerFactory,
21+
IResourceRepository<Article, int> repository,
22+
IResourceContextProvider provider,
23+
IResourceHookExecutor hookExecutor = null)
24+
: base(queryParameters, options, loggerFactory, repository, provider, hookExecutor)
25+
{ }
2426

2527
public override async Task<Article> GetAsync(int id)
2628
{

src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.AspNetCore.Hosting;
22
using Microsoft.Extensions.DependencyInjection;
3-
using Microsoft.Extensions.Logging;
43
using JsonApiDotNetCoreExample.Data;
54
using Microsoft.EntityFrameworkCore;
65
using JsonApiDotNetCore.Extensions;
@@ -18,14 +17,8 @@ public NoDefaultPageSizeStartup(IWebHostEnvironment env) : base(env) { }
1817

1918
public override void ConfigureServices(IServiceCollection services)
2019
{
21-
var loggerFactory = new LoggerFactory();
2220
var mvcBuilder = services.AddMvcCore();
2321
services
24-
.AddSingleton<ILoggerFactory>(loggerFactory)
25-
.AddLogging(builder =>
26-
{
27-
builder.AddConsole();
28-
})
2922
.AddDbContext<AppDbContext>(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient)
3023
.AddJsonApi(options => {
3124
options.Namespace = "api/v1";

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
5-
using Microsoft.Extensions.Logging;
65
using JsonApiDotNetCoreExample.Data;
76
using Microsoft.EntityFrameworkCore;
87
using JsonApiDotNetCore.Extensions;
98
using System;
10-
using Microsoft.Extensions.Logging.Debug;
119

1210
namespace JsonApiDotNetCoreExample
1311
{
@@ -27,19 +25,12 @@ public Startup(IWebHostEnvironment env)
2725

2826
public virtual void ConfigureServices(IServiceCollection services)
2927
{
30-
var loggerFactory = new LoggerFactory();
3128
services
32-
.AddSingleton<ILoggerFactory>(loggerFactory)
33-
.AddLogging(builder =>
34-
{
35-
builder.AddConsole();
36-
builder.AddConfiguration(Config.GetSection("Logging"));
37-
})
3829
.AddDbContext<AppDbContext>(options =>
3930
{
40-
options.UseLoggerFactory(new LoggerFactory(new[] { new DebugLoggerProvider() }))
41-
.EnableSensitiveDataLogging()
42-
.UseNpgsql(GetDbConnectionString(), options => options.SetPostgresVersion(new Version(9,6)));
31+
options
32+
.EnableSensitiveDataLogging()
33+
.UseNpgsql(GetDbConnectionString(), options => options.SetPostgresVersion(new Version(9,6)));
4334
}, ServiceLifetime.Transient)
4435
.AddJsonApi(options =>
4536
{

src/Examples/JsonApiDotNetCoreExample/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"Logging": {
66
"IncludeScopes": false,
77
"LogLevel": {
8-
"Default": "Warning",
8+
"Default": "Debug",
99
"System": "Warning",
1010
"Microsoft": "Warning",
1111
"Microsoft.EntityFrameworkCore": "Debug"

src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs

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

src/Examples/NoEntityFrameworkExample/Startup.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.AspNetCore.Hosting;
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
7-
using Microsoft.Extensions.Logging;
87
using NoEntityFrameworkExample.Services;
98
using Microsoft.EntityFrameworkCore;
109
using NoEntityFrameworkExample.Data;
@@ -31,11 +30,7 @@ public virtual void ConfigureServices(IServiceCollection services)
3130
{
3231
// Add framework services.
3332
var mvcBuilder = services.AddMvcCore();
34-
services.AddLogging(builder =>
35-
{
36-
builder.AddConfiguration(Configuration.GetSection("Logging"));
37-
builder.AddConsole();
38-
}).AddJsonApi(
33+
services.AddJsonApi(
3934
options => options.Namespace = "api/v1",
4035
resources: resources => resources.AddResource<TodoItem>("todoItems"),
4136
mvcBuilder: mvcBuilder

src/Examples/ReportsExample/Controllers/ReportsController.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
using Microsoft.AspNetCore.Mvc;
33
using JsonApiDotNetCore.Controllers;
44
using JsonApiDotNetCore.Services;
5-
using JsonApiDotNetCore.Configuration;
6-
using JsonApiDotNetCore.Internal.Contracts;
7-
5+
using JsonApiDotNetCore.Configuration;
6+
using Microsoft.Extensions.Logging;
7+
using ReportsExample.Models;
8+
89
namespace ReportsExample.Controllers
910
{
1011
[Route("api/[controller]")]
1112
public class ReportsController : BaseJsonApiController<Report, int>
1213
{
1314
public ReportsController(
14-
IJsonApiOptions jsonApiOptions,
15+
IJsonApiOptions jsonApiOptions,
16+
ILoggerFactory loggerFactory,
1517
IGetAllService<Report> getAll)
16-
: base(jsonApiOptions, getAll: getAll)
18+
: base(jsonApiOptions, loggerFactory, getAll)
1719
{ }
1820

1921
[HttpGet]

0 commit comments

Comments
 (0)