Title: Health monitoring in ASP'NET
1Health monitoring in ASP.NET
Session Code AS.01
2Agenda
- Instrumenting your application
- ASP.NET tracing
- Web Events
- Events and providers
- Rules and profiles
- Buffering
- Customizing web events
- Other instrumentation options
3Instrumenting your application
- An instrumented application is able to
- Signal specific events as they occur
- Provide an indication on performance and health
- Without instrumentation you have no clue what
your application is doing - Essentially you are looking at a black box
- Add instrumentation to your application from the
start
4Options for instrumentation
- .NET instrumentation
- Performance counters
- Application tracing
- ASP.NET 2.0 specific
- Provider based system for Web Events
- Tracing
- Windows OS
- Event tracing for Windows
5ASP.NET tracing
- Tracing facilities baked into ASP.NET
- Different from .NET tracing
- Trace output is appended at end of page
- Outputs key page lifetime events by default
6ASP.NET tracing configuration
- Specify default settings in web.config file
- Page directive overrules enabled and mode
ltconfigurationgt ltsystem.webgt lttrace
enabled"true" requestLimit"10"
localOnly"true" pageOutput"false"
mostRecent"true" traceMode"SortByTime"
writeToDiagnosticsTrace"false" /gt
lt/system.webgt lt/configurationgt
7Trace configuration details
- Limit number of requests
- requestLimit attribute
- Special section on Trace Information
- Sortable by time or category
- traceMode attribute
- Circular tracing
- Keeps most recent requests
- Specified by mostRecent"true"
8Viewing trace information
- Trace information available through special HTTP
handler trace.axd - Control visibility with attributes
- pageOutput appended to end of page (default)
- localOnly available from local machine only
- Redirect to .NET diagnostics
- writeToDiagnosticsTrace attribute of lttracegt
9Adding to trace output
- You can add information only to Trace Information
section - TraceContext allows programmatic access
- Inside page with Page.Trace property
- Always via HttpContext.Current.Trace
- Key methods are Write and Warn
- Warn uses red font for emphasis
- Take message and category
- WebPageTraceListener redirects .NET trace info to
ASP.NET trace
10Reading trace output
- Customize output generated by ASP.NET
- Handle TraceContext.TraceFinished event
- TraceContextRecord contains details
- Optional exception information
protected void Page_Load(object sender, EventArgs
e) this.Trace.TraceFinished new
TraceContextEventHandler(OnTraceFinished) void
OnTraceFinished(object sender, TraceContextEventAr
gs e) ICollection records e.TraceRecords
foreach (TraceContextRecord record in records)
11Web Events
12Health monitoring events
- Framework for monitoring status of running
applications and logging significant events - Health monitoring
- Security auditing
- Runtime observation
- Extensible and provider based model
- Enabled by default
- Configurable settings in Web.config
13Web Events
- ASP.NET comes with several key events
- Request, errors, auditing, heartbeat and
application lifetime events - All derive from WebBaseEvent base class
- Event types are mapped in Web.config
14Mapping events
- Web.config contains named events
- Differ by type and event code range
- Base classes can also be mapped
- All available event types are mapped by default
in machine-level web.config - Remove existing or add custom events
lteventMappingsgt ltremove name"Infrastructure
Errors" /gt ltadd name"Hack Attempts" type""
startEventCode"0" endEventCode"2147483647"
/gtlt/eventMappingsgt
15Raising web events
- Built-in web events are raised by ASP.NET runtime
- Raise (custom) web events yourself
- Instantiate new event object of desired type
- Make sure event code is offset by 100.000
(WebEventCodes.WebExtendedBase) - Call instance or static WebBaseEvent.Raise method
- Rules determine if event actually fires
16Event codes
- Event codes specify reason for raising of a
particular event - .NET Framework contains predefined event codes
- Ranges for various types of events
- Offset by base values
- WebEventCodes class has constants for codes and
offsets
17Rules
- A rule determines
- which event(s) is/are handled
- by which provider
- under what conditions
- To capture similar events use common base class
event - E.g. WebBaseErrorEvent for all errors events
- Throttle event raising by specifying minimum
interval between events
18Defining rules
- By default a rule fires if
- minimum interval is respected
- maximum limit is not exceeded
- Use custom rule for different evaluation
ltrulesgt ltadd name"Failure Audits Default"
eventName"Failure Audits" provider"EventLogProvi
der" minInstances"1" maxLimit"Infinite"
minInterval"000100" custom"" /gt lt/rulesgt
19Custom rules
- A custom rule contains alternate implementation
of firing logic - Custom type should implement IWebEventCustomEvalua
tor - Single method CanFire determines firing
- Decide based on RuleFiringRecord object
- Time event last fired
- Number of times event was raised before
- Specify custom type in custom attribute
20Profiles
- Named set of parameters for event logging
- Allows for a centralized administration
- Rules can refer to profile names
- Instead of defining parameters inside rule
ltprofilesgt ltadd name"Critical"
minInstances"1" maxLimit"Infinite"
minInterval"000000" custom""
/gt lt/profilesgt ltrulesgt ltadd name"Hack Attempts
Default" profile"Critical" eventName"Hack
Attempts" provider"" /gt lt/rulesgt
21Event providers
- Event providers are actually consumers
- Handle channelling of raised web events
- Available providers can channel to
- .NET diagnostics trace
- Event log
- Windows Management Instrumentation
- SMTP mail recipient
- predefined or templated mail
- SQL Server
22Details of an event
- Providers log details of events
- Certain details are common
- Time, ID, sequence, event code, machine name, app
path - Extra details are handled differently per event
and per provider - Custom event class should write extra details to
WebEventFormatter
23Creating custom events
- Derive from appropriate event base class
- Add extra properties for details
- Override FormatCustomEventDetails to output
details - Optionally override other methods
- Raise, ToString and IncrementPerfCounters
- Map event in configuration
- Add rules to channel event to provider
24Custom providers
- A custom web event provider ultimately derives
from WebEventProvider - Must implement
- Flush usually no logic needed
- ProcessEvent actual handling of event
- Shutdown cleanup of resources
- Decide whether buffering is needed
- Use base class BufferedWebEventProvider
- Do implement Flush method
25Buffering of events
- Buffering queues several events before executing
specific action - Prevents flooding by high-volume events
- Conveniently packs multiple events, e.g. mail
- Provider must derive from BufferedWebEventProvider
- Out-of-box only SQL Server and mail provider
26Flushing of buffers
- Buffers are flushed
- at regular intervals based on timer
- if urgency threshold is exceeded
- when requested by call to WebEventManager.Flush
on provider(s) - Flushes are performed by multiple threads up to a
certain maximum - Events are discarded if buffer overflows maximum
buffer size
27Buffering configuration
- Always set maxFlushSize less than maxBufferSize
- Use maxBufferThreads with caution
lthealthMonitoringgt ltbufferModesgt ltadd
name"Critical Notification" maxBufferSize"100"
maxFlushSize"20" urgentFlushThreshold"1"
regularFlushInterval"Infinite"
urgentFlushInterval"000100" maxBufferThreads"1
" /gt lt/bufferModesgt lt/healthMonitoringgt
28Logging to SQL Server
- Must use mixed mode authentication
- For full SQL Server only
- Cannot use Integrated Security
- By default uses SQL Express instance and
App_Data\AspNetDB.mdf database - Prepare database with scripts
- InstallCommon.sql
- InstallWebEventSqlProvider.sql
29Heartbeat
- Check condition of web application with heartbeat
- Raises events of type WebHeartbeatEvent
- Conveys statistics in WebProcessStatistics
- Memory usage
- Number of app domains and threads
- Requests executing, queued and rejected
- Beat interval determined at lthealthMonitoringgt
element
30More instrumentation options
31.NET tracing
- System.Diagnostics namespace contains
instrumentation types - Trace and Debug class allow output to listeners
- Register one or more listeners
- Create custom listeners for other output
32Performance counters
- .NET runtime uses Windows performance counters
for statistics - 95 ASP.NET specific counters available
- Counters categorized by FX and application
- .NET runtime itself has counters as well
- Ability to increment counters from managed code
33Event Tracing for Windows
- High performance unified logging system
- One-way packetized I/O
- Managed by kernel
- Characteristics
- Very low overhead
- Uniform way of handling log events
- No managed API prior to .NET 3.5
- Enterprise Instrumentation Framework (EIF) has
support for ETW
34Review
- Instrumenting your web app can give insights in
its health and performance - Several instrumentation options available in
ASP.NET - Simple scenarios can use ASP.NET Tracing
- Web events for advanced eventing
- Customizable framework
- Complemented by .NET Framework and OS diagnostics
35Questions
36Evaluation form
Vul je evaluatieformulier in en maak kans op een
van de prachtige prijzen!! Fill out your
evaluation form and win one of the great
prizes!! Session Code AS.01