1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Extensions
.ExceptionChaining
18 using System
.Configuration
;
21 using Castle
.Components
.Common
.EmailSender
;
24 /// A handle that sends the exception by email
26 public class EmailHandler
: AbstractExceptionHandler
, IConfigurableHandler
28 private String mailTo
;
29 private String mailFrom
= "donotreply@castleproject.org";
32 /// Implementors should check for known attributes and child nodes
33 /// within the <c>exceptionHandlerNode</c>
35 /// <param name="exceptionHandlerNode">The Xml node
36 /// that represents this handler on the configuration file</param>
37 public void Configure(XmlNode exceptionHandlerNode
)
39 XmlAttribute mailToAtt
= exceptionHandlerNode
.Attributes
["mailTo"];
41 if (mailToAtt
== null || mailToAtt
.Value
== String
.Empty
)
43 String message
="'mailTo' is a required attribute " +
44 "for EmailHandler (part of ExceptionChaining extension)";
45 throw new ConfigurationErrorsException(message
);
48 mailTo
= mailToAtt
.Value
;
50 XmlAttribute mailFromAtt
= exceptionHandlerNode
.Attributes
["mailFrom"];
52 if (mailFromAtt
!= null && mailFromAtt
.Value
!= String
.Empty
)
54 mailFrom
= mailFromAtt
.Value
;
59 /// Implementors should perform the action
60 /// on the exception. Note that the exception
61 /// is available in <see cref="IRailsEngineContext.LastException"/>
63 /// <param name="context"></param>
64 public override void Process(IRailsEngineContext context
)
66 IEmailSender emailSender
= (IEmailSender
) context
.GetService( typeof(IEmailSender
) );
68 String message
= BuildStandardMessage(context
);
72 emailSender
.Send( mailFrom
, mailTo
,
73 "MonoRail Exception: " + context
.LastException
.GetType().Name
, message
);
77 // We ignore errors during send