A common headache
for all developers is how to handle errors? Sometimes it's very
difficult to find where the errors comes. So, Better solution is write custom log files.
Here I am sharing my custom handling techniques.
My
custom log files highlights are: 
- It stores all our logs in the specified folder with the log file name? We just have to give the log file path in the web.config file
 - Will get the classname, methodname, logged username and exception name for each generated log file.
 - Log files are stored with log file generated date/time. Here is my custom log file code:
 
public static class ErrorHandeling
{
  #region Private variables
   private static StreamWriter writerForErrLog;
  #endregion
  #region Log file names
   public const string Custom_LOGFILE_NAME = "CustomLogFile";
   public const string Custom_LOGFOLDER_NAME = "CustomErrorLOGS";
  #endregion
  #region HandleException
   /// 
   /// FUNCTION   : HandleException
   /// PURPOSE    : To create a log file to log the exceptions
   /// PARAMETERS : strWeb -> current web
   ///              strMethod -> A string containing the list name
   ///              strClass -> A string containing the name of class
   ///              strUser -> A string containing user name
   /// RETURNS    : None
   /// ----------------------------------------------------------------------
  ///  
public static void HandleException(Exception ex, string strWeb, string strMethod, string strClass, string strUser)
{
 try
 {
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
   string strDirPath = 
   System.Configuration.ConfigurationSettings.AppSettings["LogFilePath"];
   string strErrLogFilePath = 
   System.Configuration.ConfigurationSettings.AppSettings["LogFilePath"] 
   + "\\" + Custom_LOGFOLDER_NAME + "\\";
   strErrLogFilePath += Custom_LOGFILE_NAME 
               + "_" + DateTime.Now.Ticks.ToString() + ".log";
   if (!System.IO.File.Exists(strErrLogFilePath))
   {
    DirectoryInfo dirInfo = 
    new DirectoryInfo(strDirPath);
    dirInfo.CreateSubdirectory(Custom_LOGFOLDER_NAME);
   }
   //Creation of log file for execution entries  
   //------------------------------------------
    writerForErrLog = 
       new System.IO.StreamWriter(strErrLogFilePath);
    if (File.Exists(strErrLogFilePath))
    {
     writerForErrLog.WriteLine
     ("Web: " + strWeb + Environment.NewLine);
     writerForErrLog.WriteLine
     ("Method: " + strMethod + Environment.NewLine);
     writerForErrLog.WriteLine
     ("Class: " + strClass + Environment.NewLine);
     writerForErrLog.WriteLine
     ("User: " + strUser + Environment.NewLine);
     writerForErrLog.WriteLine
     ("Date: " + System.DateTime.Now + Environment.NewLine);
     writerForErrLog.WriteLine
     ("Message : " + ex.Message + Environment.NewLine);
     writerForErrLog.WriteLine
     ("Description : " + ex.StackTrace + Environment.NewLine);
     writerForErrLog.WriteLine("*****END****");
     writerForErrLog.Close();
    }
  });
 }
 catch (Exception exLog)
 {
  writerForErrLog.Close();
  }
 }
 }
}
See below code how to use the log file
in each method:
Public static void MethodName()
{
  try
  {
    //Code
  }
  catch (Exception ex)
  {
    ErrorHandeling.HandleException(ex, currentWeb.Url.ToString(),
    "MethodName", "Classname",
     currentWeb.CurrentUser.Name.ToString());
  }
Happy Coding...
Nice!!
ReplyDelete