1212using System . Text ;
1313using System . Threading . Tasks ;
1414using CodeFactory . Automation . Standard . NDF . Logic ;
15-
15+ using Microsoft . Extensions . Logging ;
1616
1717namespace CodeFactory . Automation . Data . Sql . EF . Logic
1818{
19- /// <summary>
19+ /// <summary>
2020 /// Automation logic to create or update a data repository that supports a target entity hosted in Entity Framework.
2121 /// </summary>
2222 public static class RepositoryAutomation
@@ -32,6 +32,8 @@ public static class RepositoryAutomation
3232 /// <param name="contractProject">Project the contract will be added to.</param>
3333 /// <param name="contractFolder">Optional, project folder contracts will be stored in, default is null.</param>
3434 /// <param name="additionalContractNamespaces">Optional, additional namespaces to add to the contract definition, default is null.</param>
35+ /// <param name="loggerFieldName">Optional, the name of the logger field if logging is supported, default value is '_logger'</param>
36+ /// <param name="logLevel">Optional, the target logging level for logging messages, default value is Information.</param>
3537 /// <param name="repoProject">Repository project.</param>
3638 /// <param name="contextClass">EF context class for accessing entity framework.</param>
3739 /// <param name="useNDF">Optional, flag that determines if the NDF libraries are used, default true.</param>
@@ -43,7 +45,7 @@ public static class RepositoryAutomation
4345 public static async Task < CsClass > RefreshEFRepositoryAsync ( this IVsActions source , CsClass efEntity , VsProject repoProject ,
4446 VsProject contractProject , CsClass poco , CsClass contextClass , bool useNDF = true , bool supportLogging = true , VsProjectFolder repoFolder = null , VsProjectFolder contractFolder = null ,
4547 string namePrefix = null , string nameSuffix = null , List < ManualUsingStatementNamespace > additionRepositoryNamespaces = null ,
46- List < ManualUsingStatementNamespace > additionalContractNamespaces = null )
48+ List < ManualUsingStatementNamespace > additionalContractNamespaces = null , string loggerFieldName = "_logger" , LogLevel logLevel = LogLevel . Information )
4749 {
4850
4951 if ( source == null )
@@ -188,13 +190,14 @@ private static async Task<CsInterface> CreateRepositoryContractAsync(this IVsAct
188190 /// <param name="supportLogging">Optional, flag that determines if logging is supported, default true.</param>
189191 /// <param name="repoFolder">Optional, project folder the repositories are stored in, default is null.</param>
190192 /// <param name="additionRepositoryNamespaces">Optional, list of additional namespaces to update the repository with.</param>
193+ /// <param name="loggerFieldName">Optional, name of the logger field if logging is supported, default is '_logger'</param>
191194 /// <param name="namePrefix">Optional, prefix to assign to the name of the repository, default is null.</param>
192195 /// <param name="nameSuffix">Optional, suffix to assign to the name of the repository, default is null.</param>
193196 /// <returns>Source for the created repository.</returns>
194197 /// <exception cref="CodeFactoryException">Raised if required data is missing to create the repository.</exception>
195198 private static async Task < CsSource > CreateEFRepositoryAsync ( this IVsActions source , CsClass efEntity , VsProject repoProject ,
196199 CsInterface repoContract , CsClass poco , CsClass contextClass , bool useNDF = true , bool supportLogging = true , VsProjectFolder repoFolder = null ,
197- string namePrefix = null , string nameSuffix = null , List < ManualUsingStatementNamespace > additionRepositoryNamespaces = null )
200+ string namePrefix = null , string nameSuffix = null , List < ManualUsingStatementNamespace > additionRepositoryNamespaces = null , string loggerFieldName = "_logger" )
198201 {
199202 if ( source == null )
200203 throw new CodeFactoryException ( "CodeFactory automation was not provided, cannot create the repository." ) ;
@@ -266,7 +269,7 @@ private static async Task<CsSource> CreateEFRepositoryAsync(this IVsActions sour
266269 repoFormatter . AppendCodeLine ( 2 , "/// <summary>" ) ;
267270 repoFormatter . AppendCodeLine ( 2 , "/// Logger used by the repository." ) ;
268271 repoFormatter . AppendCodeLine ( 2 , "/// </summary>" ) ;
269- repoFormatter . AppendCodeLine ( 2 , "private readonly ILogger _logger ;" ) ;
272+ repoFormatter . AppendCodeLine ( 2 , $ "private readonly ILogger { loggerFieldName } ;") ;
270273 repoFormatter . AppendCodeLine ( 2 ) ;
271274 repoFormatter . AppendCodeLine ( 2 , "/// <summary>" ) ;
272275 repoFormatter . AppendCodeLine ( 2 , "/// Creates a new instance of the repository." ) ;
@@ -275,7 +278,7 @@ private static async Task<CsSource> CreateEFRepositoryAsync(this IVsActions sour
275278 repoFormatter . AppendCodeLine ( 2 , "/// <param name=\" connection\" >The connection information for the repository.</param>" ) ;
276279 repoFormatter . AppendCodeLine ( 2 , $ "public { repoName } (ILogger<{ repoName } > logger, dataModel.IDBContextConnection<dataModel.{ contextClass . Name } > connection)") ;
277280 repoFormatter . AppendCodeLine ( 2 , "{" ) ;
278- repoFormatter . AppendCodeLine ( 3 , "_logger = logger;") ;
281+ repoFormatter . AppendCodeLine ( 3 , $ " { loggerFieldName } = logger;") ;
279282 repoFormatter . AppendCodeLine ( 3 , "_connectionString = connection.ConnectionString;" ) ;
280283 repoFormatter . AppendCodeLine ( 2 , "}" ) ;
281284 repoFormatter . AppendCodeLine ( 2 ) ;
@@ -304,11 +307,13 @@ private static async Task<CsSource> CreateEFRepositoryAsync(this IVsActions sour
304307 /// <param name="supportLogging">Optional, flag that determines if logging is supported, default true.</param>
305308 /// <param name="repoFolder">Optional, project folder the repositories are stored in, default is null.</param>
306309 /// <param name="additionRepositoryNamespaces">Optional, list of additional namespaces to update the repository with.</param>
310+ /// <param name="loggerFieldName">Optional, the name of the logger field if logging is supported, default value is '_logger'</param>
311+ /// <param name="logLevel">Optional, the target logging level for logging messages, default value is Information.</param>
307312 /// <returns></returns>
308313 /// <exception cref="CodeFactoryException"></exception>
309314 private static async Task < CsClass > UpdateEFRepositoryAsync ( this IVsActions source , CsClass efEntity , VsProject repoProject ,
310315 CsSource repoSource , CsInterface repoContract , CsClass poco , CsClass contextClass , bool useNDF = true , bool supportLogging = true , VsProjectFolder repoFolder = null ,
311- List < ManualUsingStatementNamespace > additionRepositoryNamespaces = null )
316+ List < ManualUsingStatementNamespace > additionRepositoryNamespaces = null , string loggerFieldName = "_logger" , LogLevel logLevel = LogLevel . Information )
312317 {
313318 if ( source == null )
314319 throw new CodeFactoryException ( "CodeFactory automation was not provided, cannot update the repository." ) ;
@@ -355,8 +360,8 @@ private static async Task<CsClass> UpdateEFRepositoryAsync(this IVsActions sourc
355360
356361 if ( supportLogging )
357362 {
358- if ( useNDF ) logFormatter = new LoggerBlockNDF ( "_logger" ) ;
359- else logFormatter = new LoggerBlockMicrosoft ( "_logger" ) ;
363+ if ( useNDF ) logFormatter = new LoggerBlockNDF ( loggerFieldName ) ;
364+ else logFormatter = new LoggerBlockMicrosoft ( loggerFieldName ) ;
360365 }
361366
362367 var boundCheckBlocks = new List < IBoundsCheckBlock > ( ) ;
@@ -419,7 +424,7 @@ private static async Task<CsClass> UpdateEFRepositoryAsync(this IVsActions sourc
419424 injectFormatter . AppendCodeLine ( 0 , "}" ) ;
420425
421426 string syntax = injectFormatter . ReturnSource ( ) ;
422- await methodBuilder . InjectMethodAsync ( missingMethod , repoManager , 2 , syntax : syntax ) ;
427+ await methodBuilder . InjectMethodAsync ( missingMethod , repoManager , 2 , syntax : syntax , defaultLogLevel : logLevel ) ;
423428
424429 injectFormatter . ResetFormatter ( ) ;
425430 }
0 commit comments