Code to Write Log files in C#

dotnetlogo
Following method can be used to write errors into a log file through an application.

using System.IO;
public void WriteLogLine(string sCallerName, string sLogFolder,
long lCallerInstance, string sLogLine)
{
lock(this)
{
string sFileName;
sFileName = String.Format("{0}_{1:yyyy.MM.dd}_{2:00}.log",
sCallerName, DateTime.Now, lCallerInstance);
StreamWriter swServerLog =
new StreamWriter(sLogFolder + sFileName, true);
swServerLog.WriteLine(
String.Format("[{0:T}] {1}", DateTime.Now, sLogLine));
swServerLog.Close();
}
}

Anky Goyal

I am MCPD certified in .NET and working on .NET technologies for past 3yrs. I am very passionate about the new features which get introduced in the technology. I am here to share the new features i get to know in the world of Microsoft. Follow me on twitter @ankygoyal [email protected]Ankit Goyal

More Posts - Website