Ejemplo n.º 1
0
        private async Task <string> GetBodyAsStringAndRewindAsync(HttpContent content)
        {
            try
            {
                var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);

                if (stream != null)
                {
                    if (content.IsTextContent())
                    {
                        var encoding = content.GetEncoding();
                        var str      = await stream.ReadAllTextAndRewindAsync(encoding).ConfigureAwait(false);

                        return(str.Length <= MaxLoggedBodyLength
                            ? str
                            : str.Substring(0, MaxLoggedBodyLength));
                    }
                    else
                    {
                        var bytes = await stream.ReadAllAndRewindAsync().ConfigureAwait(false);

                        return(BitConverter.ToString(bytes, 0, MaxLoggedBodyLength).Replace("-", string.Empty));
                    }
                }
            }
            catch
            {
            }

            return(null);
        }