Exemplo n.º 1
0
        /// <summary>
        /// Use SqlSugar for Cosmos.Logging
        /// </summary>
        /// <param name="integration"></param>
        /// <param name="settingAct"></param>
        /// <param name="configAction"></param>
        /// <returns></returns>
        public static DatabaseIntegration UseSqlSugar(
            this DatabaseIntegration integration,
            Action <SqlSugarEnricherOptions> settingAct = null,
            Action <IConfiguration, SqlSguarEnricherConfiguration> configAction = null)
        {
            var settings = new SqlSugarEnricherOptions();

            settingAct?.Invoke(settings);

            void InternalAction(IConfiguration conf, SqlSguarEnricherConfiguration sink, LoggingConfiguration configuration)
            {
                configAction?.Invoke(conf, sink);
                if (configuration?.LogLevel != null)
                {
                    var @namespace = $"{typeof(SqlSugarClient).Namespace}.*";
                    if (configuration.LogLevel.ContainsKey(@namespace))
                    {
                        configuration.LogLevel[@namespace] = GetExpectLevelName();
                    }
                    else
                    {
                        configuration.LogLevel.Add(@namespace, GetExpectLevelName());
                    }
                }
            }

            string GetExpectLevelName() => settings.MinimumLevel.HasValue ? settings.MinimumLevel.Value.GetName() : LogEventLevel.Verbose.GetName();

            return(UseSqlSugarCore(integration, Options.Create(settings), InternalAction));
        }
        private static DatabaseIntegration UseEntityFrameworkCore(DatabaseIntegration integration, IOptions <EfSinkOptions> settings,
                                                                  Action <IConfiguration, EfSinkConfiguration, LoggingConfiguration> config = null)
        {
            if (integration == null)
            {
                throw new ArgumentNullException(nameof(integration));
            }

            var serviceImpl = integration.ExposeServiceCollectionWrapper;

            if (serviceImpl != null)
            {
                serviceImpl.AddExtraSinkSettings <EfSinkOptions, EfSinkConfiguration>(settings.Value, (conf, sink, configuration) => config?.Invoke(conf, sink, configuration));
                serviceImpl.AddDependency(s => s.TryAdd(ServiceDescriptor.Singleton(settings)));
                serviceImpl.AddDependency(s => s.TryAdd(ServiceDescriptor.Singleton(typeof(EfInterceptorDescriptor),
                                                                                    provider => new EfInterceptorDescriptor(provider.GetService <ILoggingServiceProvider>(), settings))));

                serviceImpl.AddDependency(s => s.TryAdd(ServiceDescriptor.Singleton(typeof(EfIntegrationActivation),
                                                                                    provider => new EfIntegrationActivation(provider.GetService <EfInterceptorDescriptor>()))));

                RegisterCoreComponentsTypes();
            }

            return(integration);
        }
        /// <summary>
        /// Add database integration
        /// </summary>
        /// <param name="service"></param>
        /// <param name="integrationAct"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static ILogServiceCollection AddDatabaseIntegration(this ILogServiceCollection service, Action <DatabaseIntegration> integrationAct)
        {
            if (integrationAct == null)
            {
                throw new ArgumentNullException(nameof(integrationAct));
            }
            var integration = new DatabaseIntegration(service);

            integrationAct.Invoke(integration);
            return(service);
        }
        /// <summary>
        /// Use Cosmos Logging EntityFrameworkCore enricher
        /// </summary>
        /// <param name="integration"></param>
        /// <param name="settingAct"></param>
        /// <param name="configAction"></param>
        /// <returns></returns>
        public static DatabaseIntegration UseEntityFrameworkCore(
            this DatabaseIntegration integration,
            Action <EfCoreEnricherOptions> settingAct = null,
            Action <IConfiguration, EfCoreEnricherConfiguration> configAction = null)
        {
            var settings = new EfCoreEnricherOptions();

            settingAct?.Invoke(settings);

            void InternalAction(IConfiguration conf, EfCoreEnricherConfiguration sink, LoggingConfiguration configuration)
            {
                configAction?.Invoke(conf, sink);
                if (configuration?.LogLevel != null)
                {
                    AddNamespace($"{typeof(DbContext).Namespace}.*", GetExpectLevelName());
                    AddNamespace($"{typeof(DbContextOptionsBuilder).FullName}", GetExpectLevelName());
                    AddNamespace(DbLoggerCategory.Database.Command.Name, GetExpectLevelName());
                    AddNamespace(DbLoggerCategory.Query.Name, GetExpectLevelName());
                    AddNamespace(DbLoggerCategory.Update.Name, GetExpectLevelName());
                }

                void AddNamespace(string @namespace, string expectLevelName)
                {
                    if (string.IsNullOrWhiteSpace(@namespace))
                    {
                        return;
                    }
                    if (configuration.LogLevel.ContainsKey(@namespace))
                    {
                        configuration.LogLevel[@namespace] = expectLevelName;
                    }
                    else
                    {
                        configuration.LogLevel.Add(@namespace, expectLevelName);
                    }
                }

                string GetExpectLevelName() => settings.MinimumLevel.HasValue
                    ? settings.MinimumLevel.Value.GetName()
                    : LogEventLevel.Verbose.GetName();
            }

            return(UseEntityFrameworkCoreInternal(integration, Options.Create(settings), InternalAction));
        }
        private static DatabaseIntegration UseEntityFrameworkCoreInternal(
            DatabaseIntegration integration,
            IOptions <EfCoreEnricherOptions> settings,
            Action <IConfiguration, EfCoreEnricherConfiguration, LoggingConfiguration> config = null)
        {
            var services = integration.ExposeServiceCollectionWrapper;

            if (services != null)
            {
                services.AddExtraSinkSettings(settings.Value, config);
                services.AddDependency(s => s.TryAddSingleton(settings));

                RegisterEntityFrameworkCoreInterceptor(services, settings);

                RegisterCoreComponentsTypes();
            }

            return(integration);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Add NHibernate for Cosmos.Logging
        /// </summary>
        /// <param name="integration"></param>
        /// <param name="settingAct"></param>
        /// <param name="configAction"></param>
        /// <returns></returns>
        public static DatabaseIntegration UseNHibernate(
            this DatabaseIntegration integration,
            Action <NhEnricherOptions> settingAct = null,
            Action <IConfiguration, NhEnricherConfiguration> configAction = null)
        {
            var settings = new NhEnricherOptions();

            settingAct?.Invoke(settings);

            void InternalAction(IConfiguration conf, NhEnricherConfiguration sink, LoggingConfiguration configuration)
            {
                configAction?.Invoke(conf, sink);
                if (configuration?.LogLevel != null)
                {
                    AddNamespace($"{typeof(ISessionFactory).Namespace}.*", GetExpectLevelName());
                }

                void AddNamespace(string @namespace, string expectLevelName)
                {
                    if (string.IsNullOrWhiteSpace(@namespace))
                    {
                        return;
                    }
                    if (configuration.LogLevel.ContainsKey(@namespace))
                    {
                        configuration.LogLevel[@namespace] = expectLevelName;
                    }
                    else
                    {
                        configuration.LogLevel.Add(@namespace, expectLevelName);
                    }
                }

                string GetExpectLevelName() => settings.MinimumLevel.HasValue
                    ? settings.MinimumLevel.Value.GetName()
                    : LogEventLevel.Verbose.GetName();
            }

            return(UseNHibernateCore(integration, Options.Create(settings), InternalAction));
        }
        private static DatabaseIntegration UseNHibernateCore(DatabaseIntegration integration, IOptions <NhSinkOptions> settings,
                                                             Action <IConfiguration, NhSinkConfiguration, LoggingConfiguration> config = null)
        {
            if (integration == null)
            {
                throw new ArgumentNullException(nameof(integration));
            }

            var serviceImpl = integration.ExposeServiceCollectionWrapper;

            if (serviceImpl != null)
            {
                serviceImpl.AddExtraSinkSettings <NhSinkOptions, NhSinkConfiguration>(settings.Value, (conf, sink, configuration) => config?.Invoke(conf, sink, configuration));
                serviceImpl.AddDependency(s => s.TryAdd(ServiceDescriptor.Singleton(settings)));
                serviceImpl.AddDependency(s => s.TryAdd(ServiceDescriptor.Singleton(provider =>
                                                                                    new StaticServiceResolveInitialization(provider.GetRequiredService <ILoggingServiceProvider>(), settings.Value))));

                RegisterCoreComponentsTypes();
            }

            return(integration);
        }
Exemplo n.º 8
0
        private static DatabaseIntegration UseSqlSugarCore(
            DatabaseIntegration integration,
            IOptions <SqlSugarEnricherOptions> settings,
            Action <IConfiguration, SqlSguarEnricherConfiguration, LoggingConfiguration> config = null)
        {
            if (integration is null)
            {
                throw new ArgumentNullException(nameof(integration));
            }

            var services = integration.ExposeServiceCollectionWrapper;

            if (services != null)
            {
                services.AddExtraSinkSettings(settings.Value, config);
                services.AddDependency(s => s.TryAddSingleton(settings));

                RegisterSqlSugarInterceptor(services, settings);

                RegisterCoreComponentsTypes();
            }

            return(integration);
        }