Using System;
Using System.Configuration;
Using System.IO;
Using System.Threading;
Namespace Fqdservice.utils
{
<summary>
Write Log class
</summary>
public class FileLogger
{
#region Field
public static readonly Object _lock = new Object ();
#endregion
#region Write files
<summary>
Write a file
</summary>
public static void WriteFile (string log, String path)
{
Thread thread = new Thread (new Parameterizedthreadstart (Delegate (object obj)
{
Lock (_lock)
{
if (! File.exists (PATH))
{
using (FileStream fs = new FileStream (path, FileMode.Create)) {}
}
using (FileStream fs = new FileStream (path, Filemode.append, FileAccess.Write))
{
using (StreamWriter SW = new StreamWriter (FS))
{
#region Log Content
String value = String. Format (@ "{0}
--------------------------------------------------------
{1}
", DateTime.Now.ToString (" Yyyy-mm-dd HH:mm:ss "), obj. ToString ());
#endregion
Sw. WriteLine (value);
Sw. Flush ();
}
}
}
}));
Thread. Start (log);
}
#endregion
#region Write a log
<summary>
Write log
</summary>
public static void Writelog (string log)
{
String logPath = configurationmanager.appsettings["LogPath"] + "\\FQDService_Log.txt";
WriteFile (log, LogPath);
}
#endregion
#region Write error log
<summary>
Write error log
</summary>
public static void Writeerrorlog (string log)
{
String logPath = configurationmanager.appsettings["LogPath"] + "\\FQDService_ErrorLog.txt";
WriteFile (log, LogPath);
}
#endregion
}
}
C # Write Text Log helper class (Multithreading supported)