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