code_junkie

Best way to read in a rolling log file in .NET?

Nov 13th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. Object mLogLock = new Object(); //to make logging thread safe
  2. string mLogFile = ""; //set your log location
  3. string mLogDirectory = "";
  4. public void HandleMessage(string inMessage)
  5. {
  6.  
  7. lock (mLogLock)
  8. {
  9. if (!System.IO.Directory.Exists(mLogDirectory ))
  10. {
  11. System.IO.Directory.CreateDirectory(mLogDirectory );
  12. }
  13.  
  14. String theMessage = DateTime.Now.ToString("s");
  15. if (inMessage != null)
  16. theMessage += " : " + inMessage;
  17.  
  18. StreamWriter sw = new StreamWriter(mLogFile, true);
  19.  
  20. sw.WriteLine(theMessage );
  21.  
  22. sw.Dispose();
  23. sw.Close();
  24. }
  25. }
Add Comment
Please, Sign In to add comment