views:

154

answers:

2

I am creating a common class called writelog.cs This is to store the common method to call in all my programs (aspx page) After which, whatever error is produced, it will come out to an error log.txt

This is my codes for writelog.cs And i got this error: The name 'Global' does not exist in the current context

/// <summary>
/// Summary description for Writelog
/// </summary>
/// <param name="Desc">Desc</param>
/// <param name="ID">ID</param>
/// <param name="Pg">Program</param>
/// <param name="Msg">System Error Message</param>
public class Writelog
{
    public static void WritelogDesc(string Desc, string ID, string Pg, string Msg)
    {
        StringBuilder SB = new StringBuilder();

        string path = Global.getLogFilePath();

        SB.Append(DateTime.Now.ToString("dd/MM/yyyy") + " " + DateTime.Now.ToShortTimeString());
        SB.Append(" | ");
        SB.Append(Desc);
        SB.Append(" | ");
        SB.Append(ID);
        SB.Append(" | ");
        SB.Append(Pg);
        SB.Append(" | ");
        SB.Append(Msg);

        if (!File.Exists(path))
        {
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(SB.ToString());
            }
        }

        using (StreamWriter sw = File.AppendText(path))
        {
            Writelog.WritelogDesc();
            sw.WriteLine(SB.ToString());
        }

    }
}

And here is how i call writelog.cs

Writelog.WritelogDesc();
+1  A: 

Is there any reason why you are not using a loggin framework like log4net? These frameworks provide you with so much more functionality so you can just abuse it and get on with the work you want to do.

If there is a reason why you want to roll your own logging, are you sure that you have addded a reference to the assembly that contains the Global namespace?

Penfold
I agree, log4net, NLog, Enterprise Library, ELAMH are all fantastic logging frameworks and there shouldn't be any need to roll in your own.
Kane
umm. cuz I am doing my Final Year Project and this is the request from the teacher in charge.
Nana
Fair enough. Did you check that the assembly with the Global namespace has been referenced?
Penfold
A: 
StreamWriter sw = (!File.Exists(path))? File.CreateText(path):File.AppendText(path);  
sw.WriteLine(SB.ToString());

Call this function where you want using just the classname and functionname with parameters

Writelog.WritelogDesc(Desc, ID, Pg, Msg);
Sauron