C # Write Text Log helper class (Multithreading supported)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.