I, Me, Myself

My photo
Hyderabad, Hindhu/AndhraPradesh, India
CoOl,jOvial,strAight fOrward

Search

Tuesday, June 7, 2011

Error File

public static void WriteError(string errorMessage, Exception objException)
        {
            try
            {
                string path = "~/ErrorLogs/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
                if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
                {
                    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
                }
                using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
                {
                    w.WriteLine("\r\n Error Logs : ");

                    string err = "An Unhandled Exception Occured in: " + System.Web.HttpContext.Current.Request.Url.ToString() + "\r\n" +

                                  "\r\n Machine Name        : " + Dns.GetHostName().ToString() + "\r\n" +
                                  "\r\n Date                : " + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n" +
                                  "\r\n Method Name            : " + objException.TargetSite.Name.ToString() + "\r\n" +
                                  "\r\n Error Message        : " + objException.Message.ToString().Trim() + "\r\n" +
                                  "\r\n Stack Trace            : " + objException.StackTrace.ToString().Trim();
                    w.WriteLine(err);
                    w.WriteLine("^^-------------------------------------------------------------------^^");
                    w.Flush();
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                WriteError(ex.Message, ex);
            }

        }

No comments:

Followers