Error log in catch block write Exception Detail in C#

This method helps you to trace or keeping log of your exception in a text file with a formatted manner. Just use as extension method and pass object of you Catch Exception obj.

 

public class ErrorLog
{
 public static void WriteError(Exception ex)
 {
  string err = "";
  try
  {

   string path = System.AppDomain.CurrentDomain.BaseDirectory + "/ErrorLog/" + 
   DateTime.Today.ToString("dd-MM-yy") + ".txt";

   if (!File.Exists(path))
   {
    File.Create(path).Close();
   }
  using (StreamWriter w = File.AppendText(path))
  {
    w.WriteLine("\r\nLog Entry : ");
    w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
    err = "Error Log at : " + ex.ToString() + "___. Error Message:" + ex.StackTrace;
    w.WriteLine(err);
    w.WriteLine("________________________________________________");
    w.Flush();
    w.Close();
  }
 }
    catch (Exception ex1)
    {
       return;
     }
  }
}


And you can use this extension method anywhere in catch block with class name.


try
{
  //your Code
}
catch(Exception ex)
{
  ErrorLog.WriteError(ex);
}

 

Best way to convert array of object into array of string in C#

I have an array of object in c#

object arrayofobject = new object[] {"Name", "Password", "Address"}

now i want to convert this array of object into array of string so i’ll use Array.CovertAll() method as string array.

string[] arr = Array.ConvertAll((object[])arrayofobject, Convert.ToString);

Full Code Here

object arrayofobject = new object[] {"Name", "Password", "Address"}
string[] arrayofstring = Array.ConvertAll((object[])arrayofobject, Convert.ToString);

now you can get arrayofstring.

Get Enum Description in C#

To get Enum description in c# here is a method or code.

For this first you have to import a namespace of Refletion because it is a part of Reflection.

using System.Reflection;

After importing namespace you can use reflection and get enum description.

public static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }
            else
            {
                return value.ToString();
            }
        }

Now you can use this method to get enum description.

GetEnumDescription(Enum value)

Here value that we are passing will be string as same as in enum name.

Program to Check Whether a String Palindrome is or not in c#

It contains a program to reverse a string and to check whether a Entered string is palindrome or not.

using System; 
namespace palindrome 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
string s,revs=""; 
Console.WriteLine(" Enter string"); 
s = Console.ReadLine(); 
for (int i = s.Length-1; i >=0; i--) //String Reverse 
{ 
revs += s[i].ToString(); 
} 
if (revs == s) // Checking whether string is palindrome or not 
{ 
Console.WriteLine("String is Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs); 
} 
else 
{ 
Console.WriteLine("String is not Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs); 
} 
Console.ReadKey(); 
} 
} 
}