Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region AutoMapper
            services.AddAutoMapper(typeof(EntityToViewModelMappingProfile), typeof(ViewModelToEntityMappingProfile));
            #endregion

            #region Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(SwaggerName, new OpenApiInfo {
                    Title = "EasyLogger", Description = "日志记录教程", Version = "v1"
                });
                var basePath = AppContext.BaseDirectory;
                var xmlPath  = Path.Combine(basePath, "EasyLogger.Api.xml");
                c.IncludeXmlComments(xmlPath, true);
            });
            #endregion

            #region SqlSugar
            services.AddSingleton <IDynamicLinkBase, SqlSugarDynamicLink>();
            // 改造一下把 自己的注入部分封装起来
            var defaultDbPath = Path.Combine(PathExtenstions.GetApplicationCurrentPath(), $"{Configuration["EasyLogger:DbName"]}.db");
            services.AddSqlSugarDbStorage(new SqlSugarSetting()
            {
                Name             = SqlSugarDbStorageConsts.DefaultProviderName,
                ConnectionString = @$ "Data Source={defaultDbPath}",
                DatabaseType     = DbType.Sqlite,
                LogExecuting     = (sql, pars) =>
                {
                    Console.WriteLine($"sql:{sql}");
                }
            });
            #endregion

            #region 默认创建基础数据库 和  时间数据库

            if (!File.Exists(defaultDbPath))
            {
                var partition = services.BuildServiceProvider().GetService <IPartitionDbTableFactory>();
                partition.DbTableCreate(defaultDbPath, true);
            }

            var startUpDbPath = Path.Combine(PathExtenstions.GetApplicationCurrentPath(), $"{Configuration["EasyLogger:DbName"]}-{DateTime.Now.ToString("yyyy-MM")}.db");
            if (!File.Exists(startUpDbPath))
            {
                var partition = services.BuildServiceProvider().GetService <IPartitionDbTableFactory>();
                partition.DbTableCreate(startUpDbPath, false);
            }

            #endregion

            services.AddControllers();

            IocManager.SetConfiguration(Configuration);
            IocManager.Build();
        }
        public override void Intercept(IInvocation invocation)
        {
            MethodInfo method;

            try
            {
                method = invocation.MethodInvocationTarget;
            }
            catch (Exception ex)
            {
                method = invocation.GetConcreteMethod();
            }


            var dynamicLinkAttr = GetDynamicLinkAttributeOrNull(method);

            if (dynamicLinkAttr == null || dynamicLinkAttr.IsDisabled)
            {
                invocation.Proceed();//直接执行被拦截方法
            }
            else
            {
                var input = this.GetTiemRange(invocation);

                var dateList = TimeTools.GetMonthByList(input.TimeStart.ToString("yyyy-MM"), input.TimeEnd.ToString("yyyy-MM"));

                foreach (var item in dateList)
                {
                    var DbName     = $"{IocManager.Configuration["EasyLogger:DbName"]}-{item.ToString("yyyy-MM")}";
                    var dbPathName = Path.Combine(PathExtenstions.GetApplicationCurrentPath(), DbName + ".db");

                    IocManager.ServiceProvider.AddSqlSugarDatabaseProvider(new SqlSugarSetting()
                    {
                        Name             = DbName,
                        ConnectionString = @$ "Data Source={dbPathName}",
                        DatabaseType     = DbType.Sqlite,
                        LogExecuting     = (sql, pars) =>
                        {
                            Console.WriteLine($"sql:{sql}");
                        }
                    });
                }


                invocation.Proceed();//直接执行被拦截方法
            }
        }
Beispiel #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region SqlSugar
            var defaultDbPath = Path.Combine(PathExtenstions.GetApplicationCurrentPath(), $"{Configuration["EasyLogger:DbName"]}.db");

            services.AddSingleton <ISqlSugarProvider>(new SqlSugarProvider(new SqlSugarSetting()
            {
                Name             = SqlSugarDbStorageConsts.DefaultProviderName,
                ConnectionString = @$ "Data Source={defaultDbPath}",
                DatabaseType     = DbType.Sqlite,
                LogExecuting     = (sql, pars) =>
                {
                    Console.WriteLine($"sql:{sql}");
                }
            }));

            services.AddTransient(typeof(ISqlSugarRepository <,>), typeof(SqlSugarRepository <,>));
            services.AddTransient(typeof(IDbRepository <,>), typeof(SqlSugarRepository <,>));
            services.AddSingleton <ISqlSugarProviderStorage, DefaultSqlSugarProviderStorage>();
            #endregion

            #region 默认创建基础数据库 和  时间数据库

            if (!File.Exists(defaultDbPath))
            {
                var db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString      = @$ "Data Source={defaultDbPath}",
                    DbType                = DbType.Sqlite,
                    IsAutoCloseConnection = true,                 // 自动释放数据务,如果存在事务,在事务结束后释放
                    InitKeyType           = InitKeyType.Attribute // 从实体特性中读取主键自增列信息
                });

                db.CodeFirst.BackupTable().InitTables <EasyLoggerProject>();

                db.Dispose();
            }
            #endregion


            services.AddControllers();
        }