Не всегда бывает, что все идет именно так как планировалось. И когда возникают ошибки - их нужно записать в лог и потом исправить. Для логгирования ошибок в SharePoint workflow можно использовать следующий код
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint.Workflow;
namespace HubKey.DevelopmentHole
{ public sealed partial class MyWorkflow: SequentialWorkflowActivity
{ //assign workflowProperties in the OnWorkflowActivated activity
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
{ try
{ workflowProperties.Workflow.CreateHistoryEvent(
(int)SPWorkflowHistoryEventType.WorkflowError,
0,
workflowProperties.OriginatorUser,
"Nothing good",
string.Format("An exception occured during activity {0}. Check the item data for more information.", executionContext.Activity.Name), Misc.FormatException(exception));
}
catch { } return base.HandleFault(executionContext, exception);
}
}
public static class Misc
{ static Regex stackRegex = new Regex(@"\r\n at ", RegexOptions.Compiled);
public static string FormatException(Exception ex)
{ return FormatException(ex, new StringBuilder(), 0);
}
private static string FormatException(Exception ex, StringBuilder sb, int tab)
{ if (ex == null)
return sb.ToString();
string sTab = new string('\t', tab); sb.AppendFormat("{0}Source: {1}\r\n", sTab, ex.Source); sb.AppendFormat("{0}Message: {1}\r\n", sTab, ex.Message); sb.AppendFormat("{0}StackTrace: {1}\r\n", sTab, FormatStackTrace(ex, sTab)); sb.AppendLine();
return FormatException(ex.InnerException, sb, ++tab);
}
private static string FormatStackTrace(Exception ex, string sTab)
{ string stack = ex.StackTrace;
if (stack == null)
return null;
stack = stackRegex.Replace(ex.StackTrace, delegate(Match m)
{ return string.Format("\r\n{0} at ", sTab); });
return stack;
}
}