public static void GetIp_Should_Return_Default_When_Context_Request_Is_Null() { // arrange NancyContext context = new NancyContext(); // act var ip = context.GetIp(); // assert Assert.Equal("??", ip); }
public static void GetIp_Should_Return_Default_When_Context_Request_Headers_Is_Null_And_Origin_Is_Null() { // arrange NancyContext context = NancyContextMock.Create(requestHeaders: null, originIp: null); // act var ip = context.GetIp(); // assert Assert.Equal("??", ip); }
public static void GetIp_Should_Return_Ip_When_Context_Request_Headers_Contains_XForwarwedFor() { // arrange // arrange NancyContext context = NancyContextMock.Create( requestHeaders: new Dictionary <string, IEnumerable <string> > { { "X-Forwarded-For", new string[] { "233.233.233.233" } } }); // act var ip = context.GetIp(); // assert Assert.Equal("233.233.233.233", ip); }
public static void GetIp_Should_Return_Default_When_Context_Request_Headers_Not_Contains_XForwarwedFor_And_Origin_Is_Null() { // arrange // arrange NancyContext context = NancyContextMock.Create( requestHeaders: new Dictionary <string, IEnumerable <string> > { { "Test", new string[] { "233.233.233.233" } } }, originIp: null); // act var ip = context.GetIp(); // assert Assert.Equal("??", ip); }
/// <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); } }