Ejemplo n.º 1
0
        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            var      code = HttpStatusCode.OK; // 500 if unexpected
            ErrorLog log  = new ErrorLog()
            {
                time         = DateTime.Now,
                action       = context.Request.Path + context.Request.QueryString,
                log_level    = "alert",
                api_message  = new ErrorLog.ApiMessage(),
                project_name = "log",
                ip           = context.Connection.RemoteIpAddress.ToString()
            };
            //if (exception is MyNotFoundException) code = HttpStatusCode.NotFound;
            //else if (exception is MyUnauthorizedException) code = HttpStatusCode.Unauthorized;
            //else if (exception is MyException) code = HttpStatusCode.BadRequest;
            var token = LoginSession.Current(context);

            log.api_message = new ErrorLog.ApiMessage();
            if (token != null)
            {
                log.authorize_name = token.FullName + ":" + token.UserName;
            }
            var stream = context.Request.Body;

            log.error_message = exception.ToString();
            //Lay lai cai goc;
            context.Response.Body = stream;
            await LogHelper.AddLogAsync(log);

            var result = JsonConvert.SerializeObject(new { status = 1, msg = exception.Message });

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)code;
            await context.Response.WriteAsync(result);
        }
        public async Task <object> Resolve(
            ResolveFieldContext context,
            FieldMiddlewareDelegate next)
        {
            var metadata = new Dictionary <string, object>
            {
                { "typeName", context.ParentType.Name },
                { "fieldName", context.FieldName },
                { "path", context.Path },
                { "arguments", context.Arguments },
            };
            var path = $"{context.ParentType.Name}.{context.FieldName}";

            try
            {
                using (context.Metrics.Subject("field", path, metadata))
                {
                    return(await next(context).ConfigureAwait(false));
                }
                //return await next(context);
            }
            catch (Exception e)
            {
                ErrorLog log = new ErrorLog()
                {
                    time        = DateTime.Now,
                    action      = path,
                    log_level   = "alert",
                    api_message = new ErrorLog.ApiMessage()
                    {
                        request = JsonConvert.SerializeObject(context.Arguments)
                    },
                    project_name = "log"
                };

                log.error_message = e.ToString();
                //Lay lai cai goc;
                try
                {
                    await LogHelper.AddLogAsync(log);
                }catch (Exception ex)
                {
                }
                throw;
            }
        }
Ejemplo n.º 3
0
 public async Task Invoke(HttpContext context)
 {
     try
     {
         string contentType = context.Request.ContentType;
         if (contentType == null)
         {
             await HandleShemaConf(context);
         }
         else
         if (!contentType.StartsWith(jsonMediaType))
         {
             try
             {
                 HandleMultipart(context);
             }catch (Exception exx)
             {
                 //error handle multypart
                 ErrorLog log = new ErrorLog()
                 {
                     time         = DateTime.Now,
                     action       = context.Request.Path + context.Request.QueryString,
                     log_level    = "alert",
                     api_message  = new ErrorLog.ApiMessage(),
                     project_name = "log",
                     ip           = context.Connection.RemoteIpAddress.ToString()
                 };
                 await LogHelper.AddLogAsync(log);
             }
         }
         await next(context);
     }
     catch (Exception ex)
     {
         await HandleExceptionAsync(context, ex);
     }
 }