C Method of Log Message to Log File

  • 2021-07-10 20:37:16
  • OfStack

This article illustrates how C # logs messages to log files. Share it for your reference. The specific implementation method is as follows:


public void LogMessageToFile(string msg)
{
  System.IO.StreamWriter sw = System.IO.File.AppendText("log.txt");
  try
  {
   string logLine = System.String.Format("{0:G}: {1}.", System.DateTime.Now, msg);
   sw.WriteLine(logLine);
  }
  finally
  {
   sw.Close();
  }
}

I hope this article is helpful to everyone's C # programming.


Related articles: