Envoyer un mail lorsqu’une instance de processus tombe en erreur

Lorsqu’une instance de processus tombe en erreur, par défaut, une entrée est créée dans la liste des erreurs sur le K2 Workspace (l’interface d’administration de la plateforme K2). Les administrateurs K2 peuvent alors intervenir sur l’instance en question. Il est également possible de configurer K2 pour que dès qu’une erreur est soulevée dans un processus, cela crée une entrée dans l’observateur d’événements de Windows (je vous invite à vous référer au billet expliquant le fonctionnement des logs K2 pour cette configuration).

Ces méthodes, bien qu’intéressantes, sont principalement accessibles pour les équipes qui exploitent la plateforme K2. Il peut être intéressant que les administrateurs applicatifs (les propriétaires des applications K2) soient tenus au courant lorsqu’il y a un problème sur leur application. Nous allons donc voir dans le suite de ce billet comment procéder.

Il y a 2 (même 3) méthodes disponibles :

  1. l’installation d’un mini outil disponible sur notre espace communautaire,
  2. l’ajout d’une règle sur chacun de vos processus,
  3. utilisation des tâches planifiées Windows et d’un script PowerShell.

Méthode I – utilisation d’un outil

L’outil du market K2 Process Error Notification Service de Peter est une service windows à déployer sur le serveur K2, qui permet d’envoyer un mail dès qu’une instance tombe en erreur. L’outil est configurable processus par processus, ce qui est très pratique et permet d’adresser des administrateurs fonctionnels différents.

Son mode de fonctionnement : toutes les minutes, le service se connecte à K2 et vérifie, à l’aide des API K2, si une instance K2 est tombée en erreur. Si c’est le cas, il notifie les personnes concernées.

Voici 2 copies d’écran de l’outil :

Page de configuration de l'outil

Exemple de mail envoyé

L’outil fonctionne très bien, il n’est pas compliqué à configurer et la documentation attachée est complète. Le code source est également à disposition si vous souhaitez y faire des améliorations. Le principal inconvénient réside dans le fait qu’il faille installer l’outil sur le serveur K2 et qu’en fonction des polices de sécurité de votre organisation, vous n’aurez peut-être pas la permission de le faire. Le second inconvénient, mais il s’agit d’un point personnel, c’est que l’outil fonctionne en boucle et non pas sur capture d’événement ; il va donc consommer de la ressource “inutilement” et de manière régulière. Je rappelle cependant que sur une production, et si vos applications ont été correctement recettées, il ne devrait/doit pas y avoir d’erreurs trop souvent , donc la requête exécutée régulièrement ne devrait pas remonter trop d’éléments.

Bon, si vous ne pouvez pas utiliser cette méthode, passons à :

Méthode II – mise à jour des processus

Cette méthode a le mérite de fonctionner par capture d’événement, elle ne consommera donc de la ressource qu’en cas d’erreur (attention cependant en cas de boucle d’erreur, cela peut créer également un encombrement de messagerie ).

Son principe de fonctionnement : positionner une règle d’exception au niveau du processus, cette règle se déclenchera dès qu’une instance de processus tombera en erreur. Ensuite dans la personnalisation de la règle d’exception, il suffit de développer l’envoi d’un mail de notification avec les informations relatives à l’objet K2 qui a soulevé l’erreur, l’instance de processus et l’erreur elle-même. Une fois que le mail est envoyé, le code soulève à nouveau l’exception originale, de façon à ce que l’instance de processus reste en statut Error.

Cependant, contrairement à la 1ère méthode, celle-ci va nécessiter d’écrire du code, mais ne vous inquiétez pas, si écrire du code n’est pas votre tasse de thé, vous pourrez vous contenter de faire du copier/coller ou même de télécharger un modèle de projet K2 intégrant déjà la configuration ci-dessous, vous pourrez donc utiliser cette méthode sans avoir à mettre le nez dans le code et donc en utilisant uniquement K2 Studio.

Voici la procédure à suivre si vous souhaitez mettre cela en place sur vos processus :

  1. Ouvrez votre processus avec un Visual Studio sur lequel les composants K2 ont été activés.
  2. Cliquez sur l’icône correspondant à la règle d’exception du processus comme sur la copie d’écran ci-dessous :Process Exception Rule
  3. Sélectionnez Enable exception rule et cliquez sur View Code :Enable Exception Rule
  4. Cela ouvre une nouvelle fenêtre où vous voyez le code Workflow Foundation associé à la règle d’exception K2. Double-cliquez sur l’activité Properties pour accéder aux lignes de code :Enable Exception Rule
  5. Remplacez alors les lignes sélectionnées sur la copie d’écran ci-dessous par le code disponible en dessous de ce pas-à-pas :Code à remplacer
  6. Ajoutez un DataField au niveau des DataFields de processus : NotifyErrorMail, de type String avec comme valeur par défaut l’adresse mail de la personne que vous souhaitez notifier.DataFields à créer
  7. Il suffit alors de déployer cette nouvelle version de votre processus pour que dès qu’une erreur apparaît lors de l’exécution du processus, un mail soit envoyé à l’adresse spécifiée dans le champ NotifyErrorMail. Si vous souhaitez désactiver la notification par mail, il suffit de décocher la case Enable exception rule du point 3. Exemple de mail reçu : Mail de notification d'erreur

NB : le point 6 est optionnel, il permet simplement de ne plus avoir à retourner dans le code pour modifier le destinataire du mail de notification

Le code (enfin les 500+ lignes ) :

private void Properties_ExecuteCode(object sender, EventArgs e)
{
		K2.AddToErrorLog = K2.Configuration.IsErrorLog;
		K2.AddToServerLog = K2.Configuration.IsServerLog;

		// variables susceptibles d'etre mises a jour
		string mailServer = K2.StringTable["Mail Server"];
		string mailAddressFrom = K2.StringTable["From Address"];
		string mailAddressTo = K2.ProcessInstance.DataFields["NotifyErrorMail"].Value.ToString();
		string viewFlowURL = K2.StringTable["Workspace URL"] + "/ViewFlow/ViewFlow.aspx?ProcessID=";
		// fin de --- variables susceptibles d'etre mises a jour

		System.Net.Mail.MailMessage _expMail = null;
		System.Net.Mail.SmtpClient _smtpClient = null;
		System.Net.Mail.MailAddress _mailAddressFrom = null;
		System.Net.Mail.MailAddress _mailAddressTo = null;
		Exception ex = null;
		object _exContext = null;

		string _emailBody = string.Empty;
		bool _bFlag = true;

		try
		{

				_smtpClient = new System.Net.Mail.SmtpClient();
				_smtpClient.Host = mailServer;
				_smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
				_smtpClient.UseDefaultCredentials = true;

				_expMail = new System.Net.Mail.MailMessage();
				_mailAddressFrom = new System.Net.Mail.MailAddress(mailAddressFrom);
				_mailAddressTo = new System.Net.Mail.MailAddress(mailAddressTo);
				_expMail.From = _mailAddressFrom;
				_expMail.To.Add(_mailAddressTo);
				_expMail.Subject = "[Erreur K2] Instance de Processus " + K2.ProcessInstance.ID.ToString() + " en erreur";
				_expMail.IsBodyHtml = true;

				// Basic Message
				_emailBody += "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
				_emailBody += "<html>";
				_emailBody += "<head>";
				_emailBody += "<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\"/>";
				_emailBody += "<title>K2 Notification</title>";
				_emailBody += "</head>";
				_emailBody += "<body lang=\"Fr-fr\">";
				_emailBody += "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:20px;font-size:11.0pt;font-family:Calibri\" align=\"left\" valign=\"top\">Bonjour,</td></tr>";
				_emailBody += "<tr style=\"height:10px\"><td style=\"width:100%;\">&nbsp;</td></tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:20px;font-size:11.0pt;font-family:Calibri\" align=\"left\" valign=\"top\">L'instance [" + K2.ProcessInstance.Folio + "] du processus [" + K2.ProcessInstance.Process.FullName + "] vient de tomber en erreur. Ci-dessous les informations concernant le processus et l'erreur.</td></tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%;\">&nbsp;</td></tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:50px;font-size:10.0pt;font-family:Arial; font-weight:bold;\" align=\"left\" valign=\"top\">Détails de l'instance du processus</td></tr>";

				// Process Instance Details
				_emailBody += "<tr style=\"height:20px\">";
				_emailBody += "<td style=\"width:100%;\" align=\"left\" valign=\"top\">";
				_emailBody += "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
				_emailBody += "<tr style=\"height:25px\">";
				_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Identifiant</td>";
				_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\"><a href=\"" + viewFlowURL + K2.ProcessInstance.ID.ToString() + "\">" + K2.ProcessInstance.Folio + "<a/></td>";
				_emailBody += "</tr>";
				_emailBody += "<tr style=\"height:25px\">";
				_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Processus</td>";
				_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ProcessInstance.Process.FullName + "</td>";
				_emailBody += "</tr>";
				_emailBody += "<tr style=\"height:25px\">";
				_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">ID d'instance</td>";
				_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ProcessInstance.ID.ToString() + "</td>";
				_emailBody += "</tr>";
				_emailBody += "<tr style=\"height:25px\">";
				_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Demandeur</td>";
				_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ProcessInstance.Originator.Name + "</td>";
				_emailBody += "</tr>";
				_emailBody += "<tr style=\"height:25px\">";
				_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Date de démarrage</td>";
				_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ProcessInstance.StartDate.ToString() + "</td>";
				_emailBody += "</tr>";
				_emailBody += "</table>";
				_emailBody += "</td>";
				_emailBody += "</tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%;\">&nbsp;</td></tr>";

				// Process Data Fields
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:50px;font-size:10.0pt;font-family:Arial; font-weight:bold;\" align=\"left\" valign=\"top\">Valeur des DataFields</td></tr>";
				_emailBody += "<tr style=\"height:20px\">";
				_emailBody += "<td style=\"width:100%;\" align=\"left\" valign=\"top\">";
				_emailBody += "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";

				foreach (SourceCode.KO.DataField _dataField in K2.ProcessInstance.DataFields)
				{
						_emailBody += "<tr style=\"height:25px\">";
						_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + _dataField.Name + " [" + _dataField.FieldType.ToString() + "]</td>";
						_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + _dataField.Value.ToString() + "</td>";
						_emailBody += "</tr>";
				}

				_emailBody += "</table>";
				_emailBody += "</td>";
				_emailBody += "</tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%;\">&nbsp;</td></tr>";

				// Failure Details
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:50px;font-size:10.0pt;font-family:Arial; font-weight:bold;\" align=\"left\" valign=\"top\">Détails de l'erreur</td></tr>";
				_emailBody += "<tr style=\"height:20px\">";
				_emailBody += "<td style=\"width:100%;\" align=\"left\" valign=\"top\">";
				_emailBody += "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
				switch (K2.ContextType)
				{
						case SourceCode.KO.ContextType.ClientEvent:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.ClientEventContext)_exContext).ActivityInstanceDestination.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nom de l'event</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.ClientEventContext)_exContext).Event.Name + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.DestinationRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.DestinationRuleContext)_exContext).ActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Assignations</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">";
										foreach (SourceCode.KO.Destination _dest in ((SourceCode.KO.DestinationRuleContext)_exContext).Destinations)
										{
												_emailBody += " " + _dest.Name + " [" + _dest.Type.ToString() + "];";
										}
										_emailBody += "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Données du slot</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">";
										foreach (string _slotdata in ((SourceCode.KO.DestinationRuleContext)_exContext).SlotInstanceData)
										{
												_emailBody += " " + _slotdata + ";";
										}
										_emailBody += "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Queues dynamiques</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.DestinationRuleContext)_exContext).DynamicQueues.ToString() + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Résolution des queues à l'utilisateur</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.DestinationRuleContext)_exContext).ResolveQueuesToUsers.ToString() + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Instance Simple</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.DestinationRuleContext)_exContext).SingleInstance.ToString() + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nombre de slots</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.DestinationRuleContext)_exContext).Slots.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.EscalationAction:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EscalationActionContext)_exContext).ActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Redirection</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EscalationActionContext)_exContext).Redirect.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.EscalationRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EscalationRuleContext)_exContext).ActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.EventEscalationAction:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EventEscalationActionContext)_exContext).EventInstance.ActivityInstanceDestination.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nom de l'event</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EventEscalationActionContext)_exContext).EventInstance.Event.Name + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.EventEscalationRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EventEscalationRuleContext)_exContext).EventInstance.ActivityInstanceDestination.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nom de l'event</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EventEscalationRuleContext)_exContext).EventInstance.Event.Name + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.EventSucceedingRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EventSucceedingRuleContext)_exContext).EventInstance.ActivityInstanceDestination.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nom de l'event</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EventSucceedingRuleContext)_exContext).EventInstance.Event.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Règle de succession</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.EventSucceedingRuleContext)_exContext).SucceedingRule.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.IPCEvent:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.IPCEventContext)_exContext).ActivityInstanceDestination.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nom de l'event</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.IPCEventContext)_exContext).Event.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Processus IPC</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.IPCEventContext)_exContext).Process + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Chaîne de connexion IPC</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.IPCEventContext)_exContext).ConnectionString + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Synchrône</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.IPCEventContext)_exContext).Synchronous + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.LineRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Activité de démarrage</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.LineRuleContext)_exContext).LineInstance.StartActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Activité de terminaison</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.LineRuleContext)_exContext).LineInstance.FinishActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nom de ligne</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.LineRuleContext)_exContext).LineInstance.Line.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Règle de ligne</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.LineRuleContext)_exContext).LineRule.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.PrecedingRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.PrecedingRuleContext)_exContext).ActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Règle de précédence</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.PrecedingRuleContext)_exContext).PrecedingRule.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.ProcessEscalationAction:
						case SourceCode.KO.ContextType.ProcessEscalationRule:
						case SourceCode.KO.ContextType.ProcessFinishRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.ProcessStartRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Message</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.ProcessStartRuleContext)_exContext).Message + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Règle de démarrage</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.ProcessStartRuleContext)_exContext).Start.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.ServerEvent:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.ServerEventContext)_exContext).ActivityInstanceDestination.Activity.Name + "</td>";
										_emailBody += "</tr>";

										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Nom de l'event</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.ServerEventContext)_exContext).Event.Name + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.StartRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.StartRuleContext)_exContext).ActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";
										break;
								}
						case SourceCode.KO.ContextType.SucceedingRule:
								{
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Erreur soulevée par</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + K2.ContextType.ToString() + "</td>";
										_emailBody += "</tr>";

										_exContext = K2.ContextObject;
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Etape</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.SucceedingRuleContext)_exContext).ActivityInstance.Activity.Name + "</td>";
										_emailBody += "</tr>";
										_emailBody += "<tr style=\"height:25px\">";
										_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Règle de succession</td>";
										_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ((SourceCode.KO.SucceedingRuleContext)_exContext).SucceedingRule.ToString() + "</td>";
										_emailBody += "</tr>";
										break;
								}
						default:
								{
										_emailBody += "<td style=\"width:100%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\"> colspan=\"2\">Aucune données disponibles</td>";
										break;
								}
				}
				_emailBody += "</table>";
				_emailBody += "</td>";
				_emailBody += "</tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%;\">&nbsp;</td></tr>";


				// Exception Messages
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:50px;font-size:10.0pt;font-family:Arial; font-weight:bold;\" align=\"left\" valign=\"top\">Messages d'exception</td></tr>";
				_emailBody += "<tr style=\"height:20px\">";
				_emailBody += "<td style=\"width:100%;\" align=\"left\" valign=\"top\">";
				_emailBody += "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";

				ex = new Exception("NOTHING");
				ex = (Exception)K2.ExceptionObject;
				_bFlag = true;
				while (_bFlag == true)
				{
						if (ex.InnerException != null)
						{
								/* These exceptions dont have any meaningful information
								 *
								_emailBody += "<tr style=\"height:25px\">";
								_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Message</td>";
								_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ex.Message + "</td>";
								_emailBody += "</tr>";

								_emailBody += "<tr style=\"height:25px\">";
								_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Stack trace</td>";
								_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ex.StackTrace + "</td>";
								_emailBody += "</tr>";

								_emailBody += "<tr style=\"height:5px\" colspan=\"2\"><td style=\"width:100%;\">&nbsp;</td></tr>";
								*/
								ex = ex.InnerException;
						}
						else
						{
								_emailBody += "<tr style=\"height:25px\">";
								_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Message</td>";
								_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ex.Message + "</td>";
								_emailBody += "</tr>";

								_emailBody += "<tr style=\"height:25px\">";
								_emailBody += "<td style=\"width:25%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">Stack trace</td>";
								_emailBody += "<td style=\"width:75%; padding-left:50px;font-size:11.0pt;font-family:Arial;\" align=\"left\" valign=\"top\">" + ex.StackTrace + "</td>";
								_emailBody += "</tr>";
								_bFlag = false;
						}
				}
				_emailBody += "</table>";
				_emailBody += "</td>";
				_emailBody += "</tr>";

				_emailBody += "<tr style=\"height:30px\"><td style=\"width:100%;\">&nbsp;</td></tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:20px;font-size:11.0pt;font-family:Calibri\" align=\"left\" valign=\"top\">Cordialement,</td></tr>";
				_emailBody += "<tr style=\"height:20px\"><td style=\"width:100%; padding-left:20px;font-size:11.0pt;font-family:Calibri\" align=\"left\" valign=\"top\">Votre dévoué serveur K2</td></tr>";
				_emailBody += "</table>";
				_emailBody += "</body>";
				_emailBody += "</html>";
				_expMail.Body = _emailBody;
				_smtpClient.Send(_expMail);

		}
		catch (Exception exMail)
		{
				Console.WriteLine("[Process Error Notify] Error sending email: " + exMail.Message);
		}
		finally
		{
				_expMail = null;
				_smtpClient = null;
				_mailAddressFrom = null;
				_mailAddressTo = null;
				throw ((Exception)K2.ExceptionObject);
		}
}

Si vous ne pouvez pas écrire de code, vous pouvez télécharger ce modèle de modélisation K2, sur lequel la configuration ci-dessus a été appliquée. Il ne vous reste qu’à modifier la valeur du datafield NotifyErrorMail avec l’adresse mail de la personne à notifier en cas d’erreur.

Méthode III – Utiliser la planification de tâches Windows

Une autre solution peut consister à créer une tâche windows quotidienne qui va vérifier s’il y a des erreurs dans des instances K2 à l’aide d’un script PowerShell. Johnny décrit cette solution ici en .

Crédits

Ce billet est une traduction et une remise au goût du jour d’un billet d’Ashley qui a été tout d’abord diffusé sur feu K2underground, dans le blog Pitchblack et qui s’est inspiré d’une idée originale d’Eugène.

Une entrée de forum sur le sujet sur l’espace communautaire de K2 est disponible ici. Le code livré dans cet article est légèrement adapté de celui fournit sur le forum, il est traduit en français, mais des corrections et petites améliorations y ont été apportées.

jean

Directeur technique de K2 France depuis 2006 et passionné par les technologies, je travaille dans le monde du BPM et des applications métier depuis... que je travaille :). Vous pouvez également me suivre sur twitter, linkedin.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.