C # Print Log

Principle is very simple, is to create a folder, create a file, write to

First, determine the folders, if the file exists

Then create or append

Little introduction, directly on the code

public  static  void BuildLogFile ( String param) 
{ 
String sFilePath = " E: \\ the ErrorLog " ;
 String sFileName = DateTime.Now.ToString ( " yyyyMMdd " ) + " .log " ;
 // absolute path of the file 
sFileName = Path.Combine (sFilePath, sFileName);
 // verify that the path exists, does not exist, create 
iF (! Directory.Exists (sFilePath)) 
{ 
Directory.CreateDirectory (sFilePath); 
} 
the FileStream FS; 
the StreamWriter SW; 
iF(The File.Exists (sFileName))
 // Verify that the file exists, there is an additional, non create 
{ 
FS = new new the FileStream (sFileName, FileMode.APPEND, FileAccess.Write); 
} 
the else 
{ 
FS = new new the FileStream (sFileName, FileMode .create, FileAccess.Write); 
} 
SW = new new the StreamWriter (FS);
 // log contents 
sw.WriteLine (DateTime.Now.ToString ( " YYYY the mM-dd-HH: mm: SS " ) + " ---- > " + param); 
sw.Close (); 
fs.Close (); 
}

 

Guess you like

Origin www.cnblogs.com/ocean-wang/p/10943074.html