Friday, June 7, 2013

USE OF LOGGERS IN ASP.NET

The main purpose of creating logger file in our project is that, after deploying our project in to the server we are not able to check the errors or line by line debugging, In that time we have to know , what are the errors are coming when our project is running on the live(available in the website).

In that time if we use the below code in our project, then all the errors will be saved/stored in a text document in our system in a folder.

Why i wrote the path in web.config file means, when we are Hosting(up) our project in to server, in that time we will change only web.config file not the code.
You can save this log files in your database also.
-->We are creating our logger text file in E: Drive.(We can change the drive also)


<appSettings>
<add key="log" value="E:\\Logger.Log"/>
</appSettings>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.IO;

namespace Project
{
   public static class logger
    {
       public static void log(string className, string method, string message)
        {
            FileStream fs=null; StreamWriter sw=null;
            try
            {
                // Here we are calling the path from web.config file
                string str=ConfigurationManager.AppSettings["log"];
                fs = new FileStream(str, FileMode.Append);
                sw = new StreamWriter(fs);
                sw.WriteLine(DateTime.Now.ToString() + "\t" + className+ "\t" + method + "\t" + message);
                //Don't forgot to close Stream Writer before File Stream
                sw.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(sw!=null)
                    sw.Close();
                if(fs!=null)
                    fs.Close();
            }
        }
    }
}


// how we will use this Logger method in our project

public class MessageDB
    {
public void InsertMessage(long UID, MessageType Type, string Message, string Remarks)
        {
// logger is the class name and log is the method name
           logger.log(" MessageDB","InsertMessage","Begin");          
            try
            {
               // our code
                    logger.log(" MessageDB", "InsertMessage", "End");
                }
            }
            catch (Exception ex)
            {
                logger.log("MessageDB", "InsertMessage", "Exception" + ex.Message);              
               throw ex;
            }
            finally
            {

            }
        }


// how the file is stored in our directory
In the text file format it will store.
Logger.Log // text file(Logger.Log is file name)
the text inside of the file will be stored like below

--> If we wont get any error in our project means the text look like blow
6/8/2013 10:37:15 AM     " MessageDB",   "InsertMessage",   "Begin"
6/8/2013 10:37:15 AM    " MessageDB",    "InsertMessage",    "END"

--> If we got any error in our project means the text look like blow
6/8/2013 10:37:15 AM Login login Start
6/8/2013 10:37:15 AM UserBiz AuthenticateUser start
6/8/2013 10:37:15 AM UserBiz AuthenticateUser end
6/8/2013 10:37:15 AM UserDB AutenticateUser start
6/8/2013 10:37:15 AM UserDB AuthenticateUser MessageCannot open database "EmployeeDataBase" requested by the login. The login failed.


No comments:

Post a Comment