/// <summary>
        ///     Adds a sink that writes log events as documents using DocSet.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="connectionsStringName">The connectionsString name.</param>
        /// <param name="schemaName">The name of the database schema.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="propertiesAsTags">The properties as tags.</param>
        /// <param name="propertiesWhiteList">The properties filter.</param>
        /// <param name="options">The options.</param>
        /// <param name="indexMap">The index map.</param>
        /// <param name="enableDocSetLogging">if set to <c>true</c> [enable document set logging].</param>
        /// <returns>
        ///     Logger configuration, allowing configuration to continue.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration DocSet(
            this LoggerSinkConfiguration loggerConfiguration,
            string connectionsStringName,
            string schemaName = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchPostingLimit = 50,
            TimeSpan? period = null,
            IFormatProvider formatProvider = null,
            IEnumerable<string> propertiesAsTags = null,
            IEnumerable<string> propertiesWhiteList = null,
            IStorageOptions options = null,
            List<IIndexMap<LogEvent>> indexMap = null,
            bool enableDocSetLogging = false)
        {
            if (loggerConfiguration == null) throw new ArgumentNullException(nameof(loggerConfiguration));
            if (connectionsStringName == null) throw new ArgumentNullException(nameof(connectionsStringName));

            if (options == null)
                options = new StorageOptions(
                    new ConnectionStrings().Get(connectionsStringName),
                    schemaName: schemaName);

            if (indexMap == null)
                indexMap = new List<IIndexMap<LogEvent>>
                {
                    new IndexMap<LogEvent>(nameof(LogEvent.Level), i => i.Level),
                    new IndexMap<LogEvent>(nameof(LogEvent.Timestamp), i => i.Timestamp.ToString("s"))
                };

            try
            {
                return loggerConfiguration.Sink(
                    new DocSetSink(
                        new DocStorage<LogEvent>(new SqlConnectionFactory(), options, new SqlBuilder(),
                            new JsonNetSerializer(), null /*new Md5Hasher()*/, indexMap),
                        batchPostingLimit,
                        period ?? DocSetSink.DefaultPeriod,
                        formatProvider,
                        propertiesAsTags ?? new[] {"CorrelationId", "App" /*, "SourceContext"*/},
                        propertiesWhiteList ??
                        new[] {/*"CorrelationId",*/ "App", "SourceContext" /*"Message", "DocSetKey"*/}),
                    restrictedToMinimumLevel);
            }
            catch (DbException)
            {
                // could not connect to the db, use a null docstorage instead
                return loggerConfiguration.Sink(
                    new DocSetSink(
                        null,
                        batchPostingLimit,
                        period ?? DocSetSink.DefaultPeriod,
                        formatProvider,
                        propertiesAsTags ?? new[] {"CorrelationId", "App" /*, "SourceContext"*/},
                        propertiesWhiteList ??
                        new[] {/*"CorrelationId",*/ "App", "SourceContext" /*"Message", "DocSetKey"*/}),
                    restrictedToMinimumLevel);
            }
        }
        protected StorageContext(IStorageOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Options = options;
        }
Example #3
0
        /// <summary>
        /// Initializes a new <see cref="TableStorageContext"/> instance.
        /// </summary>
        public TableStorageContext(IStorageOptions options, ITableEntityMapper mapperRegistry = null) : base(options)
        {
            var comparer = new TableEntityEqualityComparer <ITableEntity>();

            tableEntityMapper = mapperRegistry ?? new AggregateTableEntityMapper(new RegistryTableEntityMapper(), new AttributeTableEntityMapper(), new TypeNameTableEntityMapper());
            newEntities       = new HashSet <ITableEntity>(comparer);
            dirtyEntities     = new HashSet <ITableEntity>(comparer);
            removedEntities   = new HashSet <ITableEntity>(comparer);
            cleanEntities     = new HashSet <ITableEntity>(comparer);
        }
Example #4
0
        internal Mock <TableStorageContext> CreateStorageContextMock(IStorageOptions options = null)
        {
            if (options == null)
            {
                options = new CreateOwnContextTableStorageOptions();
            }
            var storageContextMock = new Mock <TableStorageContext>(options, null)
            {
                CallBase = false
            };

            return(storageContextMock);
        }
Example #5
0
        public DocStorage(IDbConnectionFactory connectionFactory, IStorageOptions options, ISqlBuilder sqlBuilder,
                          ISerializer serializer = null, IHasher hasher = null, IEnumerable <IIndexMap <TDoc> > indexMap = null)
        {
            if (connectionFactory == null)
            {
                throw new ArgumentNullException(nameof(connectionFactory));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (sqlBuilder == null)
            {
                throw new ArgumentNullException(nameof(sqlBuilder));
            }

            ConnectionFactory = connectionFactory;
            Options           = options;

            var builder = new SqlConnectionStringBuilder(options.ConnectionString);

            options.DataSource = builder.DataSource;
            if (!builder.InitialCatalog.IsNullOrEmpty()) // overrule database name if provided in connectionstring
            {
                Options.DatabaseName = builder.InitialCatalog;
            }

            SqlBuilder = sqlBuilder;
            Serializer = serializer ?? new JsonNetSerializer();
            Hasher     = hasher;// ?? new Md5Hasher();
            TableName  = options.GetTableName <TDoc>();
            IndexMaps  = indexMap.NullToEmpty().ToList().Where(im => im != null).OrderBy(i => i.Name);

            if (Options.EnableLogging)
            {
                Log.Debug($"datasource: {Options.DataSource}");
                Log.Debug($"database: {Options.DatabaseName}");
                Log.Debug($"schema: {Options.SchemaName}");
                Log.Debug($"table: {TableName}");
                IndexMaps.ForEach(
                    im => Log.Debug($"index map: {typeof(TDoc).Name} > {im.Name} [{im.Description}]"));
                //Log.Debug($"connection: {Options.ConnectionString}");
            }

            Initialize();
        }
Example #6
0
        public async ValueTask <IStorageOptions> GetConfig(IServiceProvider serviceProvider, PrimaryKey primaryKey)
        {
            if (parameter.Singleton)
            {
                if (config == default)
                {
                    await seamphore.WaitAsync();

                    try
                    {
                        if (config == default)
                        {
                            config           = generator(serviceProvider, primaryKey, parameter);
                            config.Singleton = parameter.Singleton;
                            var initTask = config.Build();
                            if (!initTask.IsCompletedSuccessfully)
                            {
                                await initTask;
                            }
                        }
                    }
                    finally
                    {
                        seamphore.Release();
                    }
                }
                return(config);
            }
            else
            {
                var newConfig = generator(serviceProvider, primaryKey, parameter);
                newConfig.Singleton = parameter.Singleton;
                var initTask = newConfig.Build();
                if (!initTask.IsCompletedSuccessfully)
                {
                    await initTask;
                }
                return(newConfig);
            }
        }
Example #7
0
 private void EnsureOpenConnection(IDbConnection conn, IStorageOptions options)
 {
     try
     {
         conn.Open();
     }
     catch (SqlException e) // cannot login (catalog does not exist?), try without catalog in the conn string
     {
         var builder = new SqlConnectionStringBuilder(options.ConnectionString);
         if (builder.InitialCatalog.IsNullOrEmpty())
         {
             throw;
         }
         if (options.EnableLogging)
         {
             Log.Warn($"fallback to db connectionstring with an empty initial catalog: {e.Message}");
         }
         builder.InitialCatalog   = "";
         options.ConnectionString = builder.ConnectionString;
         conn.ConnectionString    = options.ConnectionString;
         conn.Open();
     }
 }
Example #8
0
     protected virtual void EnsureSchema(IStorageOptions options)
     {
         if (string.IsNullOrEmpty(options.SchemaName))
         {
             return;
         }
         using (var conn = CreateConnection())
         {
             conn.Open();
             if (Options.EnableLogging)
             {
                 Log.Debug($"{options.SchemaName} ensure schema [{conn.Database}]");
             }
             if (conn.Query <string>($@"
 {SqlBuilder.BuildUseDatabase(options.DatabaseName)}
 SELECT QUOTENAME(SCHEMA_NAME) AS Name
 FROM INFORMATION_SCHEMA.SCHEMATA")
                 .Any(t =>
                      t.Equals($"[{options.SchemaName}]",
                               StringComparison.OrdinalIgnoreCase)))
             {
                 return;
             }
             try
             {
                 conn.Execute($"CREATE SCHEMA [{options.SchemaName}] AUTHORIZATION dbo");
             }
             catch (SqlException e)
             {
                 // swallog
                 if (Options.EnableLogging)
                 {
                     Log.Warn($"create schema: {e.Message}");
                 }
             }
         }
     }
Example #9
0
        protected virtual void EnsureDatabase(IStorageOptions options)
        {
            if (string.IsNullOrEmpty(options.DatabaseName))
            {
                return;
            }
            using (var conn = CreateConnection())
            {
                if (Options.EnableLogging)
                {
                    Log.Debug($"{options.DatabaseName} ensure database [{conn.Database}]");
                }

                EnsureOpenConnection(conn, options);

                if (conn.Query <string>($@"
    SELECT *
    FROM sys.databases
    WHERE name='{options.DatabaseName}'")
                    .Any())
                {
                    return;
                }
                try
                {
                    conn.Execute($"CREATE DATABASE [{options.DatabaseName}]");
                }
                catch (SqlException e)
                {
                    // swallog
                    if (Options.EnableLogging)
                    {
                        Log.Warn($"create database: {e.Message}");
                    }
                }
            }
        }
Example #10
0
 protected override void EnsureSchema(IStorageOptions options)
 {
     return; // not needed
 }
Example #11
0
 public SqliteDocStorage(IDbConnectionFactory connectionFactory, IStorageOptions options, ISqlBuilder sqlBuilder,
                         ISerializer serializer, IHasher hasher = null, IEnumerable <IIndexMap <TDoc> > indexMap = null)
     : base(connectionFactory, options, sqlBuilder, serializer, hasher, indexMap)
 {
 }
Example #12
0
 public ValueTask <IArchiveStorage <PrimaryKey, State> > CreateArchiveStorage <PrimaryKey, State>(IStorageOptions config, PrimaryKey grainId)
     where State : class, new()
 {
     if (config.Singleton)
     {
         var storage = ArchiveStorageDict.GetOrAdd(config, key =>
         {
             return(new ArchiveStorage <PrimaryKey, State>(serviceProvider, serializer, config as StorageOptions));
         });
         return(new ValueTask <IArchiveStorage <PrimaryKey, State> >(storage as IArchiveStorage <PrimaryKey, State>));
     }
     else
     {
         return(new ValueTask <IArchiveStorage <PrimaryKey, State> >(new ArchiveStorage <PrimaryKey, State>(serviceProvider, serializer, config as StorageOptions)));
     }
 }
Example #13
0
 public ValueTask <ISnapshotStorage <PrimaryKey, State> > CreateSnapshotStorage <PrimaryKey, State>(IStorageOptions config, PrimaryKey grainId)
     where State : class, new()
 {
     if (config.Singleton)
     {
         var storage = stateStorageDict.GetOrAdd(config, key =>
         {
             return(new SnapshotStorage <PrimaryKey, State>(serializer, config as StorageOptions));
         });
         return(new ValueTask <ISnapshotStorage <PrimaryKey, State> >(storage as SnapshotStorage <PrimaryKey, State>));
     }
     else
     {
         return(new ValueTask <ISnapshotStorage <PrimaryKey, State> >(new SnapshotStorage <PrimaryKey, State>(serializer, config as StorageOptions)));
     }
 }
Example #14
0
 public ValueTask <IEventStorage <PrimaryKey> > CreateEventStorage <PrimaryKey>(IStorageOptions config, PrimaryKey grainId)
 {
     if (config.Singleton)
     {
         var storage = eventStorageDict.GetOrAdd(config, key =>
         {
             return(new EventStorage <PrimaryKey>(serviceProvider, config as StorageOptions));
         });
         return(new ValueTask <IEventStorage <PrimaryKey> >(storage as EventStorage <PrimaryKey>));
     }
     else
     {
         return(new ValueTask <IEventStorage <PrimaryKey> >(new EventStorage <PrimaryKey>(serviceProvider, config as StorageOptions)));
     }
 }
        /// <summary>
        ///     Adds a sink that writes log events as documents using DocSet.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="connectionsStringName">The connectionsString name.</param>
        /// <param name="schemaName">The name of the database schema.</param>
        /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="propertiesAsTags">The properties as tags.</param>
        /// <param name="propertiesWhiteList">The properties filter.</param>
        /// <param name="options">The options.</param>
        /// <param name="indexMap">The index map.</param>
        /// <param name="enableDocSetLogging">if set to <c>true</c> [enable document set logging].</param>
        /// <returns>
        ///     Logger configuration, allowing configuration to continue.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration DocSet(
            this LoggerSinkConfiguration loggerConfiguration,
            string connectionsStringName,
            string schemaName = null,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            int batchPostingLimit                    = 50,
            TimeSpan?period                          = null,
            IFormatProvider formatProvider           = null,
            IEnumerable <string> propertiesAsTags    = null,
            IEnumerable <string> propertiesWhiteList = null,
            IStorageOptions options                  = null,
            List <IIndexMap <LogEvent> > indexMap    = null,
            bool enableDocSetLogging                 = false)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (connectionsStringName == null)
            {
                throw new ArgumentNullException(nameof(connectionsStringName));
            }

            if (options == null)
            {
                options = new StorageOptions(
                    new ConnectionStrings().Get(connectionsStringName),
                    schemaName: schemaName);
            }

            if (indexMap == null)
            {
                indexMap = new List <IIndexMap <LogEvent> >
                {
                    new IndexMap <LogEvent>(nameof(LogEvent.Level), i => i.Level),
                    new IndexMap <LogEvent>(nameof(LogEvent.Timestamp), i => i.Timestamp.ToString("s"))
                }
            }
            ;

            try
            {
                return(loggerConfiguration.Sink(
                           new DocSetSink(
                               new DocStorage <LogEvent>(new SqlConnectionFactory(), options, new SqlBuilder(),
                                                         new JsonNetSerializer(), null /*new Md5Hasher()*/, indexMap),
                               batchPostingLimit,
                               period ?? DocSetSink.DefaultPeriod,
                               formatProvider,
                               propertiesAsTags ?? new[] { "CorrelationId", "App" /*, "SourceContext"*/ },
                               propertiesWhiteList ??
                               new[] { /*"CorrelationId",*/ "App", "SourceContext" /*"Message", "DocSetKey"*/ }),
                           restrictedToMinimumLevel));
            }
            catch (DbException)
            {
                // could not connect to the db, use a null docstorage instead
                return(loggerConfiguration.Sink(
                           new DocSetSink(
                               null,
                               batchPostingLimit,
                               period ?? DocSetSink.DefaultPeriod,
                               formatProvider,
                               propertiesAsTags ?? new[] { "CorrelationId", "App" /*, "SourceContext"*/ },
                               propertiesWhiteList ??
                               new[] { /*"CorrelationId",*/ "App", "SourceContext" /*"Message", "DocSetKey"*/ }),
                           restrictedToMinimumLevel));
            }
        }
    }