Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Best Practice for Logging using Log4Net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.63k
    Comment on it

    As we all know that Logging is used to log the program's execution and this log information is used for debugging and testing purpose. One can easily find the problem using this log information without seeing the source code. To better write this log, one should recommend the best practices, which are as follows:

     

    1. Use unique object for each logger type.
      static log4net.ILog Log = log4net.LogManager.GetLogger(
                  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
              );
               

       

    2. Log each exception with relevant data.
    3. Log each and everything even if it is simple and smaller.
      try
        {
           int max =0;
           int percent = (100 * position) / max;
        }
      catch(Exception ex)
        { 
           Log.Error("Divide by Zero exception was raised",e);
        }

       

    4. Aggregates all Log & Exception data to one place.
    5. Log file should be placed at that place where it is easily available to everyone.
    6. Log file should be highly indexed and searchable.
    7. Do not write logs on network share, it should be in a file. The path of this file should be defined in the web.config file.
    8. Argument list should be validated with extra care.
    9. Argument list should be evaluated before logging method is called and should be guard with a check of IsDebugEnabled property.
       public void Execute( ITaskExecutionContext taskExecutionContext )
              {        
                  if( Log.IsDebugEnabled )
                    {
                        Log.DebugFormat( "Execution of task under context [{0}]...", ( null == taskExecutionContext  ? "null" : taskExecutionContext.Description ) );
                    }
              }

       

    10. Surround the argument placeholder ({0})  with brackets or parentheses, doing this would be easier to find the log.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: