ASP.NET Health Monitoring is a useful tool for logging error information that could ease diagnosing problems in a deployed application.
I have found this great article at Microsoft for configuring health monitoring feature in web projects.
Logging Error Details with ASP.NET Health Monitoring (C#)
Although builtin providers are more than enough to provide a rich information about the state of application. But there could be a chance you need to create custom provider to log additional information or additional storage option. Like in my case we need to log data in separate database which our application do not have direct access, but we have to call WCF service which would store data in target database. For this requirement I have written my custom provider.
Before proceeding to write code for custom provider, I recommend you to review the above mentioned article for health monitoring which will give you a strong background information.
Lets start writing custom provider class.
public class WCFCustomWebEventProvider : WebEventProvider { //WCF service's client object. MiddleWare.ServiceClient _client = new MiddleWare.ServiceClient(); public override void Initialize(String name, System.Collections.Specialized.NameValueCollection config) { base.Initialize(name, config); } public override void Flush() { } public override void ProcessEvent(WebBaseEvent raisedEvent) { //prepare StringBuilder with required information from WebBaseEvent object StringBuilder sb = new StringBuilder(); sb.Append("EventId: " + raisedEvent.EventID + ", "); sb.Append("EventTime: " + raisedEvent.EventTime + ", "); sb.Append("EventTimeUtc: " + raisedEvent.EventTimeUtc + ", "); sb.Append("EventType: " + raisedEvent.GetType().Name + ", "); sb.Append("EventSource: " + raisedEvent.EventSource + ", "); sb.Append("EventSequence: " + raisedEvent.EventSequence + ", "); sb.Append("EventOccurrence: " + raisedEvent.EventOccurrence + ", "); sb.Append("EventCode: " + raisedEvent.EventCode + ", "); sb.Append("EventDetailCode: " + raisedEvent.EventDetailCode + ", "); sb.Append("Message: " + raisedEvent.Message + ", "); sb.Append("ApplicationPath: " + WebBaseEvent.ApplicationInformation.ApplicationPath + ", "); sb.Append("ApplicationVirtualPath: " + WebBaseEvent.ApplicationInformation.ApplicationVirtualPath + ", "); sb.Append("MachineName: " + WebBaseEvent.ApplicationInformation.MachineName + ", "); sb.Append("RequestUrl: " + HttpContext.Current.Request.Url.AbsoluteUri + ", "); sb.Append("Details: " + raisedEvent.ToString()); //pass the string to WCF client object. _client.CreateLog(sb.ToString()); } public override void Shutdown() { } }
I have tried to keep it simpler to meet basic requirements, and avoid additional changes which you may need for performance requirements like make use of buffering.
Here, in the ProcessEvent
override method, I prepared a string variable build up the required information from
argument WebBaseEvent raisedEvent
. Then the final string message containing all required logging details
is then passed to the WCF client. Putting all the logging information in a single string variable is obviously not a good
approach, I did here only to keep things simple and try to focus on the logic to add custom provider.
A better approach could be to split data of our interest in separate variables and update the WCF method to accept
multiple parameters.
Here we are done with custom provider's C# code.
I assumed that you have read the above mentioned article, so now I will move to the web.config part.
Add the following healthMonitoring
tag inside system.web
.
<healthMonitoring enabled="true"> <providers> <add name="WCFCustomWebEventProvider_1" type="WCFCustomWebEventProvider" /> </providers> <rules> <add name="All Events Rule" eventName="All Events" provider="WCFCustomWebEventProvider_1" minInstances="1" maxLimit="Infinite" minInterval="00:00:00" custom="" /> </rules> <eventMappings> <add name="All Events" type="System.Web.Management.WebBaseEvent,System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647" /> </eventMappings> </healthMonitoring>
For simplicity, I am logging All Events
, and also removed the configuration sections for
bufferModes
and profiles
which you can add if you need more control over logging. Note that
I have mentioned the custom provider WCFCustomWebEventProvider
in the type
attribute while adding provider.
At-minimum, health monitoring needs you to configure three basic elements.
-
Providers
: You have to specify a provider which will decide the storage target and do the work to store logging information. You can also define multiple providers like for some critical errors, you need to log information in the database and also want to generate an email to the system administrator. In our exmaple, we have used only one custom provider which will store information by calling WCF client method. -
EventMappings
: It contains all the events names we are interested to log information about. In our example I placed a single event nameAll Events
, which enables the application to log data aboutall events
. If you dont need to log data about all events, you can define the desired events according to the requirements.Some other available events are:
- All Events
- Heartbeats
- Application Lifetime Events
- Request Processing Events
- Infrastructure Errors
- Request Processing Errors
- All Audits
- Failure Audits
- Success Audits
-
Rules
: Rule is basically a mapping you have to define, to decide which event should mapped to which provider. In our example, I have defined thateventName="All Events"
should be handled byprovider="WCFCustomWebEventProvider_1"
.
No comments:
Post a Comment