88using System . Linq ;
99using Microsoft . Extensions . Configuration ;
1010using Xtensive . Collections ;
11+ using Xtensive . Core ;
1112using Xtensive . Orm . Configuration . Options ;
1213
1314namespace Xtensive . Orm . Configuration . Internals
1415{
15- internal interface IConfigurationSectionParser
16+ internal interface IConfigurationSectionParser < TConfiguration >
1617 {
17- DomainConfiguration Parse ( IConfiguration configuration ) ;
18+ TConfiguration Parse ( IConfiguration configuration ) ;
1819
19- DomainConfiguration Parse ( IConfigurationRoot configuration , string sectionName ) ;
20+ TConfiguration Parse ( IConfigurationRoot configuration , string sectionName ) ;
21+ }
22+
23+ internal sealed class LoggingConfigurationParser : IConfigurationSectionParser < LoggingConfiguration >
24+ {
25+ private const string LoggingSectionName = "Logging" ;
26+ private const string LogsSectionName = "Logs" ;
27+
28+ private const string ProviderElementName = "Provider" ;
29+ private const string LogElementName = "Log" ;
30+ private const string LogSourceElementName = "Source" ;
31+ private const string LogTargerElementName = "Target" ;
32+
33+ public LoggingConfiguration Parse ( IConfiguration configuration )
34+ {
35+ ArgumentValidator . EnsureArgumentNotNull ( configuration , nameof ( configuration ) ) ;
36+
37+ if ( configuration is IConfigurationRoot configurationRoot ) {
38+ return Parse ( configurationRoot , WellKnown . DefaultConfigurationSection ) ;
39+ }
40+ else if ( configuration is IConfigurationSection configurationSection ) {
41+ return Parse ( configurationSection ) ;
42+ }
43+ throw new InvalidOperationException ( "" ) ;
44+ }
45+
46+ public LoggingConfiguration Parse ( IConfigurationRoot configuration , string sectionName )
47+ {
48+ ArgumentValidator . EnsureArgumentNotNull ( configuration , nameof ( configuration ) ) ;
49+ ArgumentValidator . EnsureArgumentNotNullOrEmpty ( sectionName , nameof ( sectionName ) ) ;
50+
51+ return Parse ( configuration . GetSection ( sectionName ) ) ;
52+ }
53+
54+ public LoggingConfiguration Parse ( IConfigurationSection configurationSection )
55+ {
56+ ArgumentValidator . EnsureArgumentNotNull ( configurationSection , nameof ( configurationSection ) ) ;
2057
21- DomainConfiguration Parse ( IConfigurationSection domainConfiguration , Dictionary < string , string > connectionStrings = null ) ;
58+ var ormConfigurationSection = configurationSection ;
2259
60+ var loggingSection = ormConfigurationSection . GetSection ( LoggingSectionName ) ;
61+
62+ if ( loggingSection != null && loggingSection . GetChildren ( ) . Any ( ) ) {
63+ var provider = loggingSection . GetSection ( ProviderElementName ) ? . Value ;
64+ var logsSection = loggingSection . GetSection ( LogsSectionName ) ;
65+ IConfigurationSection logElement ;
66+
67+ if ( logsSection != null && logsSection . GetChildren ( ) . Any ( ) ) {
68+ logElement = logsSection . GetSection ( LogElementName ) ;
69+ if ( logElement == null || ! logElement . GetChildren ( ) . Any ( ) ) {
70+ logElement = logsSection ;
71+ }
72+ }
73+ else {
74+ logElement = loggingSection . GetSection ( LogElementName ) ;
75+ }
76+
77+ var configuration = new LoggingConfiguration ( provider ) ;
78+
79+ foreach ( var logItem in logElement . GetSelfOrChildren ( ) ) {
80+ var source = logItem . GetSection ( LogSourceElementName ) . Value ;
81+ var target = logItem . GetSection ( LogTargerElementName ) . Value ;
82+ if ( source . IsNullOrEmpty ( ) || target . IsNullOrEmpty ( ) )
83+ throw new InvalidOperationException ( ) ;
84+ configuration . Logs . Add ( new LogConfiguration ( source , target ) ) ;
85+ }
86+
87+ return configuration ;
88+ }
89+
90+ throw new InvalidOperationException (
91+ string . Format ( Strings . ExSectionIsNotFoundInApplicationConfigurationFile , WellKnown . DefaultConfigurationSection ) ) ;
92+ }
2393 }
2494
2595 internal sealed class XmlDomainConfigParser : DomainConfigurationParser
2696 {
27-
2897 protected override void ProcessNamingConvention ( IConfigurationSection namingConventionSection , DomainConfigurationOptions domainConfiguratonOptions )
2998 {
3099 if ( namingConventionSection == null ) {
@@ -37,7 +106,8 @@ protected override void ProcessNamingConvention(IConfigurationSection namingConv
37106 if ( namingConvetion != null ) {
38107 var synonymsSection = namingConventionSection . GetSection ( NamespaceSynonymSectionName ) ;
39108 var synonyms = synonymsSection != null && synonymsSection . GetChildren ( ) . Any ( )
40- ? GetSelfOrChildren ( synonymsSection . GetSection ( SynonymElementName ) )
109+ ? synonymsSection . GetSection ( SynonymElementName )
110+ . GetSelfOrChildren ( )
41111 . Select ( s => s . Get < NamespaceSynonymOptions > ( ) )
42112 . Where ( ns => ns != null )
43113 . ToArray ( )
@@ -85,7 +155,8 @@ private bool TryProcessTypeRegistrationsWithAttributes(IConfigurationSection typ
85155 {
86156 var registrations = typesSection . GetSection ( OldStyleTypeRegistrationElementName ) ;
87157 if ( registrations != null && registrations . GetChildren ( ) . Any ( ) ) {
88- domainConfigurationOptions . Types = GetSelfOrChildren ( registrations )
158+ domainConfigurationOptions . Types = registrations
159+ . GetSelfOrChildren ( )
89160 . Select ( s => s . Get < TypeRegistrationOptions > ( ) )
90161 . Where ( tr => tr != null )
91162 . ToArray ( ) ;
@@ -101,7 +172,8 @@ private bool TryProcessTypeRegistrationsWithNodes(IConfigurationSection typesSec
101172 if ( registrations == null )
102173 return false ;
103174
104- domainConfigurationOptions . Types = GetSelfOrChildren ( registrations )
175+ domainConfigurationOptions . Types = registrations
176+ . GetSelfOrChildren ( )
105177 . Select ( s => s . Get < TypeRegistrationOptions > ( ) )
106178 . Where ( tr => tr != null )
107179 . ToArray ( ) ;
@@ -116,7 +188,7 @@ protected override void ProcessDatabases(IConfigurationSection databasesSection,
116188 var databaseElement = databasesSection . GetSection ( DatabaseElementName ) ;
117189 if ( databaseElement == null )
118190 return ;
119- foreach ( var section in GetSelfOrChildren ( databaseElement , true ) ) {
191+ foreach ( var section in databaseElement . GetSelfOrChildren ( true ) ) {
120192 var dbItem = section . Get < DatabaseOptions > ( ) ;
121193 if ( dbItem == null )
122194 continue ;
@@ -150,7 +222,7 @@ protected override void ProcessSessions(IConfigurationSection sessionsSection, O
150222 var sessionElement = sessionsSection . GetSection ( SessionElementName ) ;
151223 if ( sessionElement == null )
152224 return ;
153- foreach ( var section in GetSelfOrChildren ( sessionElement , true ) ) {
225+ foreach ( var section in sessionElement . GetSelfOrChildren ( true ) ) {
154226 var sessionItem = section . Get < SessionConfigurationOptions > ( ) ;
155227 if ( sessionItem == null )
156228 continue ;
@@ -166,36 +238,15 @@ private void ProcessCollectionOfOptions<TOption>(IConfigurationSection collectio
166238 var collectionElement = collectionSection . GetSection ( itemKey ) ;
167239 if ( collectionElement == null )
168240 return ;
169- foreach ( var item in GetSelfOrChildren ( collectionElement ) ) {
241+ foreach ( var item in collectionElement . GetSelfOrChildren ( ) ) {
170242 var optItem = item . Get < TOption > ( ) ;
171243 collection . Add ( optItem ) ;
172244 }
173245 }
174-
175- private IEnumerable < IConfigurationSection > GetSelfOrChildren ( IConfigurationSection section , bool requiredName = false )
176- {
177- var children = section . GetChildren ( ) . ToList ( ) ;
178- if ( children . Count > 0 ) {
179- if ( requiredName ) {
180- var anyItemWithName = children . Any ( i => i [ "name" ] != null ) ;
181- if ( anyItemWithName ) {
182- return children ;
183- }
184- }
185- var firstChild = children [ 0 ] ;
186- var isIndexed = firstChild . Key == "0" ;
187- if ( isIndexed )
188- return children ;
189- else
190- return EnumerableUtils . One ( section ) ;
191- }
192- return children ;
193- }
194246 }
195247
196248 internal class FromJsonToDomainConfigurationParser : DomainConfigurationParser
197249 {
198-
199250 protected override void ProcessNamingConvention ( IConfigurationSection namingConventionSection , DomainConfigurationOptions domainConfiguratonOptions )
200251 {
201252 if ( namingConventionSection == null || ! namingConventionSection . GetChildren ( ) . Any ( ) )
@@ -207,7 +258,7 @@ protected override void ProcessNamingConvention(IConfigurationSection namingConv
207258 }
208259 }
209260
210- internal abstract class DomainConfigurationParser : IConfigurationSectionParser
261+ internal abstract class DomainConfigurationParser : IConfigurationSectionParser < DomainConfiguration >
211262 {
212263 protected const string RuleElementName = "Rule" ;
213264 protected const string KeyGeneratorElementName = "KeyGenerator" ;
0 commit comments