Saturday 17 March 2012

Catch a Error log and write that error as Xml File

Try
          ErrorLog.Log("Price is coming as empty for product in Viewcart " & i.Split(",")(i.Split(",").Length - 1) & " " & HttpContext.Current.Request.Cookies("pgid").Value, Nothing)

.................
Catch ex As Exception
            ErrorLog.XMLError("GetFaceBookCode()", ex, False, "Error Occured in GetFaceBookCode() method")
End Try

========================================================================
Imports System.Web.Mail
Imports System.Web
Imports System.Web.HttpContext
Imports System.Configuration
Imports System.IO
Imports System.Xml
Imports GotDotNet.Elmah

Public Enum MessageType
  Trace = 1
  Log
End Enum
'Note: We need to define few keys in appSettings section in Web.config.
'Note: ErMsg is added to send the
Public Class ErrorLog

    Shared BrowserInfo As String = String.Empty
    Public Shared Sub XMLError(ByVal FunctionName As String, ByVal Er As Exception, Optional ByVal isEmail As Boolean = True, Optional ByVal ErMsg As String = "")
        Try
            If Er.Message.Contains("Thread was being aborted") Then
                Return
            End If
            Dim dirErrorLog As New DirectoryInfo(HttpContext.Current.Server.MapPath("~/ErrorLogs"))
            If Not dirErrorLog.Exists Then
                dirErrorLog.Create()
            End If
            If File.Exists(HttpContext.Current.Server.MapPath("~/ErrorLogs/Error-") & Format(Now, "yyyyMMdd") & ".xml") Then
                ErrorElementsName(Er, ErMsg)
            Else
                Dim writer As New XmlTextWriter(HttpContext.Current.Server.MapPath("~/ErrorLogs/Error-") & Format(Now, "yyyyMMdd") & ".xml", Nothing)
                writer.WriteStartDocument()
                writer.WriteStartElement(Nothing, "Errors", Nothing)
                writer.WriteEndElement()
                writer.WriteEndDocument()
                writer.Close()
                ErrorElementsName(Er, ErMsg)
            End If
            If isEmail Then
                Try
                    'Dim errorMail As Hashtable = ConfigurationManager.GetSection("gotdotnet.elmah/errorMail")
                    'Dim err As New GotDotNet.Elmah.Error(Er, HttpContext.Current)
                    'Dim strw As New StringWriter()
                    'Dim formatter As New GotDotNet.Elmah.ErrorMailHtmlFormatter()
                    'Dim strBody As String = strw.ToString()
                    'GotDotNet.Elmah.ErrorLog.Default.Log(err)
                    'formatter.Format(strw, err)
                    'strBody = strBody.Replace(vbCrLf, "")
                    'strBody = strBody.Replace(vbCr, "")
                    'strBody = strBody.Replace(vbLf, "")
                    Dim errorMail As Hashtable = ConfigurationManager.GetSection("gotdotnet.elmah/errorMail")
                    Dim err As New GotDotNet.Elmah.Error(Er, HttpContext.Current)
                    GotDotNet.Elmah.ErrorLog.Default.Log(err)
                    Dim strw As New StringWriter()
                    Dim formatter As New ErrorMailHtmlFormatter()
                    formatter.Format(strw, err)
                    Dim strBody As String = strw.ToString()
                    strBody = strBody.Replace(vbCrLf, "")
                    strBody = strBody.Replace(vbCr, "")
                    strBody = strBody.Replace(vbLf, "")
                    EmailUtil.SendMail(errorMail("from"), errorMail("to"), "", "", Rebex.Mail.MailPriority.High, err.Message.Replace("\r", " ").Replace("\n", " "), MailFormat.Html, strBody, "")
                Catch
                End Try
            End If
        Catch ex As Exception
        End Try
    End Sub
    Public Shared Sub Log(ByVal Er As String, ByVal MType As MessageType)
        'Try
        Dim dirErrorLog As New DirectoryInfo(HttpContext.Current.Server.MapPath("~/ErrorLogs"))
        If Not dirErrorLog.Exists Then
            dirErrorLog.Create()
        End If
        If File.Exists(HttpContext.Current.Server.MapPath("~/ErrorLogs/Log-") & Format(Now, "yyyyMMdd") & ".xml") Then
            LogElementsName(MType, Er)
        Else
            Dim writer As New XmlTextWriter(HttpContext.Current.Server.MapPath("~/ErrorLogs/Log-") & Format(Now, "yyyyMMdd") & ".xml", Nothing)
            writer.WriteStartDocument()
            writer.WriteStartElement(Nothing, "Logs", Nothing)
            writer.WriteEndElement()
            writer.WriteEndDocument()
            writer.Close()
            LogElementsName(MType, Er)
        End If

        'Catch ex As Exception
        'End Try
    End Sub
    Private Shared Sub ErrorElementsName(ByVal er As Exception, Optional ByVal ErMsg As String = "")
        'Added by Team7e for Issue#1519.
        BrowserInfo = "The browser type : " & HttpContext.Current.Request.Browser.Type & ", browser version : " & HttpContext.Current.Request.Browser.Version & "  and IP Address : " & HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")

        Dim doc As New XmlDocument
        doc.Load(HttpContext.Current.Server.MapPath("~/ErrorLogs/Error-" & Format(Now, "yyyyMMdd") & ".xml"))

        Dim el As XmlElement
        Dim passPhrase As String = "Ecommerce1CrAp5"
        Dim initVector As String = "@4B2c2E4e4F6g2326"
        Dim rijndaelKey As Rijndael = New Rijndael(passPhrase, initVector, 128, 0, 256)

        el = doc.CreateElement("Error")
        doc.DocumentElement.AppendChild(el)
        CreateChildElement(doc, "Error_Logged_On", Now.ToString, el)
        'CreateChildElement(doc, "Function_Name", FunctionName, el)
        'CreateChildElement(doc, "Error_Message", er.Message + ErMsg, el)
        CreateChildElement(doc, "Error_Message", rijndaelKey.Encrypt(er.Message + ErMsg), el)

        'Added by Team7e for Issue#1519.
        CreateChildElement(doc, "Browser_Info", BrowserInfo, el)


        'Return rijndaelKey.Encrypt(PlainText)
        CreateChildElement(doc, "Stack_Trace", rijndaelKey.Encrypt(er.StackTrace), el)
        'CreateChildElement(doc, "Stack_Trace", er.StackTrace, el)
        doc.Save(HttpContext.Current.Server.MapPath("~/ErrorLogs/Error-" & Format(Now, "yyyyMMdd") & ".xml"))
    End Sub
    Private Shared Sub LogElementsName(ByVal MType As MessageType, ByVal er As String)
        Dim doc As New XmlDocument
        doc.Load(HttpContext.Current.Server.MapPath("~/ErrorLogs/Log-" & Format(Now, "yyyyMMdd") & ".xml"))
        Dim el As XmlElement
        el = doc.CreateElement("Log")
        doc.DocumentElement.AppendChild(el)
        CreateChildElement(doc, "Logged_On", Now.ToString, el)
        'CreateChildElement(doc, "Function_Name", FunctionName, el)
        If MType = MessageType.Trace Then
            CreateChildElement(doc, "Type", "Trace", el)
        ElseIf MType = MessageType.Log Then
            CreateChildElement(doc, "Type", "Log", el)
        End If
        CreateChildElement(doc, "Message", er, el)
        doc.Save(HttpContext.Current.Server.MapPath("~/ErrorLogs/Log-" & Format(Now, "yyyyMMdd") & ".xml"))
    End Sub
    Private Shared Sub CreateChildElement(ByVal doc As XmlDocument, ByVal ElName As String, ByVal Text As String, ByVal el As XmlElement)
        Dim el1 As XmlElement = doc.CreateElement(ElName)
        el1.InnerText = Text
        el.AppendChild(el1)
    End Sub
    Private Shared Function GetTodayDate() As String
        Return Now.Year.ToString & "_" & Now.Month.ToString & "_" & Now.Day.ToString
    End Function
End Class
=======================================================================
Error Log OutPut..... in the file of  "Error-20120317.xml"

  <?xml version="1.0" ?>
- <Errors>
- <Error>
  <Error_Logged_On>3/17/2012 3:24:44 PM</Error_Logged_On>
  <Error_Message>Access to the path 'E:\Source\TFS\AIMSEasyShop\EasyShop2008Solution\EasyShop2008\App_Themes\Generic\style.css' is denied.</Error_Message>
  <Browser_Info>The browser type : Firefox10.0.2, browser version : 10.0.2 and IP Address : 192.10.250.53</Browser_Info>
  <Stack_Trace>at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path) at System.IO.File.OpenText(String path) at Admin_AllSettings.readCssFile() in E:\Source\TFS\AIMSEasyShop\EasyShop2008Solution\EasyShop2008\Admin\AllSettings.aspx.vb:line 1124 at Admin_AllSettings.Page_Load(Object sender, EventArgs e) in E:\Source\TFS\AIMSEasyShop\EasyShop2008Solution\EasyShop2008\Admin\AllSettings.aspx.vb:line 35</Stack_Trace>
  </Error>
</Errors>
 
==============================================================
 Log OutPut..... in the file of  "Log-20120317.xml"

  <?xml version="1.0" ?>
- <Logs>
- <Log>
  <Logged_On>3/17/2012 12:40:43 PM</Logged_On>
  <Type>Log</Type>
  <Message>Receipt has not been sent to Customer:sadathali2u@yahoo.co.in and Admin:.</Message>
  </Log>
  </Logs>

 

No comments:

Post a Comment