Logger Files in DotNet

Discussion in 'ASP.NET' started by naimish, Jul 3, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    Introduction



    Actually when any project is on production and when any error comes than that error is captured in log file. Very easy for programmer as well as client to know about the type of error.

    Background



    This is an ASP.NET file which is used to log the errors and warning in a file.

    The code



    Code:
    using System;
    using System.IO;
    using System.Threading;
    using System.Xml;
    using System.Text;
    using System.Web;
    namespace ChangeControl
    {
        /// <summary>
        /// Summary description for Logger.
        /// </summary>
        /// 
        public enum LOGMODE
        {
            VERBOSE = 0,
            INFO = 1,
            WARNING = 2,
            ERROR = 3
        }
        public class Logger
        {
            static AutoResetEvent logLock = new AutoResetEvent(true);
            static string sCommonLogFile;
            static string sErrorOnlyLogFile;
            //static string sNewAFEsLogFile;
            private string sLogFormat;
            private string sErrorTime;
            public Logger()
            {
                sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
    
                //this variable used to create log filename format "
                //for example filename : ErrorLogYYYYMMDD
                string sYear = DateTime.Now.Year.ToString();
                string sMonth = DateTime.Now.Month.ToString();
                string sDay = DateTime.Now.Day.ToString();
                sErrorTime = sYear + sMonth + sDay;
                //
                // TODO: Add constructor logic here
                //
            }
            public void ErrorLog(string sErrMsg)
            {
                string sPathName = HttpContext.Current.Server.MapPath("Logs/ErrorLog.txt");
                //StreamWriter sw = new StreamWriter(sPathName,true);
                StreamWriter sw = new StreamWriter(sPathName + sErrorTime, true);
                sw.WriteLine(sLogFormat + sErrMsg);
                sw.Flush();
                sw.Close();
            }
            public static void Log(LOGMODE mode, string logMessage)
            {
                try
                {
                    string LogMode = "ERROR";//XmlManager.GetInstance().Read("LogMode");
                    bool bLogIt = false;
                    if (LogMode == LOGMODE.ERROR.ToString())
                    {
                        bLogIt = (mode == LOGMODE.ERROR) ? true : false;
                    }
                    else if (LogMode == LOGMODE.WARNING.ToString())
                    {
                        bLogIt = (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING) ? true : false;
                    }
                    else if (LogMode == LOGMODE.INFO.ToString())
                    {
                        bLogIt = (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING || mode == LOGMODE.INFO) ? true : false;
                    }
                    else if (LogMode == LOGMODE.VERBOSE.ToString())
                    {
                        bLogIt = (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING || mode == LOGMODE.INFO || mode == LOGMODE.VERBOSE) ? true : false;
                    }
                    if (bLogIt)
                    {
                        logLock.WaitOne();
                        sCommonLogFile = @"C:\Inetpub\wwwroot\NewEmployee\NewEmployee\Logs" + DateTime.Now.ToString("ddMMMyyyy").ToUpper() + ".Log";
                        sErrorOnlyLogFile = @"C:\Inetpub\wwwroot\NewEmployee\NewEmployee\Logs" + "ERROR.Log";
                        Log(sCommonLogFile, logMessage);
                        if (mode == LOGMODE.ERROR || mode == LOGMODE.WARNING)
                            Log(sErrorOnlyLogFile, logMessage);
                        logLock.Set();
                    }
                }
                catch (Exception ex)
                {
                    Logger logger = new Logger();
                    logger.ErrorLog("Log - " + ex.Message + "::" + ex.StackTrace);
                }
            }
    
            public static void Log(string sFile, string logMessage)//protected
            {
                // Initialize Log File for First Time
                if (!File.Exists(sFile))
                {
                    // Create a file to write to.
                    using (StreamWriter sw = File.CreateText(sFile))
                    {
                        sw.WriteLine("+--------------------------------------------+");
                        sw.WriteLine("| Action Log |");
                        sw.WriteLine("+--------------------------------------------+");
                        sw.Flush();
                        sw.Close();
                    }
                }
                // Log all actions in common log file
                using (StreamWriter sw = File.AppendText(sFile))
                {
                    sw.Write("{0} : ", DateTime.Now.ToString());
                    sw.WriteLine(logMessage);
                    sw.Flush();
                    sw.Close();
                }
            }
        }
    }
     
  2. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    So, how does this work?
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    This is an ASP.NET file which you will need to integrate into your project.
     
  4. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    Yes, I understand that I need to integrate into my project.

    What else would be required?
    Do we need to add some more code for this?
    OR Do we need to make any change in settings?
     
  5. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    It's depends upon the project buddy....integrate it simply means the same.....either for little bit of code change to communitcate with it....settings I dnt think it's require untill and unless you go with .config file.
     
  6. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    I saved this file, add to the project.
    But can't reference it.
    Also can't access it's properties and methiods :(
     
  7. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    Don't know where am i going wrong
     
  8. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    You will need to pass certain parameters depending upon the code, plus you will need to integrate it properly in your project, please get help from someone who can show you live how to achieve this....the certain steps are different on difernet codes. May I tell you the steps, but I won't be able to exactly help you, otherwise, send me your project, I will integrate it into it and then will send it to you.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice