Showing posts with label Utility. Show all posts
Showing posts with label Utility. Show all posts

Friday, September 23, 2011

Exception log simplified

One of very common problem is to keep log of exception/error which happens in production environment for debugging and quality improvement purpose.

There are many great logging tools/framework available in the market with many enhanced functionality. In most cases these detailed functionalities are not required.

Few weeks back, when this problem presented itself (to log exception detail to database or file) I thought let’s make things simpler.

Problem Description: Quickly log exception details without any 3rd party framework.

Conditions:

  1. Where to log exceptions details was controlled by system parameter and can be managed from UI.
  2. Exception log method should have ability to log more details if required and supplied.
Solution
  1. Method to get exception details for log
  2. Decide which logging method(s) to call
  3. Implementation of various logging methods


public static void ProcessError(Exception exception, string otherDetails = "")
{
 StackTrace stackTrace = new StackTrace();
 string methodName = stackTrace.GetFrame(1).GetMethod().Name;
 string className = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
 string classDetail = string.Format("{0}.{1}", className, methodName);
 string exceptionDetail = string.Format("Error occurred in {0}. {1}Message:{2}", 
  classDetail,
  Environment.NewLine,
  exception.Message
 );

 if (!string.IsNullOrWhiteSpace(otherDetails))
 {
  exceptionDetail = string.Format("{0}{1}Other Details: {2}.",
   exceptionDetail,
   Environment.NewLine,
   otherDetails
  );
 }
 // send this detail for saving
 LogError(exceptionDetail);
}

Here the trick is to get Calling method and class name for logging. We used following lines to do that job

// to get name of calling method
string methodName = stackTrace.GetFrame(1).GetMethod().Name; 

// to get name of calling class
string className = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name; 

Now lets decide where to save this detail



public enum LogMethods
{
 Database,
 File,
 Email
};


...
...


public static void LogError(string exceptionDetail)
{
 switch (errorLogMethod)
 {
  case LogMethods.Database:
   LogErrorInDatabase(exceptionDetail);
   break;
  case LogMethods.File:
   LogErrorInFile(exceptionDetail);
   break;
  case LogMethods.Email:
   SendErrorLog(exceptionDetail);
   break;
  case default:
   SendErrorLog(exceptionDetail);
   break;
 }
}

Now last step, implement save methods


Here is an example of saving detail in database. Similarly other methods can be implemented.


public static void logErrorMessageInDB(string exceptionDetail, string methodName)
{
 try
 {
  MyDataContext myDC = DataContext;

  Log lerror = new Log
  {
   FullDescription = exceptionDetail,
   MethodName = methodName,
   CreateDataTime = DateTime.Now
  };

  iDC.Log.InsertOnSubmit(lerror);
  iDC.SubmitChanges();
 }
 catch (Exception exp)
 {
  /*
   LogErrorInFile(exp);
   // OR
   LogErrorInEventLog(exp);
  */
 }
}

Wednesday, March 30, 2011

Join Multiple Text Files using Dirty Operating System (DOS)


Dirty Operating System (DOS)

Few days back I needed to join few (Thousand) text file as One file. I thought of
several options

  1. Open Notepad and copy paste files one by one :(
  2. Write a program which joins files.
  3. Download and use 3rd party program like TXTCollector
  4. Try something new ...

I remembered earlier (dark) days of DOS (Dirty Operation System) when screen was
black and text was white, everything was between 32 rows and 80 columns. Few of
DOS commands were very interesting like | (more) and > or >> (pipe). I thought to
do an experiment with that. I went to DOS prompt in same directory where all .txt
files were stored and used following command

type *.txt > MergedFile.txt

and ...

Yes !!! I got combined file named MergetFile.txt which was 14GB. There are few more
tricks like this

type firstFile.txt secondFile.txt thirdFile.txt fifthFile.txt > MergedFile.txt

This will join FirstFile.txt, SecondFile.txt, ThirdFile.txt and fifthFile.txt into
MergedFile.txt, this is useful when you want to join files in certain order.

Please note that each file is separated by a space. As you may already know, if
are in different folders please use full path instead of just file name.

type *.txt *.csv >MergedFile.txt

This will first join all files with .txt extension and then will join all files
with .csv extension to create a file called MergedFile.txt Here the magic command
is > which takes output of left side command and gives as input to right side command.
If used as >> then output gets be appended. As stated in
the principal of "Occam's razor" "The simplest explanation is
most likely the correct one"

Instead of writing my own code (spending few hours) or download or buy 3rd party
tool (spending money earned in few hours !!!) I used DOS to do the work. Hope this
helps Enjoy coding !!!