Best way to create a log file
Hello,
I am developing an application for a client. They have requested some logging information.
I am thinking about be able to log debug messages, information, and serious errors.
Is there a way I could automatically switch the ones I want to log? For example, if I just wanted to write information to the log file, or write debug and information, or just serious errors.
What is the best practice when creating and writing to log files?
Many thanks for any advice,
Re: Best way to create a log file
hey steve_rm
use following class for loggin purpose
just make a private or global object of cLogger and set as many information you want
:-)
vb Code:
class cLogger
{
private string m_LogFileName;
private string m_LogPath;
private bool m_Logging;
private string m_Area;
private string m_ClassName;
private string m_FunctionName;
private StreamWriter oStreamWriter;
public string LogFileName
{
get
{
return m_LogFileName;
}
set
{
m_LogFileName = value;
}
}
public string LogPath
{
get
{
return m_LogPath;
}
set
{
m_LogPath = value;
if (m_LogPath[m_LogPath.Length - 1].ToString() != @"\") m_LogPath = m_LogPath + @"\";
}
}
public bool Logging
{
get
{
return m_Logging;
}
set
{
m_Logging = value;
}
}
public string Area
{
get
{
return m_Area;
}
set
{
m_Area = value;
}
}
public string ClassName
{
get
{
return m_ClassName;
}
set
{
m_ClassName = value;
}
}
public string FunctionName
{
get
{
return m_FunctionName;
}
set
{
m_FunctionName = value;
}
}
public cLogger()
{
try
{
m_Logging = bGlobalLogging;
m_LogPath = sGlobalLogPath;
m_LogFileName = sGlobalLogFileName;
}
catch // (Exception oEx)
{
// throw;
}
}
public bool WriteLog(string sFunctionName, string sMessage)
{
bool bResult = true;
try
{
if (m_Logging == true)
{
m_FunctionName = sFunctionName;
return WriteLog(sMessage);
}
}
catch //(Exception oEx)
{
bResult = false;
//throw;
}
return bResult;
}
public bool WriteLog(string sMessage)
{
bool bResult = true;
try
{
if (m_Logging == true)
{
string sLogFilePathName = m_LogPath + m_LogFileName;
string sContents = string.Empty;
sContents = DateTime.Now + "|" + m_Area + "|" + m_ClassName + "|" + m_FunctionName + "|" + sMessage + Environment.NewLine;
oStreamWriter = File.AppendText(sLogFilePathName);
oStreamWriter.WriteLine(sContents);
oStreamWriter.Close();
}
}
catch //(Exception oEx)
{
bResult = false;
//throw;
}
return bResult;
}
Re: Best way to create a log file
Maybe you could keep some flags for that in the database or at application level and in the logger function check for the flags and accordingly log the required events.