Views: 18.9K
Replies: 4
Archived
|
Logging Control Events in WinForms ApplicationIn 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 1Don'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 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 2Hi, 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 3You can use Logging Application Block.
http://msdn.microsoft.com/en-us/library/ff647183.aspx -Pramod Pramod Gupta, Jul 30, 2012
|
|
Reply 4You can use Logging Application Block.
http://msdn.microsoft.com/en-us/library/ff647183.aspx -Pramod Pramod Gupta, Jul 30, 2012
|