When we do the project, we often use the log log, today to share how to add log information in the background
Create a method to write log:
1 Public voidWritelog (stringAction)2 {3 Try4 {5 stringStrlogpath = system.configuration.configurationmanager.appsettings["LogPath"]. ToString ();6 stringstrLogName = System.DateTime.Now.ToString ("YYYY/MM/DD"). Replace ("/","-");7 stringStrIP = request.userhostaddress +":"+ Request.Url.Port;//IP and port of the caller8 if(!strlogpath.endswith ("\\"))9Strlogpath + ="\\";TenStrlogpath + ="log\\"; One //determine if there is such a path and create A if(System.IO.Directory.Exists (strlogpath) = =false) - { - System.IO.Directory.CreateDirectory (strlogpath); the } -strLogName = Strlogpath + strLogName +". txt"; - ////If the file does not exist, it will be created automatically - stringStrnote = System.DateTime.Now.ToString ("YYYY/MM/DD HH:mm:ss"); +Strnote + =":"+ StrIP +": \ n"+ Action +"\ n"; -System.IO.StreamWriter file =NewSystem.IO.StreamWriter (strLogName,true); + file. WriteLine (strnote); A file. Close (); at file. Dispose (); - } - Catch //(Exception ex) - { - - } in}
Invoke Event:
1 protected voidIbtnquery_click (Objectsender, EventArgs e)2 {3 Try4 { 5 Query ();6 }7 Catch(Exception ex)8 { 9 Writelog (ex. Message); Ten } One}
Effect Show:
C # Background Add log information