title: How can I create a log file, one per day, in C# webpages to log info?
body: <p>I have successful code that creates and writes to a log file during OnPostAsync.
  Every time this page is called it creates a log file with today's date and time and writes to the log file. </p>
  
  <p>I would like to create one log file per day and write to it. I can put time inside the contents instead of creating a new file named after today and time.
  I would assume I check for a log file with today's date and if it doesn't exist I write my stuff. If it exists I just append my stuff.</p>
  
  <p>Here is what I am using to create a log file for every post of the page:</p>
  
  <pre><code>var newFileName = "logs\\" + DateTime.Now.ToString().Replace("/","_").Replace(":","-").Replace(" ","__") + ".txt";
  var webRootPath = _environment.WebRootPath;
  var dataPath = Path.Combine(webRootPath, newFileName);
  </code></pre>
  
  <p>then at the end of the OnPostAsync I write whatever string of info I need to write.</p>
  
  <pre><code>var newCourseAfterSaveInfo = "New Course Saved ::: New Temp ID in Courses_New: blah blah blah ";
  
   System.IO.File.AppendAllLines(dataPath, new[] {newCourseAfterSaveInfo });
  </code></pre>
  
  <p>I've done this in VB and VBA. The correct file write function will create a file &amp; name if it doesn't exist, otherwise it will just append a line. But I am at a loss to do this with C#.</p>