public void OnException(ExceptionContext context)
        {
            var errorMsg = "";

            if (context.Exception is SqlException)
            {
                errorMsg = @"系统繁忙,请稍后再试";
            }
            else
            {
                errorMsg = "未知错误,请重试";
                MyLogManager.Error(context.Exception.TargetSite.DeclaringType.ToString() + context.Exception.Message, context.Exception, this.GetType());
            }

            /*
             * 下面是我们自己处理、跳转到指定页面
             * context.Result=new RedirectResult("/home/Error");
             * 返回指定的错误对象
             * var json = new ErrorResponse("未知错误,请重试");
             */
            //public class ErrorResponse
            //{
            //    public ErrorResponse(string msg)
            //    {
            //        Message = msg;
            //    }
            //    public string Message { get; set; }
            //    public object DeveloperMessage { get; set; }
            //}

            context.Result           = new ApplicationErrorResult(errorMsg);
            context.ExceptionHandled = true;
        }
Beispiel #2
0
 private void InitializeLogging()
 {
     try
     {
         _logFilename     = MyLogManager.GetLogFilename();
         _viewLog.Enabled = true;
         _canViewLogs     = true;
     } catch (Exception)
     {
         _viewLog.Enabled = false;
         _canViewLogs     = false;
     }
 }
Beispiel #3
0
        protected void Application_Start()
        {
            MyLogManager.ConfigureFromWebConfig();

            // verify connection with database
            MyLogManager.GetLogger(typeof(MvcApplication)).Debug("Verifing connection with database");
            using (ZigBeeCoordinatorContext ctx = new ZigBeeCoordinatorContext())
            {
                ctx.DbCtx.CoordinatorUsers.FirstOrDefault();
            }

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
Beispiel #4
0
        public bool ValidateToken(string Token, out Dictionary <string, string> Clims)
        {
            Clims = new Dictionary <string, string>();
            ClaimsPrincipal principal = null;

            if (string.IsNullOrWhiteSpace(Token))
            {
                return(false);
            }
            var handler = new JwtSecurityTokenHandler();

            try
            {
                var jwt = handler.ReadJwtToken(Token);

                if (jwt == null)
                {
                    return(false);
                }
                var secretBytes          = Convert.FromBase64String(this._base64Secret);
                var validationParameters = new TokenValidationParameters
                {
                    RequireExpirationTime    = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(secretBytes),
                    ClockSkew                = TimeSpan.Zero,
                    ValidateIssuer           = true,                             //是否验证Issuer
                    ValidateAudience         = true,                             //是否验证Audience
                    ValidateLifetime         = this._jwtConfig.ValidateLifetime, //是否验证失效时间
                    ValidateIssuerSigningKey = true,                             //是否验证SecurityKey
                    ValidAudience            = this._jwtConfig.Audience,
                    ValidIssuer              = this._jwtConfig.Issuer
                };
                SecurityToken securityToken;
                principal = handler.ValidateToken(Token, validationParameters, out securityToken);
                foreach (var item in principal.Claims)
                {
                    Clims.Add(item.Type, item.Value);
                }
                return(true);
            }
            catch (System.Exception ex)
            {
                MyLogManager.Error(ex.Message, this.GetType());
                return(false);
            }
        }
Beispiel #5
0
        private static void RealMain(string[] args)
        {
            try
            {
                _log = MyLogManager.GetLogger(typeof(Program));
                Application.ThreadException += ApplicationThreadException;
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                new Program().Run(args);
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                throw;
            }
        }
Beispiel #6
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            switch (logLevel)
            {
            case LogLevel.Information:
                MyLogManager.Info(formatter(state, null), this.GetType());
                break;

            case LogLevel.Debug:
                MyLogManager.Debug(formatter(state, exception), this.GetType());
                break;

            case LogLevel.Error:
                MyLogManager.Error(formatter(state, exception), exception, this.GetType());
                break;

            case LogLevel.Warning:
                MyLogManager.Warning(formatter(state, exception), this.GetType());
                break;
            }
        }
Beispiel #7
0
        private void Init(IEnumerable <string> args)
        {
            try
            {
                FullScreen = true;
                _log       = MyLogManager.GetLogger(typeof(App));
                ProcessCommandLineArguments(args);

                if (_mockSoS)
                {
                    SirenOfShameDevice = new MockSirenOfShameDevice();
                }
                else
                {
                    SirenOfShameDevice = new SirenOfShameDevice();
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                throw;
            }
        }
Beispiel #8
0
 public static void Main()
 {
     MyLogManager.ConfigureFromXmlAtLocationOfAssembly(typeof(Program).Assembly);
     MyLogManager.GetLogger(typeof(Program)).Debug("Main");
     ServiceBase.Run(new ZigBeeCoordinatorService());
 }
 public void SetUp()
 {
     MyLogManager.Configure();
 }