Exemple #1
0
        /// <summary>
        /// 执行指定任务
        /// </summary>
        /// <param name="context"></param>
        /// <param name="action"></param>
        public async Task ExecuteJob(IJobExecutionContext context, Func <Task> func)
        {
            try
            {
                var s = context.Trigger.Key.Name;
                //DbLogHelper.WriteRunInfo(task.TaskName + " 开始", task.TaskID.ToString(), "");
                //记录Job时间
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                await func();//执行任务

                // 3. 开始执行相关任务
                stopwatch.Stop();
                Console.Out.WriteLine("执行时间" + stopwatch.Elapsed.TotalMilliseconds);
                // 4. 记录Task 运行状态数据库
                //DbLogHelper.WriteRunInfo(task.TaskName + " 结束", task.TaskID.ToString(), "成功执行");
                LogServer.WriteLog(context.Trigger.Key.Name.Replace("-", ""), $"{context.Trigger.Key.Name}定时任务运行一切OK", "任务结束");
            }
            catch (Exception ex)
            {
                JobExecutionException e2 = new JobExecutionException(ex);
                //true  是立即重新执行任务
                e2.RefireImmediately = true;
                LogServer.WriteErrorLog(context.Trigger.Key.Name.Replace("-", ""), $"{context.Trigger.Key.Name}任务运行异常", ex);
                // 记录异常到数据库和 log 文件中。
                //DbLogHelper.WriteErrorInfo(ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// 在自定义策略处理器中调用方法
        /// </summary>
        /// <param name="roleArr"></param>
        /// <returns></returns>
        public async Task <List <RoleActionModel> > GetRoleAction(Guid [] roleArr)
        {
            try
            {
                var RoleModuleList = new List <RoleActionModel>();
                var Rolelist       = await _roleRepositoty.GetAllListAsync(x => roleArr.Contains(x.Id));                             //根据Httpcontext存储的角色名称获取角色ID

                var RoleAssig = await _roleRightAssigRepository.GetAllListAsync(x => Rolelist.Select(s => s.Id).Contains(x.RoleId)); //根据角色ID获取到所有的权限

                var Btnlist = await _buttonRepositoty.GetAllListAsync(x => x.IsDrop == false);                                       //获取所有的按钮

                List <SysMenu> Menulist = new List <SysMenu>();
                if (await _redisCacheManager.Get(Appsettings.app(new string[] { "CacheOptions", "Menukey" })))                          //判断菜单缓存是否存在,如果存在则取缓存不存在则取数据库
                {
                    Menulist = await _redisCacheManager.GetList <SysMenu>(Appsettings.app(new string[] { "CacheOptions", "Menukey" })); //.Where(x=>MenuIds.Contains(x.Id)).ToList();
                }
                else
                {
                    Menulist = await this._menuRepositoty.GetAll(x => x.IsDrop == false).AsNoTracking().ToListAsync();

                    await _redisCacheManager.Set(Appsettings.app(new string[] { "CacheOptions", "Menukey" }), Menulist);
                }
                if (!Menulist.Any())
                {
                    Menulist = await _menuRepositoty.GetAllListAsync(x => x.IsDrop == false);//根据菜单ID获取菜单列表 x=>MenuIds.Contains(x.Id)
                }
                foreach (var item in RoleAssig)
                {
                    var RoleModel = Rolelist.Where(x => x.Id == item.RoleId).FirstOrDefault(); //获取角色实体
                    var MenuModel = Menulist.Where(x => x.Id == item.MenuId).FirstOrDefault(); //获取菜单实体
                    RoleModuleList.Add(new RoleActionModel {
                        RoleName = RoleModel.Id, ActionName = MenuModel.APIAddress
                    });
                    if (!item.ButtonIds.IsNullOrEmpty()) //判断是否存在按钮
                    {
                        List <Guid> guids   = new List <Guid>();
                        var         btnArr  = item.ButtonIds.Split(',').Select(x => x.ToGuid()).ToList();
                        var         RoleBtn = Btnlist.Where(x => btnArr.Contains(x.Id)).ToList();
                        RoleModuleList.AddRange(RoleBtn.Select(x => new RoleActionModel
                        {
                            RoleName   = RoleModel.Id,//在这里代表的是
                            ActionName = x.APIAddress,
                        }));
                    }
                }
                return(RoleModuleList);
            }
            catch (Exception ex)
            {
                var FromMailAddres = Appsettings.app(new string[] { "FromMailConfig", "FromMailAddres" });
                var FromMailPwd    = Appsettings.app(new string[] { "FromMailConfig", "FromMailPwd" });
                var ToMail         = Appsettings.app(new string[] { "FromMailConfig", "ToMail" });
                await SendEmail.SendMailAvailableAsync(FromMailAddres, FromMailPwd, ToMail, $"{ DateTime.Now.ToString("yyyy-MM-dd")}Redis超出限制错误", "Redis链接错误");

                LogServer.WriteErrorLog($"{ DateTime.Now.ToString("yyyy-MM-dd hh:mm:dd")}Redis超出限制错误", $"Redis链接错误", ex);
                throw ex;
            }
        }
 /// <summary>
 /// 异常中间件
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public async Task Invoke(HttpContext context)
 {
     try
     {
         await _next(context);
     }
     catch (Exception ex)
     {
         LogServer.WriteErrorLog(context.Request.Path.Value.Replace("/", ""), $"{context.Request.Path.Value}接口请求错误", ex);
         await HandleExceptionAsync(context, ex);
     }
 }
Exemple #4
0
 private async Task HandleExceptionAsync(HttpContext context, Exception e)
 {
     if (e == null)
     {
         return;
     }
     if (e is UnauthorizedAccessException)
     {
         context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
     }
     else if (e is Exception)
     {
         context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
     }
     LogServer.WriteErrorLog($"{context.Request.Path.Value}接口请求错误", e);
     context.Response.ContentType = "application/json";
     var res = ApiResponse.Error <string>(e.Message);
     await context.Response.WriteAsync(res.ToJson());
 }