示例#1
0
        public async Task Invoke(HttpContext context)
        {
            WebRestLog log = new WebRestLog()
            {
                TraceId = context.TraceIdentifier
            };

            try
            {
                log = await LogRequest(context.Request, log);
            }
            catch (Exception e)
            {
                Loki.ExceptionWarning("Error in Loki Middleware logging Request", e);
            }

            var originalBodyStream = context.Response.Body;

            using (var responseBody = new MemoryStream())
            {
                context.Response.Body = responseBody;

                await _next(context);

                await LogResponse(context.Response, log);

                await responseBody.CopyToAsync(originalBodyStream);
            }
        }
示例#2
0
        public async Task Invoke(HttpContext context)
        {
            if (!LokiObjectAdapter.LokiConfig.UseLokiMiddleware || LokiObjectAdapter.LokiConfig.IgnoreRoutes.Any(x => context.Request.Path.ToString().Contains(x)))
            {
                await _next(context);
            }
            else
            {
                WebRestLog log = new WebRestLog()
                {
                    TraceId = context.TraceIdentifier
                };
                try
                {
                    if (!LokiObjectAdapter.LokiConfig.NoRequestRoutes.Any(x => context.Request.Path.ToString().Contains(x)))
                    {
                        log = await LogRequest(context.Request, log);
                    }
                }
                catch (Exception e)
                {
                    Loki.ExceptionWarning("Error in Loki Middleware logging Request", e);
                }

                var originalBodyStream = context.Response.Body;

                //Create a new memory stream...
                using (var responseBody = new MemoryStream())
                {
                    context.Response.Body = responseBody;
                    try
                    {
                        await _next(context);

                        if (context.Items.ContainsKey("Exception"))
                        {
                            Exception ex = (Exception)context.Items["Exception"];
                            log.Exception = ex.Message + "\n" + ex.StackTrace + "\n" + ex.Source;
                        }
                    }
                    catch (Exception e)
                    {
                        log.Exception = e.Message + "\n" + e.StackTrace + "\n" + e.Source;
                        await LogResponse(context.Response, log);

                        Loki.Write(Loki.REST_TYP, LogLevel.Error, "", "Invoke", "LokiWebExtension.Middleware.LokiMiddleware", 48, log);
                        throw;
                    }

                    try
                    {
                        if (!LokiObjectAdapter.LokiConfig.NoResponseRoutes.Any(x => context.Request.Path.ToString().Contains(x)))
                        {
                            await LogResponse(context.Response, log);
                        }
                    }
                    catch (Exception e)
                    {
                        Loki.ExceptionWarning("Error in Loki Middleware logging Response", e);
                    }
                    if (originalBodyStream.CanWrite && responseBody.CanRead)
                    {
                        await responseBody.CopyToAsync(originalBodyStream);
                    }
                }


                Loki.Write(Loki.REST_TYP, LogLevel.SystemGenerated, "", "Invoke", "LokiWebExtension.Middleware.LokiMiddleware", 55, log);
            }
        }