Tuesday, January 31, 2012

Axosoft OnTime Web Event Provider – Create Incidents Automatically with WebEventProvider OnTimeWebEventProvider

Prerequisites:




  1. Make sure you have installed your OnTime SDK on a Web Server and that it is functional/tested




  2. Make sure you have your Security GUID Ready to talking to the OnTime SDK




  3. Create a project (C#) and create a web reference that points to
    “http://your sdk server url goes here/OnTimeSdk/IncidentService.asmx?op=AddIncident”
    and call it “OnTime”




  4. Make sure you have the following references:
    image




Here is the source code for the actual WebEventProvider:


 


using System;
using System.Collections.Specialized;
using System.Web.Management;
using OnTimeWebEventProvider.Properties;

namespace OnTimeWebEventProvider
{
public class OnTimeWebEventProvider : BufferedWebEventProvider
{
private const string StrOnTimeWebEventProvider = "OnTimeWebEventProvider";
private const string StrDescription = "description";
private const string StrProjectId = "ProjectID";
private const string StrCreatedById = "CreatedByID";
private const string StrWorkflowStepId = "WorkflowStepID";
private const string StrPriorityId = "PriorityID";
private const string StrStatusId = "StatusID";
private const string StrAssignedToId = "AssignedToID";
private const string StrSeverityId = "SeverityID";
private const string StrReportedById = "ReportedByID";
private const string StrEscalationLevelId = "EscalationLevelID";
private const string StrCustomerById = "CustomerByID";
private const string StrOnTimeGuid = "OnTimeGuid";
private const string StrMessage = "Message";

private Guid _providerSecurityToken;
private int _providerProjectId;
private int _providerCreatedById;
private int _providerWorkflowStepId;
private int _providerPriorityId;
private int _providerStatusId;
private int _providerAssignedToId;
private int _providerSeverityId;
private int _providerReportedById;
private int _providerEscalationLevelId;
private int _providerCustomerById;
private String _providerMessage;

public override void Initialize(string name, NameValueCollection config)
{
if (config == null)
throw new ArgumentNullException(Resources.NullConfig);

if (string.IsNullOrEmpty(name))
name = StrOnTimeWebEventProvider;

if (string.IsNullOrEmpty(config[StrDescription]))
{
config.Remove(StrDescription);
config.Add(StrDescription, Resources.DefaultDescription);
}

if (!string.IsNullOrEmpty(config[StrOnTimeGuid]))
{
this._providerSecurityToken = new Guid(config[StrOnTimeGuid]);
config.Remove(StrOnTimeGuid);
}

VerifyAndSetConfigSetting(config, StrProjectId, out this._providerProjectId);

VerifyAndSetConfigSetting(config, StrCreatedById, out this._providerCreatedById);

VerifyAndSetConfigSetting(config, StrWorkflowStepId, out this._providerWorkflowStepId);

VerifyAndSetConfigSetting(config, StrPriorityId, out this._providerPriorityId);

VerifyAndSetConfigSetting(config, StrStatusId, out this._providerStatusId);

VerifyAndSetConfigSetting(config, StrAssignedToId, out this._providerAssignedToId);

VerifyAndSetConfigSetting(config, StrSeverityId, out this._providerSeverityId);

VerifyAndSetConfigSetting(config, StrReportedById, out this._providerReportedById);

VerifyAndSetConfigSetting(config, StrEscalationLevelId, out this._providerEscalationLevelId);

VerifyAndSetConfigSetting(config, StrCustomerById, out this._providerCustomerById);

if (!string.IsNullOrEmpty(config[StrMessage]))
{
this._providerMessage = config[StrMessage];
config.Remove(StrMessage);
}
else
{
this._providerMessage = String.Empty;
}

base.Initialize(name, config);
}

private static void VerifyAndSetConfigSetting(NameValueCollection config, string strConfigId, out int intProviderProjectId)
{
intProviderProjectId = 0;
if (!string.IsNullOrEmpty(config[strConfigId]))
if (int.TryParse(config[strConfigId], out intProviderProjectId))
config.Remove(strConfigId);
}

public override void ProcessEventFlush(WebEventBufferFlushInfo flushInfo)
{
foreach (WebBaseEvent anEvent in flushInfo.Events)
{
OnTime.Incident incident =
new OnTime.Incident
{
CreatedById = this._providerCreatedById,
WorkflowStepId = this._providerWorkflowStepId,
ProjectId = this._providerProjectId,
Name =
String.Format("Exception in Application: {0} {1}",
anEvent.EventSource, this._providerMessage),
Description = anEvent.ToString(),
ReportedDate = DateTime.Now,
PriorityTypeId = this._providerPriorityId,
StatusTypeId = this._providerStatusId,
AssignedToId = this._providerAssignedToId,
SeverityTypeId = this._providerSeverityId,
ReportedById = this._providerReportedById,
EscalationLevelId = this._providerEscalationLevelId,
ReportedByCustomerContactId = this._providerCustomerById
};

OnTime.IncidentHandler incidentHandler = new OnTime.IncidentHandler();

incidentHandler.AddIncident(this._providerSecurityToken, incident);
}
}
}
}


Here is an example use of the OnTimeWebEventProvider:


Make sure that you have a reference to the OnTimeWebEventProvider assembly (project or compiled assembly) so that it is properly copied to the ASP.NET’s directory to run the example.


(ASP.NET HTML)


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestOnTimeWebEventProvider._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<
title></title>
</
head>
<
body>
<
form id="form1" runat="server">
<
div>
<
asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</
div>
</
form>
</
body>
</
html>


(ASP.NET .ASPX)


using System;

namespace TestOnTimeWebEventProvider
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }

protected void Button1_Click(object sender, EventArgs e)
{
throw new Exception("Wow, something really bad just blew up!");
}
}
}


(ASP.NET web.config)


<?xml version="1.0"?>
<
configuration>
<
appSettings/>
<
connectionStrings/>
<
system.web>
<
healthMonitoring>
<
providers>
<
add name="OnTimeWebEventProvider"
type="OnTimeWebEventProvider.OnTimeWebEventProvider"
bufferMode="Notification"
buffer="true"
OnTimeGuid="your OnTime SDK guid goes here"
ProjectID="24"
WorkflowStepID="13"
CreatedByID="1"
PriorityID="1"
StatusID="1"
AssignedToID="2"
SeverityID="2"
ReportedByID="2"
EscalationLevelID="0"
CustomerByID="0"
Message="TEST"/>
</
providers>
<
rules>
<
add name="LogAllEvents" eventName="All Errors" provider="OnTimeWebEventProvider"/>
</
rules>
</
healthMonitoring>
<
compilation debug="false" targetFramework="4.0"/>
<
authentication mode="Windows"/>
<
pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</
system.web>
</
configuration>


That’s all folks, feedback welcome

No comments:

Post a Comment