using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace GenClassLibrary
{
    public class ExceptionHandler
    {
        public ExceptionHandler()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static void WriteException(string errMessage, string lineNo)//FOR WRITING ERRORS
        {
            try
            {

                //string assemblyFile = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
                //string PathToClassLibPro = Path.GetDirectoryName(assemblyFile);
                bool exists = System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/Error"));
                if (!exists)
                    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/Error"));

                string path = HttpContext.Current.Server.MapPath("/Error/") + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";
                if (!File.Exists(path))
                {
                    File.Create(path).Close();
                    File.Create(path).Dispose();
                }
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.WriteLine("\r\nLog Entry : ");
                    sw.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                    string err = "Error in " + System.Web.HttpContext.Current.Request.Url.ToString() + "\r\nError Message : " + errMessage + "\r\nLine No : " + lineNo;
                    sw.WriteLine(err);
                    sw.WriteLine("_____________________");
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception ex)
            {
                //WriteException(ex.Message, ex.StackTrace);
            }
        }
    }
}
