xBimTeam / XbimEssentials

A .NET library to work with data in the IFC format. This is the core component of the Xbim Toolkit
https://xbimteam.github.io/
Other
485 stars 172 forks source link

Remove static logging and replacing with DI-friendly system #483

Closed andyward closed 1 year ago

andyward commented 1 year ago

…DI approach for #455

Attempted as much as possible to break few contracts, and provide forwarding to new implementation

Removed dependence on Microsoft.Extensions.Logging, but added Microsoft.Extensions.DependencyInjection

Works by introducing an internal DI ServiceCollection and ServiceProvider available via a singleton resource. This collection can be configured by a consumer, but services will work out the box without any config (just with Null Logging)

Consumers can add own logging, either with AddLogging(), or by telling xbim to UseLoggerFactory(logFactory).

Removed public Setter ILoggers. Because we're not fully in control of DI composition favoured approach is to inject/pass in ILoggerFactory to key roots. We're using ServiceLocator anti-pattern in other cases as a last resort.

ModelProvider & Factory has been re-worked to actually work with DI.

One subtle change - Previosuly Heuristic ModelProvider kicked in if Esent was loaded. Now that needs to be set explicitly when configuring the app.

i.e. XbimServices.Current.ConfigureServices(s => s.AddXbimToolkit(opt => opt.UseHeuristicModel()));

Legacy 'DI lite' concepts like IfcStore.ModelProviderFactory.UseHeuristicModelProvider() have been remapped to a new approach and Obsoleted.

TODO: many tests use the old obsoleted overloads, which forward onto new . All tests pass (with one flaky IfcStore ModelProvider test to resolve)