public RequestLogEnricher(RequestLogData request, NancySerilogOptions options)
 {
     this.request = request;
     this.options = options;
     if (this.options.IgnoredRequestLogFields == null)
     {
         this.options.IgnoredRequestLogFields = new FieldChooser <RequestLogData>();
     }
 }
Beispiel #2
0
        public static RequestLogData ReadRequestProperties(this Request nancyRequest)
        {
            var request = new RequestLogData();

            request.RequestHostName      = nancyRequest.Url.HostName;
            request.Method               = nancyRequest.Method;
            request.Path                 = nancyRequest.Url.Path;
            request.QueryString          = nancyRequest.Url.Query;
            request.RequestContentLength = nancyRequest.Headers.ContentLength;
            request.RequestContentType   = nancyRequest.Headers.ContentType;
            request.RequestBodyContent   = nancyRequest.Body.ReadBodyContent();
            request.RequestHeaders       = nancyRequest.Headers.ReadRequestHeaders();
            request.UserIPAddress        = nancyRequest.UserHostAddress;
            request.Query                = ReadQuery(nancyRequest.Query);
            request.RequestCookies       = new Dictionary <string, string>(nancyRequest.Cookies);
            return(request);
        }
        public static RequestLogData ReadRequestProperties(this NancyContext context, NancySerilogOptions opts)
        {
            var nancyRequest  = context.Request;
            var request       = new RequestLogData();
            var ignoredFields = opts.IgnoredRequestLogFields.ToArray();

            if (!ignoredFields.Contains(nameof(request.RequestHostName)))
            {
                request.RequestHostName = nancyRequest.Url.HostName;
            }

            if (!ignoredFields.Contains(nameof(request.RequestHostName)))
            {
                request.Method = nancyRequest.Method;
            }

            if (!ignoredFields.Contains(nameof(request.Path)))
            {
                request.Path = nancyRequest.Url.Path;
            }

            if (!ignoredFields.Contains(nameof(request.QueryString)))
            {
                request.QueryString = nancyRequest.Url.Query;
            }

            if (!ignoredFields.Contains(nameof(request.RequestContentLength)))
            {
                request.RequestContentLength = nancyRequest.Headers.ContentLength;
            }

            if (!ignoredFields.Contains(nameof(request.RequestContentType)))
            {
                if (nancyRequest.Headers != null && nancyRequest.Headers.ContentType != null)
                {
                    request.RequestContentType = nancyRequest.Headers.ContentType.ToString();
                }
                else
                {
                    request.RequestContentType = "";
                }
            }

            if (!ignoredFields.Contains(nameof(request.RequestBodyContent)))
            {
                request.RequestBodyContent = "";

                if (!nancyRequest.Files.Any())
                {
                    // Only read request body content when there aren't any files
                    request.RequestBodyContent = nancyRequest.Body.ReadBodyContent();
                }
            }

            if (!ignoredFields.Contains(nameof(request.RequestHeaders)))
            {
                request.RequestHeaders = nancyRequest.Headers.ReadRequestHeaders();
                if (request.RequestHeaders.ContainsKey("User-Agent"))
                {
                    try
                    {
                        var userAgent  = request.RequestHeaders["User-Agent"];
                        var parser     = UAParser.Parser.GetDefault();
                        var clientInfo = parser.Parse(userAgent);
                        request.UserAgentFamily = clientInfo.UserAgent.Family;
                        request.UserAgentDevice = clientInfo.Device.Family;
                        request.UserAgentOS     = clientInfo.OS.Family;
                    }
                    catch
                    {
                        request.UserAgentFamily = "Other";
                        request.UserAgentDevice = "Other";
                        request.UserAgentOS     = "Other";
                    }
                }
                else
                {
                    request.UserAgentFamily = "Other";
                    request.UserAgentDevice = "Other";
                    request.UserAgentOS     = "Other";
                }
            }
            else
            {
                request.UserAgentFamily = "Other";
                request.UserAgentDevice = "Other";
                request.UserAgentOS     = "Other";
            }

            if (!ignoredFields.Contains(nameof(request.UserIPAddress)))
            {
                request.UserIPAddress = nancyRequest.UserHostAddress;
            }

            if (!ignoredFields.Contains(nameof(request.Query)))
            {
                if (nancyRequest.Query != null)
                {
                    request.Query = ReadDynamicDictionary(nancyRequest.Query);
                }
            }

            if (!ignoredFields.Contains(nameof(request.Query)))
            {
                request.RequestCookies = new Dictionary <string, string>(nancyRequest.Cookies);
            }

            return(request);
        }
 public RequestLogEnricher(RequestLogData request)
 {
     this.request = request;
 }