C # Operations Log

Source: Internet
Author: User

First, reference nlog DLL file

Using System.IO;

Using NLog;

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------

Base Log Class

<summary>
Write log
</summary>
public class Writelog
{
private static Logger Logger = Logmanager.getcurrentclasslogger ();
private static string Filebasepath = AppDomain.CurrentDomain.BaseDirectory + "logs\\";

<summary>
Write log
</summary>
<param name= "Log" > Log contents </param>
public void Writecomlog (string log)
{
Log warning messages
Logger. Warn (log);
}

<summary>
Write logs by name
</summary>
<param name= "Log" > Log contents </param>
<param name= "LOGNAME" > Journal category name </param>
public void Writecomlog (string log, String logname)
{
Logger loggerbyname = Logmanager.getlogger (logname);
Record General Information
Loggerbyname.info (log);
}

<summary>
Writing information to a file
</summary>
<param name= "Info" > Info </param>
<param name= "filename" > file name </param>
<param name= "filefix" > File suffix name </param>
public void WriteFile (string info, string filename, string filefix)
{
Try
{
Folder path
String _filebasepath = Gettodyrecordpath ();
String FilePath = String. Empty;

The

if (string. IsNullOrEmpty (Filefix))
{
//log file path
FilePath = _filebasepath + "\ \" + filename + ". Log";
}
Else
{
FilePath = _filebasepath + "\ \" + filename + filefix;
}
//If the file folder does not exist, create the file folder
if (! System.IO.Directory.Exists (_filebasepath))
{
Directory.CreateDirectory (_filebasepath);
}
//If the file does not exist, create the file
if (! File.exists (FilePath))
{
File.create (filePath). Close ();
}
StreamWriter sw = File.appendtext (filePath);
SW. Write (info);
SW. Close ();
}
Catch
{

}
}

<summary>
Writing information to a file
</summary>
<param name= "Info" > Info </param>
<param name= "filename" > file name </param>
<param name= "filefix" > File suffix name </param>
<param name= "Childpath" ></param>
public void WriteFile (string info, string filename, string filefix,string childpath)
{
Try
{
String _filebasepath = Gettodyrecordpath () +childpath;
String FilePath = String. Empty;

if (string. IsNullOrEmpty (Filefix))
{
FilePath = _filebasepath + "\ \" + filename + ". Log";
}
Else
{
FilePath = _filebasepath + "\ \" + filename + filefix;
}

if (! System.IO.Directory.Exists (_filebasepath))
{
Directory.CreateDirectory (_filebasepath);
}
If the file does not exist, create the file
if (! File.exists (FilePath))
{
File.create (FilePath). Close ();
}
Append log files to the log folder
StreamWriter SW = File.appendtext (FilePath);
Writes the contents of the log to the log file
Sw. Write (info);
Sw. Close ();
}
Catch
{

}
}

<summary>
Get the current date folder
</summary>
<returns>createpath Folder path </returns>
private String Gettodyrecordpath ()
{
String Createpath = String. Empty;
Create a file folder if it does not exist
if (directory.exists (Filebasepath + DateTime.Now.Year.ToString ()) = = False)
{
Create a folder
Directory.CreateDirectory (Filebasepath + DateTime.Now.Year.ToString ());
Get folder path
Createpath = DateTime.Now.Year.ToString () + @ "\";
}

If (directory.exists (Filebasepath + DateTime.Now.Year.ToString () + "/" +datetime.now.month.tostring ()) = = False)
{
Directory.CreateDirectory (Filebasepath + DateTime.Now.Year.ToString () + "/" + DateTime.Now.Month.ToString () );
Createpath = DateTime.Now.Year.ToString () + @ "\" +datetime.now.month.tostring () [email protected] "\";
}

if (directory.exists (Filebasepath + DateTime.Now.Year.ToString () + "/" + DateTime.Now.Month.ToString () + "/" + DateTime.Now.ToString ("YyyyMMdd")) = = False)
{
Directory.CreateDirectory (Filebasepath + DateTime.Now.Year.ToString () + "/" + DateTime.Now.Month.ToString () + "/" + DateTime.Now.ToString ("DD"));
Createpath = DateTime.Now.Year.ToString () + @ "\" +datetime.now.month.tostring () [email protected] "\" + DateTime.Now.ToString ("dd") + "/";
}
Createpath = Filebasepath + DateTime.Now.Year.ToString () + @ "\" + DateTime.Now.Month.ToString () + @ "\" + DateTime.Now.ToSt Ring ("DD") + @ "\";
return createpath;
}

}

--------------------------------------------------------------------------------------------------------------- -----------------------------------

C # Operations Log

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.