public void WriteLog(string TextStr)
{
string FilePath = ConfigurationManager.AppSettings["LogPath"].ToString();
//Set Filename By Date
string FileName = DateTime.Now.ToString("MM-dd-yyyy") + ".txt";
string FullName = FilePath + FileName;
//if file does not exist, create one
if (!File.Exists(FullName))
{
FileStream fileStream = new FileStream(@FullName, FileMode.Append);
}
StreamWriter SW;
SW = File.AppendText(FullName);
SW.WriteLine(TextStr);
SW.Close();
}
ASKER
ASKER
ASKER
C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).
TRUSTED BY
You need to call your file using a lock or a mutex:
use lock if your program is multithreaded.
public void WriteLog(string TextStr)
{
string FilePath = ConfigurationManager.AppSe
//Set Filename By Date
string FileName = DateTime.Now.ToString("MM-
string FullName = FilePath + FileName;
//check if file exists, else create one
lock(this)
{
FileStream fileStream = new FileStream(@FullName, FileMode.Append);
StreamWriter SW;
SW = File.AppendText(FullName);
SW.WriteLine(TextStr);
SW.Close();
}
}
else use mutex
http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx
Alternatives: use Trace class and specify your file, log framework (log4net)
http://stackoverflow.com/questions/1260157/whats-the-most-widely-used-logging-framework-in-c