简体   繁体   English

记录请求 json 数据

[英]log request json data

Assuming I am doing webservice request from the client.假设我正在做来自客户端的 web 服务请求。 The request is made by POST with json parameters.该请求是通过带有 json 参数的 POST 发出的。
In the server, Context.Request.RawUrl hold the request url.在服务器中, Context.Request.RawUrl持有请求 url。 But what holds the request json data?但是什么拥有请求 json 数据?

I need this property in order to log requests.我需要这个属性来记录请求。

Here is the code for the http module:这是 http 模块的代码:

  public class RequestResponseLoggerModule : IHttpModule
    {
        //private static Logger logger = LogManager.GetCurrentClassLogger();

        #region ResponseCaptureStream

        private class ResponseCaptureStream : Stream
        {
            private readonly Stream _streamToCapture;
            private readonly Encoding _responseEncoding;

            public string StreamContent { get; private set; }            

            public ResponseCaptureStream(Stream streamToCapture, Encoding responseEncoding)
            {
                _responseEncoding = responseEncoding;
                _streamToCapture = streamToCapture;
            }

            public override bool CanRead { get { return _streamToCapture.CanRead; } }
            public override bool CanSeek { get { return _streamToCapture.CanSeek; } }
            public override bool CanWrite { get { return _streamToCapture.CanWrite; } }
            public override long Length { get { return _streamToCapture.Length; } }
            public override long Position
            {
                get { return _streamToCapture.Position; }
                set { _streamToCapture.Position = value; }
            }


            public override void Flush()
            {
                _streamToCapture.Flush();
            }           

            public override int Read(byte[] buffer, int offset, int count)
            {
                return _streamToCapture.Read(buffer, offset, count);
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                return _streamToCapture.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                _streamToCapture.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                StreamContent += _responseEncoding.GetString(buffer);
                _streamToCapture.Write(buffer, offset, count);
            }

            public override void Close()
            {
                _streamToCapture.Close();
                base.Close();
            }
        }

        #endregion

        #region IHttpModule Members

        private HttpApplication _context;
        public void Dispose()
        {

        }

        private static string Extensions_Key = "RequestResponseLoggerModule.Extensions";
        private static string Files_Key = "RequestResponseLoggerModule.Files";
        private static string Request_Key = "RequestResponseLoggerModule.Request";
        private static string Response_Key = "RequestResponseLoggerModule.Response";

        private IEnumerable<string> _Extenstions = new string[] { };
        private IEnumerable<string> _Files = new string[] { };
        private bool _LogAlwaysRequest = false;
        private bool _LogAlwaysResponse = false;

        public void Init(HttpApplication context)
        {
            _Extenstions = ConfigurationManager.AppSettings[Extensions_Key].ToLower().Split(',').
                Select(x => x.Trim()).ToArray();
            _Files = ConfigurationManager.AppSettings[Files_Key].ToLower().Split(',').
                Select(x => x.Trim()).ToArray();
            _LogAlwaysRequest = ConfigurationManager.AppSettings[Request_Key] == "ALWAYS";
            _LogAlwaysResponse = ConfigurationManager.AppSettings[Response_Key] == "ALWAYS";

            _context = context;

            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            _context.Response.Filter = new ResponseCaptureStream(_context.Response.Filter, _context.Response.ContentEncoding);
        }

        void context_PreSendRequestContent(object sender, EventArgs e)
        {
            bool isMatch = false;
            if (_Extenstions.Count() > 0)
            {
                string ext = VirtualPathUtility.GetExtension(_context.Request.FilePath).
                    Substring(1).ToLower();
                if (_Extenstions.Contains(ext))
                {
                    isMatch = true;
                }
            }
            if (_Files.Count() > 0)
            {
                string fileName = VirtualPathUtility.GetFileName(_context.Request.FilePath).                   ToLower();
                if (_Files.Contains(fileName))
                {
                    isMatch = true;
                }
            }

            if (_LogAlwaysRequest || isMatch)
            {
                string requestText = _context.Request.RawUrl; <------------------

                //log the responseText, but this reurns just the url while I need the data of the webservice call
            }
            if (_LogAlwaysResponse || isMatch)
            {
                ResponseCaptureStream filter = _context.Response.Filter as ResponseCaptureStream;
                if (filter != null)
                {
                    string responseText = filter.StreamContent;
                    //log the responseText
                }
            }        
        }

        #endregion
    }

If you have made a POST with the json in the payload, then you need to read the request body.如果您在有效负载中使用 json 进行了 POST,那么您需要读取请求正文。 You would do this by consuming Request.InputStream , for example:您可以通过使用Request.InputStream来做到这一点,例如:

        string json;
        using (var reader = new StreamReader(Request.InputStream))
        {
            json = reader.ReadToEnd();
        }

You need to be careful, though - since consuming the body may mean that your actual code can't read it.不过,您需要小心 - 因为消耗身体可能意味着您的实际代码无法读取它。 Which would be bad.这会很糟糕。

public class RequestResponseLoggerModule : IHttpModule
{
    //private static Logger logger = LogManager.GetCurrentClassLogger();

    #region CaptureStream

    private class CaptureStream : Stream
    {
        private readonly Stream _streamToCapture;
        private readonly Encoding _encoding;

        public string StreamContent { get; private set; }

        public CaptureStream(Stream streamToCapture, Encoding encoding)
        {
            _encoding = encoding;
            _streamToCapture = streamToCapture;
        }

        public override bool CanRead { get { return _streamToCapture.CanRead; } }
        public override bool CanSeek { get { return _streamToCapture.CanSeek; } }
        public override bool CanWrite { get { return _streamToCapture.CanWrite; } }
        public override long Length { get { return _streamToCapture.Length; } }
        public override long Position
        {
            get { return _streamToCapture.Position; }
            set { _streamToCapture.Position = value; }
        }


        public override void Flush()
        {
            _streamToCapture.Flush();
        }           

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _streamToCapture.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _streamToCapture.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            _streamToCapture.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            StreamContent += _encoding.GetString(buffer);
            _streamToCapture.Write(buffer, offset, count);
        }

        public override void Close()
        {
            _streamToCapture.Close();
            base.Close();
        }
    }

    #endregion

    #region IHttpModule Members

    private HttpApplication _context;
    public void Dispose()
    {

    }

    private static string Extensions_Key = "RequestResponseLoggerModule.Extensions";
    private static string Files_Key = "RequestResponseLoggerModule.Files";
    private static string Request_Key = "RequestResponseLoggerModule.Request";
    private static string Response_Key = "RequestResponseLoggerModule.Response";

    private IEnumerable<string> _Extenstions = new string[] { };
    private IEnumerable<string> _Files = new string[] { };
    private bool _LogAlwaysRequest = false;
    private bool _LogAlwaysResponse = false;
    private bool _IsMatch = false;

    public void Init(HttpApplication context)
    {
        _Extenstions = ConfigurationManager.AppSettings[Extensions_Key].ToLower().Split(',').
            Select(x => x.Trim()).ToArray();
        _Files = ConfigurationManager.AppSettings[Files_Key].ToLower().Split(',').
            Select(x => x.Trim()).ToArray();
        _LogAlwaysRequest = ConfigurationManager.AppSettings[Request_Key] == "ALWAYS";
        _LogAlwaysResponse = ConfigurationManager.AppSettings[Response_Key] == "ALWAYS";

        _context = context;

        context.BeginRequest += new EventHandler(context_BeginRequest);
        context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
    }

    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        _context.Response.Filter = new CaptureStream(_context.Response.Filter, _context.Response.ContentEncoding);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        if (_Extenstions.Count() > 0)
        {
            string ext = VirtualPathUtility.GetExtension(_context.Request.FilePath).
                Substring(1).ToLower();
            if (_Extenstions.Contains(ext))
            {
                _IsMatch = true;
            }
        }
        if (_Files.Count() > 0)
        {
            string fileName = VirtualPathUtility.GetFileName(_context.Request.FilePath).ToLower();
            if (_Files.Contains(fileName))
            {
                _IsMatch = true;
            }
        }

        if (_LogAlwaysRequest || _IsMatch)
        {
            StreamReader reader = new StreamReader(_context.Request.InputStream);
            string requestText = reader.ReadToEnd();
            _context.Request.InputStream.Position = 0;
            //LOG requestText
        }
    }

    void context_PreSendRequestContent(object sender, EventArgs e)
    {
        if (_LogAlwaysResponse || _IsMatch)
        {
            CaptureStream filter = _context.Response.Filter as CaptureStream;
            if (filter != null)
            {
                string responseText = filter.StreamContent;
                //LOG responseText
            }
        }
    }

    #endregion
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM