WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Logging with Custom Web Events
Pages: 1, 2, 3, 4

Buffering

The final piece that we've not yet looked at is buffering. It is just fine to have your messages go out or be logged instantly, but if you are creating audit messages that audit just about everything your system does, and if you have a lot of traffic, that can slow the system down noticeably. One mechanism for dealing with this drain on the system is to buffer the messages until either a given number accumulates, or a given period of time elapses.



As you can imagine, you may want to set different buffers for different providers, depending on how critical the events they handle are. Thus, you can create a bufferModes element and within that, any number of named BufferModes.

<bufferModes>
   <add name="Logging Critical Notification" 
      maxBufferSize="100" 
      maxFlushSize="20" 
      urgentFlushThreshold="1" 
      regularFlushInterval="Infinite" 
      urgentFlushInterval="00:01:00" 
      maxBufferThreads="1"/>
  
   <add name="Logging Notification"
      maxBufferSize="500" 
      maxFlushSize="50" 
      urgentFlushThreshold="5" 
      regularFlushInterval="Infinite" 
      urgentFlushInterval="00:02:00" 
      maxBufferThreads="1"/>
        
</bufferModes>

Here, I've created two buffer Mode objects, one named Logging Critical Notification, and one named Logging Notification. The former is for more critical messages, and so flushes after one minute. The urgentFlushThreshold is set to 1, indicating that just one event will flush the buffer and setting the urgentFlushInterval to 1, allowing a flush of the buffer after just one minute (you can find all the fields explained in detail in the documentation under BufferMode).

I've defined a second, less urgent bufferMode, Logging Notification, that will flush after five messages or two minutes and can be used for less critical notifications. Each of these can be assigned to a provider. As we saw earlier, the former is assigned to the MailEventProvider, and the latter to the SqlEventLog provider.

Testing

The easiest way to test the logging is to press the Divide By Zero button, whose event handler looks like this:

protected void ThrowException_Click
   ( object sender, EventArgs e )
{
   int x = 16;
   int y = 12;
   int z = 6;
   y = y - z;
   y -= z;
   try
   {
      int q =  x / y;  // divide by zero!
   }
   catch ( Exception ex )
   {
      LoggingErrorEvent le = new LoggingErrorEvent(
         "Exception raised in Default.aspx.cs", 
              this, loggingEventCode.Exception, ex );
      le.Raise();
   }

}

The key here is to catch the exception, create an instance of our custom event, send in the simple message (Exception raised in Default.aspx.cs), the page, the code enumeration (loggingEventCode.Exception), and the exception. The result is an entry in the database:

e902262e82da402c807196732632fa0f    2/28/2007 4:37:40 PM    2/28/2007 11:37:40 AM    LoggingErrorEvent    4    1    100001    0    
Exception raised in Default.aspx.cs    D:…\Logging\    /Logging    BERNSTEIN    NULL    System.DivideByZeroException    Event code: 100001
Event message: Exception raised in Default.aspx.cs
Event time: 2/28/2007 11:37:40 AM
Event time (UTC): 2/28/2007 4:37:40 PM
Event ID: e902262e82da402c807196732632fa0f
Event sequence: 4
Event occurrence: 1
Event detail code: 0
Application information:
    Application domain: 3df6b61b-14-128171542553125000
    Trust level: Full
    Application Virtual Path: /Logging
    Application Path: D:\...\Logging\
    Machine name: BERNSTEIN

Process information:
    Process ID: 6756
    Process name: WebDev.WebServer.EXE
    Account name: BERNSTEIN\Jesse

Exception information:
    Exception type: System.DivideByZeroException
    Exception message: Attempted to divide by zero.

Custom event details: 
    ASP.default_aspx
    Attempted to divide by zero.
    App_Web_-gksxlwk
    Exception stack trace...
       at _Default.Button1_Click(Object sender, EventArgs e) in d:\...\Logging\Default.aspx.cs:line 79

and an email:

Logging Example Warning!
** Summary **
---------------
This message contains events 1 to 1 from the total of 1 events scheduled for this notification.  There were 0 events left in the buffer at the beginning of this notification.


** Application Information **
---------------
Application domain: 3df6b61b-14-128171542553125000 Trust level: Full Application Virtual Path: /Logging Application Path: D:\...\Logging\ Machine name: BERNSTEIN


** Events **
---------------
Event code: 100001
Event message: Exception raised in Default.aspx.cs Event time: 2/28/2007 11:37:40 AM Event time (UTC): 2/28/2007 4:37:40 PM Event ID: e902262e82da402c807196732632fa0f Event sequence: 4 Event occurrence: 1 Event detail code: 0

Process information:
    Process ID: 6756
    Process name: WebDev.WebServer.EXE
    Account name: BERNSTEIN\Jesse

Exception information:
    Exception type: System.DivideByZeroException
    Exception message: Attempted to divide by zero.

Custom event details: 
    ASP.default_aspx
    Attempted to divide by zero.
    App_Web_-gksxlwk
    Exception stack trace...
       at _Default.Button1_Click(Object sender, EventArgs e) in d:\...\Logging\Default.aspx.cs:line 79

---------------
Please investigate ASAP.

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.


Return to the Windows Dev Center.