| using System; |
| using System.Configuration; |
| using System.IO; |
| using System.Text; |
| using System.Web; |
| using System.Xml; |
| |
| namespace Empresa.Framework.Web.ModulosHttp |
| { |
| /// |
| /// Capturar erros ocorridos no sistema |
| /// Referencia: http://www.dotnetscraps.com/dotnetscraps/post/ASPNETHow-to-write-error-messages-into-a-text-file-using-a-simple-httpModule.aspx |
| /// |
| public class CapturarErros : IHttpModule |
| { |
|
| #region IHttpModule Members |
| |
| /// |
| /// Dispose de IHttpModule |
| /// |
| public void Dispose(){} |
| |
| /// |
| /// Init de IHttpModule |
| /// |
| /// Context |
| public void Init(HttpApplication context) |
| { |
| context.EndRequest += new EventHandler(EndRequest); |
| } |
|
| #endregion |
| |
| /// |
| /// Evento EndRequest |
| /// |
| /// Sender |
| /// evento |
| protected void EndRequest(object sender, EventArgs e) |
| { |
| HttpApplication app = (HttpApplication)sender; |
| HttpContext context = app.Context; |
| |
| if (app.Context.AllErrors != null) |
| GravarErro(app.Context.AllErrors, context.Request); |
| } |
|
| #region LerArquivoErro |
| |
| /// |
| /// Criar o arquivo de erro caso não exista |
| /// |
| protected void LerArquivoErro(XmlDocument xmlDoc, string arquivoErro) |
| { |
| if (!File.Exists(arquivoErro)) |
| { |
| string diretorio = Path.GetDirectoryName(arquivoErro); |
| if (!Directory.Exists(diretorio)) |
| Directory.CreateDirectory(diretorio); |
| |
| using (XmlTextWriter xmlWriter = new XmlTextWriter(arquivoErro, Encoding.UTF8)) |
| { |
| xmlWriter.Formatting = Formatting.Indented; |
| |
| xmlWriter.WriteStartDocument(); |
| xmlWriter.WriteComment("Exceções ocorridas no sistema"); |
| |
| xmlWriter.WriteStartElement("excecoes"); |
| xmlWriter.WriteEndElement(); |
| } |
| } |
| xmlDoc.Load(arquivoErro); |
| } |
|
| #endregion |
|
| #region GravarErro |
| |
| /// |
| /// Gravar as exception em xml |
| /// |
| /// Exceptions ocorridas |
| /// Request |
| protected void GravarErro(Exception[] listaException, HttpRequest request) |
| { |
| string arquivoErro = String.Format(ConfigurationManager.AppSettings["Erro-Diretorio"], |
| request.PhysicalApplicationPath) + "\\Exception.xml"; |
| |
| XmlDocument xmlDocErro = new XmlDocument(); |
| LerArquivoErro(xmlDocErro, arquivoErro); |
| |
| foreach (Exception exception in listaException) |
| xmlDocErro.DocumentElement.AppendChild(CriarXmlErro(xmlDocErro, exception, request)); |
| |
| xmlDocErro.Save(arquivoErro); |
| } |
|
| #endregion |
|
| #region CriarXmlErro |
| |
| /// |
| /// Criar xml da exception |
| /// |
| /// Documento XML |
| /// Exception |
| /// Request |
| /// No Xml criado |
| protected XmlNode CriarXmlErro(XmlDocument xmlDoc, Exception exception, HttpRequest request) |
| { |
| XmlNode no = xmlDoc.CreateElement(exception.GetType().Name); |
| |
| XmlNode noHora = xmlDoc.CreateElement("hora"); |
| noHora.AppendChild(xmlDoc.CreateTextNode(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"))); |
| no.AppendChild(noHora); |
| |
| XmlNode noPagina = xmlDoc.CreateElement("pagina"); |
| noPagina.AppendChild(xmlDoc.CreateTextNode(request.FilePath)); |
| no.AppendChild(noPagina); |
| |
| CriarNosBasicosException(xmlDoc, no, exception); |
| |
| return no; |
| } |
|
| #region CriarNosBasicosException |
| |
| /// |
| /// Criar estrutura basica de nos de uma exception |
| /// |
| /// Documento XML |
| /// No Pai |
| /// Exception |
| protected void CriarNosBasicosException(XmlDocument xmlDoc, XmlNode noPai, Exception exception) |
| { |
| XmlNode noMensagem = xmlDoc.CreateElement("mensagem"); |
| noMensagem.AppendChild(xmlDoc.CreateTextNode(exception.Message)); |
| noPai.AppendChild(noMensagem); |
| |
| XmlNode noStackTrace = xmlDoc.CreateElement("stackTrace"); |
| noStackTrace.AppendChild(xmlDoc.CreateTextNode(exception.StackTrace)); |
| noPai.AppendChild(noStackTrace); |
| |
| AdicionarInnerException(xmlDoc, noPai, exception.InnerException); |
| } |
|
| #region AdicionarInnerException |
| |
| /// |
| /// Adicionar InnerException no Xml de Erro |
| /// |
| /// Documento Xml |
| /// No Pai |
| /// Exception |
| protected void AdicionarInnerException(XmlDocument xmlDoc, XmlNode noPai, Exception exception) |
| { |
| if (exception != null) |
| { |
| XmlNode noInnerException = xmlDoc.CreateElement(exception.GetType().Name); |
| CriarNosBasicosException(xmlDoc, noInnerException, exception); |
| |
| noPai.AppendChild(noInnerException); |
| } |
| } |
|
| #endregion |
|
| #endregion |
|
| #endregion |
| } |
| } |
| |