C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

ログ内検索
  • キーワードを複数指定する場合は 半角スペース で区切ってください。
  • 検索条件は、(AND)=[A かつ B] (OR)=[A または B] となっています。
  • [返信]をクリックすると返信ページへ移動します。
キーワード/ 検索条件 /
検索範囲/ 強調表示/ ON (自動リンクOFF)
結果表示件数/ 記事No検索/ ON
大文字と小文字を区別する

No.87875 の関連記事表示

<< 0 >>
■87875  Re[6]: ASPもしくはIISでhttpレスポンスボディを確認する方法
□投稿者/ WebSurfer -(2018/07/09(Mon) 18:46:06)
    No87874 (abc さん) に返信
    
    > もし何かヒントがありましたらコメント頂ければ幸いです。
    
    紹介した記事のように HttpContext.Items を使うという話はどうなったのですか?
    
    IIS のクラシックモードを使うのは諦めて統合パイプラインモードでやることにしたのですか?
    
    そのあたりをクリアにしてもらえないと、この先私がお役に立てるかどうか分からないのですが・・・
    
    とりあえず、検証に使った HTTP モジュールのコードをアップしておきます。環境は Windows 10
    Pro 64-bit, ローカル IIS10, Visual Studio 2015 Cimmunity, .NET 4.6.1 です。
    
    HTTP モジュールが動かないとか文字化けするという問題はありません。
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    
    public class ResponseContentLogHttpModule : IHttpModule
    {
        public ResponseContentLogHttpModule()
        {
        }
    
        public String ModuleName
        {
            get { return "ResponseContentLogHttpModule"; }
        }
    
        // In the Init function, register for HttpApplication 
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(this.Application_BeginRequest);
            application.EndRequest += new EventHandler(this.Application_EndRequest);
        }
    
        private void Application_BeginRequest(Object source, EventArgs e)
        {
            // Create HttpApplication and HttpContext objects to access
            // request and response properties.
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            string filePath = context.Request.FilePath;
            string fileExtension = VirtualPathUtility.GetExtension(filePath);
            if (fileExtension.Equals(".aspx"))
            {
                HttpResponse response = context.Response;
                var filter = new OutputFilterStream(response.Filter);
                response.Filter = filter;
                context.Items["OutputFilterStream"] = filter;
            }
        }
    
        private void Application_EndRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            string filePath = context.Request.FilePath;
            string fileExtension = VirtualPathUtility.GetExtension(filePath);
            if (fileExtension.Equals(".aspx"))
            {
                var filter = (OutputFilterStream)context.Items["OutputFilterStream"];
                string responseContent = filter.ReadStream();
                WriteLog(responseContent);
            }
        }
    
        private void WriteLog(string log)
        {
            string responseLogPath = HttpContext.Current.Server.MapPath("~/App_Data/log.txt");
            using (var sw = new StreamWriter(responseLogPath, true))
            {
                sw.WriteLine(log);
            }
        }
    
        public void Dispose() { }
    }
    
    public class OutputFilterStream : Stream
    {
        private readonly Stream InnerStream;
        private readonly MemoryStream CopyStream;
    
        public OutputFilterStream(Stream inner)
        {
            this.InnerStream = inner;
            this.CopyStream = new MemoryStream();
        }
    
        public string ReadStream()
        {
            lock (this.InnerStream)
            {
                if (this.CopyStream.Length <= 0L ||
                    !this.CopyStream.CanRead ||
                    !this.CopyStream.CanSeek)
                {
                    return String.Empty;
                }
    
                long pos = this.CopyStream.Position;
                this.CopyStream.Position = 0L;
                try
                {
                    return new StreamReader(this.CopyStream).ReadToEnd();
                }
                finally
                {
                    try
                    {
                        this.CopyStream.Position = pos;
                    }
                    catch { }
                }
            }
        }
    
    
        public override bool CanRead
        {
            get { return this.InnerStream.CanRead; }
        }
    
        public override bool CanSeek
        {
            get { return this.InnerStream.CanSeek; }
        }
    
        public override bool CanWrite
        {
            get { return this.InnerStream.CanWrite; }
        }
    
        public override void Flush()
        {
            this.InnerStream.Flush();
        }
    
        public override long Length
        {
            get { return this.InnerStream.Length; }
        }
    
        public override long Position
        {
            get { return this.InnerStream.Position; }
            set { this.CopyStream.Position = this.InnerStream.Position = value; }
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            return this.InnerStream.Read(buffer, offset, count);
        }
    
        public override long Seek(long offset, SeekOrigin origin)
        {
            this.CopyStream.Seek(offset, origin);
            return this.InnerStream.Seek(offset, origin);
        }
    
        public override void SetLength(long value)
        {
            this.CopyStream.SetLength(value);
            this.InnerStream.SetLength(value);
        }
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            this.CopyStream.Write(buffer, offset, count);
            this.InnerStream.Write(buffer, offset, count);
        }
    }
記事No.87867 のレス /過去ログ151より / 関連記事表示
削除チェック/



<< 0 >>

パスワード/

- Child Tree -