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

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...