开发者

Monitoring all events in a class and sub-classes

开发者 https://www.devze.com 2022-12-27 21:27 出处:网络
I wonder if someone can help me. I\'ve got a console App which I use to debug various components as I develop them.

I wonder if someone can help me. I've got a console App which I use to debug various components as I develop them.

I'd like to be able to log to the console every time an event is fired either in the object I've instantiated or in anything it's instantiated [ad infinitum]. I wouldn't see some of these events normally due to them being consumed further down the chain). Ideally I would be able to log all public and private events but if only public are possible, I can live with that.

I've Googled and all I can find is how to monitor a directory - So I'm not sure if this is not possible or simply has a name that I don't know.

The sort of information I'm after is similar to what's found in an exception - Target Site, 开发者_运维技巧Source, Stack Trace, etc...

Could I perhaps do this through reflection somehow?

If someone could tell me if this is even possible and perhaps point me at some good resources, I'd be very grateful.

Many thanks

Basic

To Give you an idea of the console App:

Sub Main()
    Container = ContainerGenerate.GenerateContainer()
    Dim TemplateID As New Guid("5959b961-b347-46bc-b1b6-cba311304f43")
    Dim Templater = Container.Resolve(Of Interfaces.Mail.IMailGenerator)()
    Dim MyMessage = Templater.GenerateMail(TemplateID, Nothing, Nothing)
    Dim MySMTPClient = Container.Resolve(Of SmtpClient)()
    MySMTPClient.Send(MyMessage)
    Finish()
End Sub


You can use Interception feature of Unity container to do tracing: http://www.alexthissen.nl/blogs/main/archive/2009/03/25/using-unity-to-do-poor-man-s-tracing.aspx

The call stack can also be extracted from any profiler. Like this: alt text http://i.msdn.microsoft.com/cc135981.dottrace_L(it-it).gif


There is nothing built in to the Framework that would allow you to do this. It is possible using reflection, although I'm not sure I would recommend it. The following code shows you to get the list of all members of a type:

  Try
     Dim myObject As New [MyClass]()
     Dim myMemberInfo() As MemberInfo

     ' Get the type of 'MyClass'.
     Dim myType As Type = myObject.GetType()

     ' Get the information related to all public member's of 'MyClass'. 
     myMemberInfo = myType.GetMembers()

     Console.WriteLine(ControlChars.Cr + "The members of class '{0}' are :" + ControlChars.Cr, myType)
     Dim i As Integer
     For i = 0 To myMemberInfo.Length - 1
        ' Display name and type of the concerned member.
        Console.WriteLine("'{0}' is a {1}", myMemberInfo(i).Name, myMemberInfo(i).MemberType)
     Next i

  Catch e As SecurityException
     Console.WriteLine(("Exception : " + e.Message.ToString()))
  End Try

In the loop, you would need to determine if the member is an event, and if so, hook an event handler to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消