public static void GetResponseHeaders_Should_Return_Null_When_Context_Response_Is_Null()
        {
            // arrange
            NancyContext context = new NancyContext();

            // act
            var headers = context.GetResponseHeaders();

            // assert
            Assert.Null(headers);
        }
        public static void GetResponseHeaders_Should_Return_Empty_When_Context_Response_Headers_Is_Null()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(responseHeaders: null);

            // act
            var headers = context.GetResponseHeaders();

            // assert
            Assert.Null(headers);
        }
        public static void GetResponseHeaders_Should_Return_Response_Items()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseHeaders: new Dictionary <string, string>
            {
                { "SomeHeader1", "Test1" },
                { "SomeHeader2", "Test3" },
                { "SomeHeader3", "" },
                { "SomeHeader4", null }
            });

            // act
            var headers = context.GetResponseHeaders();

            // assert
            Assert.Equal("Test1", headers["SomeHeader1"]);
            Assert.Equal("Test3", headers["SomeHeader2"]);
            Assert.Equal("", headers["SomeHeader3"]);
            Assert.Null(headers["SomeHeader4"]);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Log context and exception
        /// </summary>
        /// <param name="context"></param>
        /// <param name="exception"></param>
        public void LogData(NancyContext context, Exception exception)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var statusCode = context.GetStatusCode(exception);

            string exceptionMessage    = null;
            string exceptionStackTrace = null;

            if (exception != null)
            {
                exceptionMessage    = HandleFieldSize(exception.Message, ExceptionMaxLenghtExtension.ErrorMessageLenght);
                exceptionStackTrace = HandleFieldSize(exception.StackTrace, ExceptionMaxLenghtExtension.ErrorExceptionLenght);
            }

            object controller = "Unknow";
            object action     = "Unknow";

            if (context.Items != null)
            {
                context.Items.TryGetValue("Controller", out controller);
                context.Items.TryGetValue("Action", out action);
            }

            LogContext.PushProperty("RequestBody", context.GetRequestBody(this.NancySerilogConfiguration.Blacklist));
            LogContext.PushProperty("Method", context.Request.Method);
            LogContext.PushProperty("Path", context.Request.Path);
            LogContext.PushProperty("Host", context.Request.Url.HostName);
            LogContext.PushProperty("Port", context.Request.Url.Port);
            LogContext.PushProperty("Url", context.Request.Url);
            LogContext.PushProperty("QueryString", context.Request.Url.Query);
            LogContext.PushProperty("Query", context.GetQueryString());
            LogContext.PushProperty("RequestHeaders", context.GetRequestHeaders());
            LogContext.PushProperty("Ip", context.GetIp());
            LogContext.PushProperty("IsSuccessful", statusCode < 400);
            LogContext.PushProperty("StatusCode", statusCode);
            LogContext.PushProperty("StatusDescription", ((HttpStatusCode)statusCode).ToString());
            LogContext.PushProperty("StatusCodeFamily", context.GetStatusCodeFamily(exception));
            LogContext.PushProperty("ProtocolVersion", context.Request.ProtocolVersion);
            LogContext.PushProperty("ErrorException", exceptionStackTrace);
            LogContext.PushProperty("ErrorMessage", exceptionMessage);
            LogContext.PushProperty("ResponseContent", context.GetResponseContent());
            LogContext.PushProperty("ContentType", context.Response.ContentType);
            LogContext.PushProperty("ContentLength", context.GetResponseLength());
            LogContext.PushProperty("ResponseHeaders", context.GetResponseHeaders());
            LogContext.PushProperty("ElapsedMilliseconds", context.GetExecutionTime());
            LogContext.PushProperty("Version", this.NancySerilogConfiguration.Version);
            LogContext.PushProperty("RequestKey", context.GetRequestKey());
            LogContext.PushProperty("Controller", controller?.ToString());
            LogContext.PushProperty("Operation", action?.ToString());
            LogContext.PushProperty("Environment", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

            if (context.Items.ContainsKey("NancySerilogAdditionalInfo"))
            {
                var additionalInfo = (AdditionalInfo)context.Items["NancySerilogAdditionalInfo"];

                if (additionalInfo?.Data != null)
                {
                    foreach (var item in additionalInfo.Data)
                    {
                        LogContext.PushProperty(item.Key, item.Value);
                    }
                }
            }



            if (exception != null || statusCode >= 500)
            {
                var errorTitle = this.NancySerilogConfiguration.ErrorTitle ?? DefaultErrorTitle;
                this.NancySerilogConfiguration.Logger.Error(errorTitle);
            }
            else
            {
                var informationTitle = this.NancySerilogConfiguration.InformationTitle ?? DefaultInformationTitle;
                this.NancySerilogConfiguration.Logger.Information(informationTitle);
            }
        }