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

            // act
            var requestKey = context.GetRequestKey();

            // assert
            Assert.Null(requestKey);
        }
        public static void GetRequestKey_Should_Return_Null_When_Context_Request_Headers_Is_Empty()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(requestHeaders: null);

            // act
            var requestKey = context.GetRequestKey();

            // assert
            Assert.Null(requestKey);
        }
        public static void GetRequestKey_Should_Return_RequestKey_Value()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseHeaders: new Dictionary <string, string>
            {
                { "RequestKey", "b2f6759b-50fa-b7c3-7955-4fee4693482e" }
            });

            // act
            var requestKey = context.GetRequestKey();

            // assert
            Assert.Equal("b2f6759b-50fa-b7c3-7955-4fee4693482e", requestKey);
        }
        public static void GetRequestKey_Should_Return_Null_When_Context_Request_Headers_Not_Contains_RequestKey()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseHeaders: new Dictionary <string, string>
            {
                { "OtherKey", "b2f6759b-50fa-b7c3-7955-4fee4693482e" }
            });

            // act
            var requestKey = context.GetRequestKey();

            // assert
            Assert.Null(requestKey);
        }
Exemple #5
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);
            }
        }