Dofactory.com
Dofactory.com
 Back to list
Views:   18.9K
Replies:  4
Archived

Logging Control Events in WinForms Application

In a Winforms Application with more than 100 forms a new requirement has come up.
Per requirement all the Events (button_Click, text_Changed ...) in any form needs to be logged to a log file.
This has to be done without adding new code to any of the forms. Meaning implementation has to be done globally.

Any framework available or suggestions would be appreciated.



Thanks
Vivek
vivekanand Adhikesavan, Jul 17, 2012
Reply 1

Don't use a control as your log at all. Instead write a log collection class that has the properties you desire and make visible false.

Then write the little bit of code that is needed to dump that collection to a variety of user interface elements like SendToEditControl and SendToListBox methods into logging object. Also add filtering capabilities to these methods.Update the UI log only as often as it makes sense and giving the best possible performance, and more importantly, letting you reduce the UI overhead when the log is changing rapidly. Don't tie your logging to a piece of UI, that's a mistake make future headache.

In the long run, a good UI for a logger is probably a custom control. But in the short run, you just want to disconnect your logging from any specific piece of UI. Most people use controls as data storage and only later realize that this is a mistake, and by then it's hard to unwind.

Thank you

Jom George, Aug 20, 2014
Reply 2
Hi, Vivek.

It's impossible without changing the forms. You would need to subclass and introduce your own events.

However, a catch-all solution would be to implement your own message filter by implementing IMessageFilter, and calling Application.AddMessageFilter(myMessageFilter) in your startup code.

In the message filter you could catch every event. Below is an example that I used in one of my apps (had to restrict the usage of function keys). Using this method though could be daunting if you're trying to catch events for certain input fields, and could impact the responsiveness of your app if the logic is lengthy.

Hope this helps,

Mike

In your startup code (Main sub): Application.AddMessageFilter(New MyMessageFilter())
.
.
.

Public Class MyMessageFilter
    Implements IMessageFilter

    ' WIN32 API:
    Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal nVirtKey As Integer) As Short
'
' Add mouse handler here too...
'

    Const WM_SYSKEYDOWN As Integer = &H104  ' System Key Down
    Const WM_TIMER As Integer = &H113       ' Timer message


    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
        If m.Msg <> WM_SYSKEYDOWN Then Return False ' Handle only system keys

        If GetKeyState(Keys.F4) < 0 Then
            Return True     ' We handled Alt-F4
        End If

       If GetKeyState(Keys.A) < 0 Then    ' Alt-A was pressed
            GlobalEvents.RaiseAltAPressed()
            Return True     ' We handled Alt-A it
        End If

        If GetKeyState(Keys.X) < 0 Then     ' Alt-X was pressed
            GlobalEvents.RaiseAltXPressed()
            Return True     ' We handled Alt-X
        End If

        If GetKeyState(Keys.M) < 0 Then     ' Alt-M was pressed
            GlobalEvents.RaiseShowMap(Nothing)
            Return True     ' We handled it
        End If

        Return False    ' We didn't handle the message. Pass it on.

    End Function

 
Michael Lopez, Aug 04, 2012
Reply 3
You can use Logging Application Block.

http://msdn.microsoft.com/en-us/library/ff647183.aspx

-
Pramod
Pramod Gupta, Jul 30, 2012
Reply 4
You can use Logging Application Block.

http://msdn.microsoft.com/en-us/library/ff647183.aspx

-
Pramod
Pramod Gupta, Jul 30, 2012
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.