Using system;using system.collections.generic;using system.linq;using system.web;using system.web.mvc;//Add Reference, and import namespaces using system.management;using system.net.networkinformation;using system.io;//log output class public void Swriter (string ipname) { String sfilepath = "D:webtest/logfol der/";//Specify Folder path String sfilename =" Logoutput "+datetime.now.tostring (" dd ") +". Log ";//Specify file sFileName = Sfilepath +"/"+ sfilename;//file under specified path &NBSP ; //To determine if there is a folder, if not present then create if (! Directory.Exists (Sfilepath) { &N Bsp Directory.CreateDirectory (Sfilepath); } FileStream fs;//declaration file flow StreamwritER sw;//declaration write stream FileInfo fi=new FileInfo (sfilename);//initialization file Operation class ///Determine if there is a file and if it does not exist then create if (!fi. Exists) { FS = new FILESTR EAM (sFileName, FileMode.Create, FileAccess.Write); } & nbsp Else { FS = new FileStream (SFILENAME,FILEMODE.APPEND,FILEACC Ess. Write); } FS. Close ();//The stream must be called at the end of the last call. Flush (); with. Close (), method, or diagram is easy to use using to process, you can also use try-catch using (sw = new StreamWriter (sfilename,true)) { Console.setout (SW);//Open the file to be written, no this will not be written Console.WriteLine ("" "+ Ipname +" "+" IP user "+ DateTime.Now +" access to the homepage!!! ");//write content } //streamwriter SW = new Stre Amwriter (@ "D:\WebTest\LogOutput.txt", true); //console.setout (SW); //console.writeline ("" "+ Ipname +" "+" IP user "+ DateTime.Now +" access to the homepage!!! "); //SW. Flush (); //SW. Close (); }
Log output--c#