My Developer Memo
開発に関する覚え書き
Latest Entries
Recent Comments
Search this site
Categories
Archives
Links
Syndicate this site
RSS1.0
Atom0.3
Powered by
Serene Bach 2.23R
<< ノータッチデプロイメントのパラメータ付き起動
|
main
|
[VS2005]Visual Studio 2005 で使用できるプロジェクトテンプレート整理 >>
2007/12/12 (水)
●
HTTP拡張モジュールでログ出力
ポストバックリクエスト時に一カ所でログ出力機能などを実装したい場合は HTTPモジュールを拡張して利用すると便利。
HTTPHandler拡張コードサンプル
Imports System Imports System.Web Imports System.Web.UI Imports Microsoft.VisualBasic '''
''' IHttpHandler 拡張クラス '''
'''
''' IHttpHandler のみだと ''' 「セッション状態は、構成ファイルまたは Page ディレクティブで enableSessionState が True に設定されている場合に限り使用できます。」 ''' というエラーになってしまうが、IRequiresSessionState も実装すると解消できる。 '''
Public Class IHttpHandlerExt Implements System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState '''
''' 別の要求で IHttpHandler インスタンスを使用できるかどうかを示す値を取得 '''
'''
'''
'''
''' IHttpHandler インスタンスが再利用できる場合は true。それ以外の場合は false '''
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return True End Get End Property '''
''' ASP.NET 状態サーバーがセッション データを処理するために使用します。 '''
'''
'''
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim handler As IHttpHandler = Nothing Dim url As String = context.Request.Url.AbsolutePath Dim path As String = context.Request.PhysicalPath '指定された仮想パスを使用して、コンパイルされたページのインスタンスを返す handler = PageParser.GetCompiledPageInstance(url, path, context) handler.ProcessRequest(context) End Sub End Class
Imports System.IO Imports System.Runtime.Serialization.Formatters.Binary Imports System.Web '''
''' ログ出力拡張HttpModule '''
'''
Public Class IHttpModuleLogExt Implements IHttpModule, System.Web.SessionState.IRequiresSessionState #Region "$ イベント" '''
''' IHttpModule を実装するモジュールで使用されるリソース (メモリを除く) を解放します。 '''
'''
Public Sub Dispose() Implements IHttpModule.Dispose 'リソースの解放処理など End Sub '''
''' モジュールを初期化し、要求を処理できるように準備します。 '''
'''
HttpApplicationオブジェクト '''
Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init 'ASP.NET イベント ハンドラ (ページ、XML Web サービスなど) の実行が完了すると発生するイベントに割り当て AddHandler application.PostRequestHandlerExecute, New EventHandler(AddressOf context_PostRequestHandlerExecute) End Sub '''
''' ASP.NET イベント ハンドラ (ページ、XML Web サービスなど) の実行が完了すると発生します。 '''
'''
'''
'''
Private Sub context_PostRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs) Dim app As HttpApplication = sender 'ログ出力機能の実装 'DebugSession(app) End Sub #End Region #Region "$ Session デバッグ例" '''
''' セッション情報をログ出力します '''
'''
Applicationオブジェクト '''
Private Sub DebugSession(ByVal app As HttpApplication) Dim stream As MemoryStream = New MemoryStream() Dim writer As BinaryWriter = New BinaryWriter(stream) Dim bf As BinaryFormatter = New BinaryFormatter Dim ctx As System.Web.HttpContext = HttpContext.Current Dim cnt As Integer Dim sbLog As New System.Text.StringBuilder Dim totalSize As Long = 0 Dim fileName As String Try 'ファイル拡張子チェック fileName = System.IO.Path.GetFileName(app.Request.Path) If Not (fileName.ToUpper.EndsWith("ASPX") OrElse fileName.ToUpper.EndsWith("ASMX")) Then Exit Sub End If Try If Not IsNothing(app.Request) Then sbLog.Append(vbCrLf).Append("【要求パス】").Append(app.Request.Path).Append(vbCrLf) End If Catch ex As System.Web.HttpException End Try For cnt = 0 To ctx.Session.Keys.Count - 1 bf.Serialize(writer.BaseStream, IIf(ctx.Session.Item(cnt) Is Nothing, "", ctx.Session.Item(cnt))) sbLog.Append("《" & (cnt + 1).ToString.PadLeft(3, "0") & "》") sbLog.Append(" [KeyName] : " & ctx.Session.Keys.Get(cnt)) sbLog.Append(" ") If IsNothing(ctx.Session.Item(cnt)) = True Then sbLog.Append(" [Type] : " & " ") sbLog.Append(" [Item] : " & "Nothing") Else sbLog.Append(" [Type] : " & ctx.Session.Item(cnt).GetType.FullName) sbLog.Append(" [Item] : " & ctx.Session.Item(cnt).ToString) End If sbLog.Append(" [Size] : " & stream.Length - totalSize & " byte" & vbCrLf) totalSize = stream.Length writer.Flush() Next sbLog.Append(" << SessionTotalSize >> " & stream.Length & " byte" & vbCrLf) If stream.Length > 500000 Then HttpContext.Current.Trace.Warn("セッションサイズが500KBを超えています。(Size:" & stream.Length & ")") End If 'テキスト書き込み処理などの実装 Catch e As Exception Finally writer.Close() stream.Close() writer = Nothing stream = Nothing bf = Nothing End Try End Sub #End Region End Class