Creating Logs and Error Handling methods in asp.net


using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Net;


namespace ACLData
{
    ///
    /// Summary description for ErrorHandler.
    ///
    public class clsErrorHandlerALL
    {
        //public void LoginFile(string SystemName,string Errordes ,long Errorno, string Errorsource, string Classname, string methodname);
        public clsErrorHandlerALL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Member Functions
        // This method creates text file and allows entry for Errors in runtime
        // Errordes:type of Exeption and its description
        // ErrorSorce:This is string of class name and method name where exeption has originated

        public enum Logtype
        {
            None = 0,
            file = 1,
            Database = 2,
            Mail = 3,
            SMS = 4,
            FileandDatabase = 5,
            All = file | Database | Mail | SMS,
        }

        public void ErrorsEntry(string Errordes, string Errorsource, long Errorno, string Classname, string Methodname, int Communicationid, Logtype logging)
        {

            switch (logging)
            {
                case Logtype.file:
                    {

                        LoginFile(System.Net.Dns.GetHostName(), Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        break;
                    }
                case Logtype.Database:
                    {
                        LoginDatabase(Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        break;
                    }
                case Logtype.Mail:
                    {
                        LogthroughMail(Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        break;
                    }
                case Logtype.SMS:
                    {
                        LogthroughSMS(Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        break;
                    }
                case Logtype.FileandDatabase:
                    {
                        LoginFile(System.Net.Dns.GetHostName(), Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        LoginDatabase(Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        break;
                    }

                case Logtype.All:
                    {
                        LoginFile(System.Net.Dns.GetHostName(), Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        LoginDatabase(Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        LogthroughMail(Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        LogthroughSMS(Errordes, Errorno, Errorsource, Classname, Methodname, Communicationid);
                        break;
                    }
            }
        }

        public void ErrorsEntry(string Errordes, string Errorsource, long Errorno, string Classname, string Methodname, Logtype logging)
        {

            switch (logging)
            {
                case Logtype.file:
                    {

                        LoginFile(System.Net.Dns.GetHostName(), Errordes, Errorno, Errorsource, Classname, Methodname);
                        break;
                    }
                case Logtype.Database:
                    {
                        LoginDatabase(Errordes, Errorno, Errorsource, Classname, Methodname);
                        break;
                    }
                case Logtype.Mail:
                    {
                        LogthroughMail(Errordes, Errorno, Errorsource, Classname, Methodname);
                        break;
                    }
                case Logtype.SMS:
                    {
                        LogthroughSMS(Errordes, Errorno, Errorsource, Classname, Methodname);
                        break;
                    }
                case Logtype.FileandDatabase:
                    {
                        LoginFile(System.Net.Dns.GetHostName(), Errordes, Errorno, Errorsource, Classname, Methodname);
                        LoginDatabase(Errordes, Errorno, Errorsource, Classname, Methodname);
                        break;
                    }

                case Logtype.All:
                    {
                        LoginFile(System.Net.Dns.GetHostName(), Errordes, Errorno, Errorsource, Classname, Methodname);
                        LoginDatabase(Errordes, Errorno, Errorsource, Classname, Methodname);
                        LogthroughMail(Errordes, Errorno, Errorsource, Classname, Methodname);
                        LogthroughSMS(Errordes, Errorno, Errorsource, Classname, Methodname);
                        break;
                    }
            }
        }

        public void LoginFile(string SystemName, string Errordes, long Errorno, string Errorsource, string Classname, string Methodname, int Communicationid)
        {
            string path = ConfigurationSettings.AppSettings["LogLocation"];
            string dir = path.Substring(0, path.LastIndexOf(@"\"));
            string file = path.Substring(0, path.LastIndexOf(".txt")) + "-" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";
            StreamWriter sw = File.AppendText(file);
            try
            {
                if (!(Directory.Exists(dir)))
                {
                    Directory.CreateDirectory(dir);
                }

                sw.WriteLine("====================" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
                sw.WriteLine("SystemName : " + SystemName);
                sw.WriteLine("Error Description : " + Errordes.ToString());
                sw.WriteLine("Error Source : " + Errorsource.ToString());
                sw.WriteLine("Errorno : " + Errorno.ToString());
                sw.WriteLine("Class Name : " + Classname);
                sw.WriteLine("Method Name : " + Methodname);
                sw.WriteLine("CommunicationId : " + Communicationid);
                sw.Flush();
                sw.Close();
            }
            catch (UnauthorizedAccessException ex)
            {
                //MessageBox.Show ("Contact Administrator for creating logs, Access denied !");
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                sw.Close();
            }
        }

        public void LoginFile(string SystemName, string Errordes, long Errorno, string Errorsource, string Classname, string Methodname)
        {
            string path = ConfigurationSettings.AppSettings["LogLocation"];
            string dir = path.Substring(0, path.LastIndexOf(@"\"));
            string file = path.Substring(0, path.LastIndexOf(".txt")) + "-" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";

            try
            {
                if (!(Directory.Exists(dir)))
                {
                    Directory.CreateDirectory(dir);
                }

                StreamWriter sw = File.AppendText(file);
                sw.WriteLine("====================" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
                sw.WriteLine("SystemName : " + SystemName);
                sw.WriteLine("Error Description : " + Errordes.ToString());
                sw.WriteLine("Error Source : " + Errorsource.ToString());
                sw.WriteLine("Errorno : " + Errorno.ToString());
                sw.WriteLine("Class Name : " + Classname);
                sw.WriteLine("Method Name : " + Methodname);

                sw.Flush();
                sw.Close();
            }
            catch (UnauthorizedAccessException ex)
            {
                //MessageBox.Show ("Contact Administrator for creating logs, Access denied !");
                Console.WriteLine(ex.ToString());
            }
        }

        private void LoginDatabase(string Errordes, long Errorno, string Errorsource, string Classname, string Methodname, int Communicationid)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@Errordescription", SqlDbType.VarChar, 500));
                cmd.Parameters["@Errordescription"].Value = Errordes;

                cmd.Parameters.Add(new SqlParameter("@Errorno", SqlDbType.Decimal));
                cmd.Parameters["@Errorno"].Value = Errorno;

                cmd.Parameters.Add(new SqlParameter("@SystemName", SqlDbType.VarChar, 50));
                cmd.Parameters["@SystemName"].Value = System.Net.Dns.GetHostName();

                cmd.Parameters.Add(new SqlParameter("@Errorsource", SqlDbType.VarChar, 100));
                cmd.Parameters["@Errorsource"].Value = Errorsource;

                cmd.Parameters.Add(new SqlParameter("@ClassName", SqlDbType.VarChar, 50));
                cmd.Parameters["@ClassName"].Value = Classname;

                cmd.Parameters.Add(new SqlParameter("@MethodName", SqlDbType.VarChar, 100));
                cmd.Parameters["@MethodName"].Value = Methodname;

                cmd.Parameters.Add(new SqlParameter("@LogTime", SqlDbType.DateTime));
                cmd.Parameters["@LogTime"].Value = System.DateTime.Now;

                cmd.Parameters.Add(new SqlParameter("@CommunicationId", SqlDbType.Int));
                cmd.Parameters["@CommunicationId"].Value = Communicationid;


                int res = ACLData.SQLHelper.ExecuteNonQuery(cmd, CommandType.StoredProcedure, "ACL.ins_ErrorLog");
            }
            catch (SqlException ex)
            {
                // res=0;
                ErrorsEntry(ex.GetBaseException().ToString(), "CallTaker", 12345, "test", ex.GetBaseException().Source, 1, clsErrorHandlerALL.Logtype.All);
            }
            catch (Exception ex)
            {
                // res=0;
                Console.Write(ex.GetBaseException().ToString());
            }
        }

        private void LoginDatabase(string Errordes, long Errorno, string Errorsource, string Classname, string Methodname)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@Errordescription", SqlDbType.VarChar, 500));
                cmd.Parameters["@Errordescription"].Value = Errordes;

                cmd.Parameters.Add(new SqlParameter("@Errorno", SqlDbType.Decimal));
                cmd.Parameters["@Errorno"].Value = Errorno;

                cmd.Parameters.Add(new SqlParameter("@SystemName", SqlDbType.VarChar, 50));
                cmd.Parameters["@SystemName"].Value = System.Net.Dns.GetHostName();

                cmd.Parameters.Add(new SqlParameter("@Errorsource", SqlDbType.VarChar, 100));
                cmd.Parameters["@Errorsource"].Value = Errorsource;

                cmd.Parameters.Add(new SqlParameter("@ClassName", SqlDbType.VarChar, 50));
                cmd.Parameters["@ClassName"].Value = Classname;

                cmd.Parameters.Add(new SqlParameter("@MethodName", SqlDbType.VarChar, 100));
                cmd.Parameters["@MethodName"].Value = Methodname;

                cmd.Parameters.Add(new SqlParameter("@LogTime", SqlDbType.DateTime));
                cmd.Parameters["@LogTime"].Value = System.DateTime.Now;

                //cmd.Parameters.Add(new SqlParameter("@CommunicationId", SqlDbType.Int));
                //cmd.Parameters["@CommunicationId"].Value = Communicationid;


                int res = ACLData.SQLHelper.ExecuteNonQuery(cmd, CommandType.StoredProcedure, "ACL.ins_ErrorLog");
            }
            catch (SqlException ex)
            {
                // res=0;
                ErrorsEntry(ex.GetBaseException().ToString(), "CallTaker", 12345, "test", ex.GetBaseException().Source, 1, clsErrorHandlerALL.Logtype.All);
            }
            catch (Exception ex)
            {
                // res=0;
                Console.Write(ex.GetBaseException().ToString());
            }
        }

        private void LogthroughMail(string Errordes, long Errorno, string Errorsource, string Classname, string Methodname)
        {
        }

        private void LogthroughMail(string Errordes, long Errorno, string Errorsource, string Classname, string Methodname, int Communicationid)
        {
        }

        private void LogthroughSMS(string Errordes, long Errorno, string Errorsource, string Classname, string Methodname, int Communicationid)
        {
        }

        private void LogthroughSMS(string Errordes, long Errorno, string Errorsource, string Classname, string Methodname)
        {
        }

        public static void LogEntry(string Description, TimeSpan Difference, string Logsource, string Classname, string Methodname, int Communicationid)
        {
            string path = ConfigurationSettings.AppSettings["LogEntry"];
            string dir = path.Substring(0, path.LastIndexOf(@"\"));
            string file = path.Substring(0, path.LastIndexOf(".txt")) + "-" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";
            if (!(Directory.Exists(dir)))
            {
                Directory.CreateDirectory(dir);
            }

            StreamWriter sw = File.AppendText(file);
            sw.WriteLine("====================" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
            sw.WriteLine("SystemName : " + System.Net.Dns.GetHostName().ToString());
            sw.WriteLine("Description : " + Description.ToString());
            sw.WriteLine("Source : " + Logsource.ToString());
            sw.WriteLine("Duration : " + Difference.ToString());
            sw.WriteLine("Class Name : " + Classname);
            sw.WriteLine("Method Name : " + Methodname);
            sw.WriteLine("CommunicationId : " + Communicationid);
            sw.Flush();
            sw.Close();

        }

        public static void LogEntry(string Description, TimeSpan Difference, string Logsource, string Classname, string Methodname)
        {
            string path = ConfigurationSettings.AppSettings["LogEntry"].ToString();
            string dir = path.Substring(0, path.LastIndexOf(@"\"));
            string file = path.Substring(0, path.LastIndexOf(".txt")) + "-" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";
            if (!(Directory.Exists(dir)))
            {
                Directory.CreateDirectory(dir);
            }

            StreamWriter sw = File.AppendText(file);
            sw.WriteLine("====================" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
            sw.WriteLine("SystemName : " + System.Net.Dns.GetHostName().ToString());
            sw.WriteLine("Description : " + Description.ToString());
            sw.WriteLine("Source : " + Logsource.ToString());
            sw.WriteLine("Duration : " + Difference.ToString());
            sw.WriteLine("Class Name : " + Classname);
            sw.WriteLine("Method Name : " + Methodname);
            //sw.WriteLine("CommunicationId : " + Communicationid);
            sw.Flush();
            sw.Close();
        }

      
    }
}

0 comments:

Post a Comment