Exemplo n.º 1
0
        public void ContextAwareLogger_WrittingLogEntry_PrependsTraceId()
        {
            string traceId      = Guid.NewGuid().ToString();
            string logEntryText = "log entry";
            var    logger       = new Fakes.FakeLogger();
            var    ctxLog       = new ContextAwareLogger(true, logger, traceId);

            ctxLog.LogInformation(logEntryText);
            string logEntry = logger.LogEntries.Single();

            Assert.True(logEntry.StartsWith(traceId + ":"));
            Assert.True(logEntry.EndsWith(logEntryText));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets called by ASP.NET framework
        /// </summary>
        /// <param name="context">Instance of <see cref="HttpContext"/> provided by ASP.NET framework</param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            ILogger log = new ContextAwareLogger(_isLoggingEnabled, _logger, context.TraceIdentifier);

            log.LogInformation($"Received request: {context.Request.Path} with query: {context.Request.QueryString.ToString() ?? ""}");
            IPathResolver pathResolver = Options.InternalServiceResolver.GetPathResolver();
            ActionContext action       = pathResolver.ResolveAction(context.Request, log);

            if (action == null)
            {
                log.LogInformation("Request is skipped to next middleware");
                if (_next != null)
                {
                    log.LogInformation("Invoking next middleware");
                    await _next?.Invoke(context);

                    log.LogInformation("Next middleware invoked");
                }
                else
                {
                    log.LogInformation("There is no next middleware to invoke");
                }
            }
            else
            {
                if (Options.RequiresHttps && !context.Request.IsHttps)
                {
                    log.LogInformation("LiteApi options are set to require HTTPS, request rejected because request is HTTP");
                    context.Response.StatusCode = 400;
                    await context.Response.WriteAsync("Bad request, HTTPS request was expected.");
                }
                else
                {
                    var actionInvoker = Options.InternalServiceResolver.GetActionInvoker();
                    await actionInvoker.Invoke(context, action, log);

                    log.LogInformation("Action is invoked");
                }
            }
            log.LogInformation("Request is processed");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets called by ASP.NET framework
        /// </summary>
        /// <param name="httpCtx">Instance of <see cref="HttpContext"/> provided by ASP.NET framework</param>
        /// <returns></returns>
        public async Task Invoke(HttpContext httpCtx)
        {
            httpCtx.SetLiteApiOptions(Options);

            ILogger log = new ContextAwareLogger(_isLoggingEnabled, _logger, httpCtx.TraceIdentifier);

            log.LogInformation($"Received request: {httpCtx.Request.Path} with query: {httpCtx.Request.QueryString.ToString() ?? ""}");

            if (Options.RequiresHttps && !httpCtx.Request.IsHttps)
            {
                log.LogInformation("LiteApi options are set to require HTTPS, request rejected because request is HTTP");
                httpCtx.Response.StatusCode = 400;
                await httpCtx.Response.WriteAsync("Bad request, HTTPS request was expected.");

                return;
            }

            if (Options.DiscoveryEnabled)
            {
                bool handledOnDiscovery = await _discoveryHandler.HandleIfNeeded(httpCtx);

                if (handledOnDiscovery)
                {
                    return;
                }
            }

            ActionContext action = _pathResolver.ResolveAction(httpCtx.Request, log);

            if (action == null)
            {
                log.LogInformation("Request is skipped to next middleware");
                if (_next != null)
                {
                    log.LogInformation("Invoking next middleware");
                    await _next?.Invoke(httpCtx);

                    log.LogInformation("Next middleware invoked");
                }
                else
                {
                    log.LogInformation("There is no next middleware to invoke");
                }
            }
            else
            {
                // TODO: consider replacing RequiresHttps with global filter
                httpCtx.SetActionContext(action);

                if (!(await CheckGlobalFiltersAndWriteResponseIfAny(httpCtx, log, action.SkipAuth)))
                {
                    return;
                }

                await _actionInvoker.Invoke(httpCtx, action, log);

                log.LogInformation("Action is invoked");
            }

            log.LogInformation("Request is processed");
        }