Monday 17 July 2017

Write exception log in asp.net c#

// Method

private static String ErrorlineNo, Errormsg, extype, exurl, hostIp, ErrorLocation, HostAdd;


public static void Write_Logs(Exception ex)
    {
        var line = Environment.NewLine + Environment.NewLine;

        ErrorlineNo = ex.StackTrace.Substring(ex.StackTrace.Length - 7, 7);
        Errormsg = ex.GetType().Name.ToString();
        extype = ex.GetType().ToString();
        exurl = context.Current.Request.Url.ToString();
        ErrorLocation = ex.Message.ToString();

        try
        {
            string filepath = context.Current.Server.MapPath("~/Ex_Log/");  //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))
            {
                sw.WriteLine(line);
                string error = "Date:" + " " + DateTime.Now.ToString() + "," + "LineNo :" + " " + ErrorlineNo + "," + "Message:" + " " + Errormsg + "," + "ExceptionType:" + " " + extype + "," + "Location :" + " " + ErrorLocation + "," + "PageUrl:" + " " + exurl + "," + "HostIP:" + " " + hostIp;
                sw.Flush();
                sw.Close();

            }

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

        }
    }



//Call


    protected void btn_backCatalogue_Click(object sender, EventArgs e)
    {
        try
        {
        }
        catch (Exception ex)
        {
            Write_Logs(ex);
        }
    }

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 ,","...