水如烟

                 顺其自然,水到渠成 LzmTW

文或代码皆是面向初学者.我是爱好者,也是初学者.那些"文章",只按自己理解写,我是不知术语名词的.所以只供参考,也仅供参考.

导航

异类HOW TO:适时弹出指示框(六)

Posted on 2006-05-30 12:08  水如烟(LzmTW)  阅读(321)  评论(0编辑  收藏  举报
Author:水如烟

日志的实现:
MethodLogAttribute.vb
Namespace uRemoting.MethodWatcher
    
<AttributeUsage(AttributeTargets.Class)> _
    
Public Class MethodLogAttribute
        
Inherits MethodWatcherBaseAttribute

        
Protected Overrides Function GetMethodWatcherProperty() As MethodWatcherBaseProperty
            
Return New MethodLogProperty
        
End Function
    
End Class

End Namespace

MethodLogProperty.vb
Namespace uRemoting.MethodWatcher

    
Public Class MethodLogProperty
        
Inherits MethodWatcherBaseProperty

        
Protected Overrides Function CreateSink(ByVal nextSink As System.Runtime.Remoting.Messaging.IMessageSink) As System.Runtime.Remoting.Messaging.IMessageSink
            
Return New MethodLogSink(nextSink)
        
End Function

    
End Class

End Namespace

MethodLogSink.vb
Imports System.Runtime.Remoting.Messaging

Namespace uRemoting.MethodWatcher

    
Public Class MethodLogSink
        
Inherits MethodWatcherBaseSink

        
Sub New(ByVal nextSink As IMessageSink)
            
MyBase.New(nextSink)
        
End Sub

        
Protected Overrides ReadOnly Property MethodWatcherAppendAttributeType() As System.Type
            
Get
                
Return GetType(MethodLogAppendAttribute)
            
End Get
        
End Property
    
End Class

End Namespace

MethodLogAppendAttribute.vb
Namespace uRemoting.MethodWatcher
    
<AttributeUsage(AttributeTargets.Class, allowmultiple:=True)> _
    
Public NotInheritable Class MethodLogAppendAttribute
        
Inherits MethodWatcherAppendBaseAttribute

        
Sub New(ByVal methodName As String)
            
MyBase.New(methodName)
        
End Sub

    
End Class

End Namespace

MethodLogCenter.vb
Imports System.Runtime.Remoting.Messaging

Namespace uRemoting.MethodWatcher

    
Public Class MethodLogCenter
        
Inherits MethodWatcherCenter

        
Public Sub Ready()
            Remove()
            
AddHandler MethodCallBegin, New BeforeMethodCallHandle(AddressOf WriteMethodCallBegin)
            
AddHandler MethodCallOver, New AfterMethodCallHandle(AddressOf WriteMethodCallOver)
        
End Sub

        
Public Sub Remove()
            
RemoveHandler MethodCallBegin, New BeforeMethodCallHandle(AddressOf WriteMethodCallBegin)
            
RemoveHandler MethodCallOver, New AfterMethodCallHandle(AddressOf WriteMethodCallOver)
        
End Sub

        
Private Sub WriteMethodCallBegin(ByVal callMsg As IMethodCallMessage, ByVal MethodAppendType As System.Type)
            
If MethodAppendType.Equals(GetType(MethodLogAppendAttribute)) Then
                Console.WriteLine(
"{0}({1},{2})", callMsg.MethodBase.DeclaringType.FullName + "+" + callMsg.MethodName, callMsg.GetArg(0), callMsg.GetArg(1))
            
Else
                Console.WriteLine(
"MethodOtherBeing:" + MethodWatcherCommon.GetFullMethodName(callMsg, MethodAppendType))
            
End If

        
End Sub

        
Private Sub WriteMethodCallOver(ByVal replyMsg As IMethodReturnMessage, ByVal MethodAppendType As System.Type)
            
If MethodAppendType.Equals(GetType(MethodLogAppendAttribute)) Then
                Console.WriteLine(
"{0} Result is {1}", replyMsg.MethodBase.DeclaringType.FullName + "+" + replyMsg.MethodName, replyMsg.ReturnValue)
            
Else
                Console.WriteLine(
"MethodOtherOver:" + MethodWatcherCommon.GetFullMethodName(replyMsg, MethodAppendType))
            
End If
        
End Sub


    
End Class

End Namespace