views:

47

answers:

2

Hi,

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
+1  A: 

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.

Scott Dorman
Sounds promising - I'll give it a go and get back to you
Basiclife
Sorry for the very slow response - This got back-burnered. That looks like it will do - Thank you. It still feels a little clunky but seems to be the best answer at the moment.
Basiclife
+2  A: 

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

Yauheni Sivukha
+1 Thanks - It's really useful to know that I can do this. I was hoping for something a little more automated (ie as things fire they're written to the console - I just want to see what's going on as the app runs)
Basiclife