Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Logger Files in DotNet (http://www.go4expert.com/articles/logger-files-dotnet-t18316/)

naimish 3Jul2009 09:00

Logger Files in DotNet
 

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: CSharp

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();
            }
        }
    }
}


nimesh 3Jul2009 22:50

Re: Logger Files in DotNet
 
So, how does this work?

naimish 6Jul2009 10:27

Re: Logger Files in DotNet
 
This is an ASP.NET file which you will need to integrate into your project.

nimesh 6Jul2009 15:05

Re: Logger Files in DotNet
 
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?

naimish 6Jul2009 15:07

Re: Logger Files in DotNet
 
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.

nimesh 6Jul2009 15:22

Re: Logger Files in DotNet
 
I saved this file, add to the project.
But can't reference it.
Also can't access it's properties and methiods :(

nimesh 6Jul2009 15:23

Re: Logger Files in DotNet
 
Don't know where am i going wrong

naimish 6Jul2009 15:26

Re: Logger Files in DotNet
 
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.

shabbir 3Aug2009 14:34

Re: Logger Files in DotNet
 
Nominate this article for Article of the month - Jul 2009


All times are GMT +5.5. The time now is 06:25.