By creating a list of application errors, you can take advantage of all the Sharepoint goodness like metadata, alerts, etc.
This post shows how to add a complete Error Management system for custom sharepoint errors, with alerts . It’s really pretty simple, use the existing Sharepoint lists and alerts to help you stay up to date with any application errors.
- Create a list to hold your application errors. See below for a screenshot.
- Title
- Application
- URL
- Message
- Inner Exception
- Source
- Stack Trace
2. From your custom code, create a utility method (I usually have this in a utility class) to add items into the Application Error list. Pass in the Error, the url, the Title, and the url to your web that contains the error list.
I tend to use constants as much as I can. Note that I also write the error into the Event Log, for another layer of reporting. Also, I usually always rethrow the error so that it shows as an error in my custom sharepoint workflows. Otherwise, it will say completed and you can easily think it finished without error.
public static void LogError(Exception ex,
string sWorkflowItemURL,
string sTitle,
string sWorkflowConfigurationUrlWeb)
{
if (!EventLog.SourceExists(“SharePoint Workflow”))
EventLog.CreateEventSource(“SharePoint Workflow”, “Application”);
string sMessage = string.Format(
@”Sharepoint List Item= {0},
Message = {1},
Inner Exception = {2},
Source = {3},
Stack Trace = {4}”,
sWorkflowItemURL,
ex.Message,
ex.InnerException,
ex.Source,
ex.StackTrace);
EventLog.WriteEntry(“SharePoint Workflow”,
sMessage,
EventLogEntryType.Error);
//Add error to SP list for alerts
//Open web
SPWeb web = new SPSite(sWorkflowConfigurationUrlWeb).OpenWeb();
//Create new Item
SPListItem item = web.Lists
item[“Title”] = sTitle;
item[“Application”] = “SharePoint Custom Workflow”;
item[“Url”] = sWorkflowItemURL;
item[“Message”] = ex.Message;
item[“Inner Exception”] = ex.InnerException;
item[“Source”] = ex.Source;
item[“StackTrace”] = ex.StackTrace;
//Save
item.Update();
web.Dispose();
}
- From your custom code, add the call in the try catch block and you are all set:
try
{
DoSomething();
}//
catch (Exception ex)
{
WFUtils.LogError(ex,
workflowProperties.Item.Url,
CustomConstant,
workflowConfigurationUrlWeb);
throw new Exception(ex.Message);
}//
A few notes here. I add an alert to the list to see when a new item comes on. A Production error comes straight into outlook and can be color coded red, using an Outlook rule. I use this mainly for custom Sharepoint workflow error handling, but this can be applied to any managed code that can access the SP objects, such as workflows, webparts, etc. Stay tuned, I will show you a trick to migrate code from Dev to Staging to PROD without hardcoding anything.
-Miles
Leave A Comment