Monday 15 January 2018

Write Exception log on catch portion in asp.net C#

////////  Call exp log
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

        }
        catch (Exception ex)
        {
            WriteErrorLog(ex);
        }

    }

/////////// Write log

public static void WriteErrorLog(Exception ex)
    {
        var line = Environment.NewLine + Environment.NewLine;
        string ErrorlineNo = ex.StackTrace.Substring(ex.StackTrace.Length - 7, 7),
                  Errormsg = ex.GetType().Name.ToString(),
                   extType = ex.GetType().ToString(),
                    expUrl = HttpContext.Current.Request.Url.ToString(),
                    hostIp = "",
             ErrorLocation = ex.Message.ToString();

        if (HttpContext.Current.Request.UserHostAddress != null)
        {
            Int64 macip = new Int64();
            string macSrc = macip.ToString("X");
            if (macSrc == "0")
            {
                if (Convert.ToString(HttpContext.Current.Request.UserHostAddress) != "127.0.0.1")
                {
                    hostIp = Convert.ToString(HttpContext.Current.Request.UserHostAddress);
                }
            }
        }

        try
        {
            string filepath = HttpContext.Current.Server.MapPath("~/ExceptionDetailsFile/");  //Text File Path

            if (!Directory.Exists(filepath))
            {
                Directory.CreateDirectory(filepath);

            }
            filepath = filepath + DateTime.Today.ToString("dd-MM-yy") + ".txt";   //Text File Name
            if (!File.Exists(filepath))
            {


                File.Create(filepath).Dispose();

            }
            using (StreamWriter sw = File.AppendText(filepath))
            {
                string error = 
"Log Written DateTime :" + " " + DateTime.Now.ToString() + Convert.ToString(line) + 
"Error Line No. :" + " " + ErrorlineNo + Convert.ToString(line) + 
"Error Message :" + " " + Errormsg + Convert.ToString(line) + 
"Exception Type :" + " " + extType + Convert.ToString(line) + 
"Error Location :" + " " + ErrorLocation + Convert.ToString(line) + 
"Error Page Url :" + " " + expUrl + Convert.ToString(line) + 
"User Host IP :" + " " + hostIp + Convert.ToString(line);

                sw.WriteLine("----Exception On " + " " + DateTime.Now.ToString() + "----");
                sw.WriteLine("------------------------------------------------------------");
                sw.WriteLine(Convert.ToString(line));
                sw.WriteLine(Convert.ToString(error));
                sw.WriteLine("-------------*End*-------------");
                sw.WriteLine(Convert.ToString(line));
                sw.Flush();
                sw.Close();

            }

        }
        catch (Exception e)
        {
            e.ToString();
        }
    }

    

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...