Example #1
0
 /// <summary>
 /// This creates the DbContextOptions  options for a SQL server database while capturing EF Core's logging output.
 /// Where the database name is formed using the appsetting's DefaultConnection with the class name and the calling method's name as as a prefix.
 /// That is, the database is unique to the calling method.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="callingClass">this should be this, i.e. the class you are in</param>
 /// <param name="logAction">This action is called with each log output</param>
 /// <param name="logToOptions">Optional: This allows you to define what logs you want and what format. Defaults to LogLevel.Information</param>
 /// <param name="builder">Optional: action that allows you to add extra options to the builder</param>
 /// <param name="callingMember">Do not use: this is filled in by compiler</param>
 /// <returns></returns>
 public static DbContextOptions <T> CreateUniqueMethodOptionsWithLogTo <T>(this object callingClass,
                                                                           Action <string> logAction,
                                                                           LogToOptions logToOptions = null, Action <DbContextOptionsBuilder <T> > builder = null,
                                                                           [CallerMemberName] string callingMember = "") where T : DbContext
 {
     return(CreateOptionWithDatabaseName <T>(callingClass, callingMember, builder)
            .AddLogTo(logAction, logToOptions)
            .Options);
 }
Example #2
0
        /// <summary>
        /// Created a Sqlite Options for in-memory database while using LogTo to get the EF Core logging output.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="logAction">This action is called with each log output</param>
        /// <param name="logToOptions">Optional: This allows you to define what logs you want and what format. Defaults to LogLevel.Information</param>
        /// <param name="builder">Optional: action that allows you to add extra options to the builder</param>
        /// <returns></returns>
        public static DbContextOptionsDisposable <T> CreateOptionsWithLogTo <T>(Action <string> logAction,
                                                                                LogToOptions logToOptions = null, Action <DbContextOptionsBuilder <T> > builder = null)
            where T : DbContext
        {
            if (logAction == null)
            {
                throw new ArgumentNullException(nameof(logAction));
            }

            return(new DbContextOptionsDisposable <T>(
                       SetupConnectionAndBuilderOptions(builder)
                       .AddLogTo(logAction, logToOptions)
                       .Options));
        }
Example #3
0
        /// <summary>
        /// This creates the DbContextOptions options for a SQL server database while capturing EF Core's logging output.
        /// The database name is formed using the appsetting's DefaultConnection with the class name as a prefix.
        /// That is, the database is unique to the object provided
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="callingClass">this should be this, i.e. the class you are in</param>
        /// <param name="logAction">This action is called with each log output</param>
        /// <param name="logToOptions">Optional: This allows you to define what logs you want and what format. Defaults to LogLevel.Information</param>
        /// <param name="builder">Optional: action that allows you to add extra options to the builder</param>
        /// <returns></returns>
        public static DbContextOptions <T> CreateUniqueClassOptionsWithLogTo <T>(this object callingClass,
                                                                                 Action <string> logAction,
                                                                                 LogToOptions logToOptions = null, Action <DbContextOptionsBuilder <T> > builder = null)
            where T : DbContext
        {
            if (logAction == null)
            {
                throw new ArgumentNullException(nameof(logAction));
            }

            return(CreateOptionWithDatabaseName <T>(callingClass, null, builder)
                   .AddLogTo(logAction, logToOptions)
                   .Options);
        }
Example #4
0
        /// <summary>
        /// This creates the DbContextOptions options for a Azure Cosmos DB Emulator database while capturing EF Core's logging output.
        /// The name of the database is the type name of the callingClass parameter (normally this) plus the method name
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="callingClass">this object is used to get the name of the database</param>
        /// <param name="logAction">This action is called with each log output</param>
        /// <param name="logToOptions">Optional: This allows you to define what logs you want and what format. Defaults to LogLevel.Information</param>
        /// <param name="builder">Optional: action that allows you to add extra options to the builder</param>
        /// <param name="callingMember">Do not use: this is filled in by compiler</param>
        /// <returns></returns>
        public static DbContextOptions <T> CreateUniqueMethodCosmosDbEmulatorWithLogTo <T>(this object callingClass,
                                                                                           Action <string> logAction,
                                                                                           LogToOptions logToOptions = null, Action <DbContextOptionsBuilder <T> > builder = null,
                                                                                           [CallerMemberName] string callingMember = "") where T : DbContext
        {
            var databaseName = callingClass.GetType().Name;

            databaseName += callingMember;
            return(databaseName.CreateOptionWithDatabaseName <T>(builder).Options);
        }