title: Csvhelper write with windows service
body: <p>I am using csvhelper with windows service which runs non stop. I want to create a  new csv log file and put the header values only once until the file size grown to 1000 kb and then create another new file so the file size grow too big. At the moment csv file is created once when windows service starts but it repeats header values every time it writes into it.</p>
  
  <p>Do we have some config properties in csv helper to flag size file?
  Please have a look at the code and suggest if it can be done efficiently in .net 4.0.
  Windows Service code</p>
  
  <pre><code>public partial class Service1 : ServiceBase
  {
  
      private Thread executeThread;
  
      public Service1()
      {
          InitializeComponent();
      }
  
      protected override void OnStart(string[] args)
      {            
  
          try
          {
  
              Thread.Sleep(30000);
  
              executeThread = new Thread(new ThreadStart(Execute));
              executeThread.Start();                                             
  
          }
          catch (Exception e)
          {
              Library.LogData("Error : " + DateTime.UtcNow + " " + e.Message);
              OnStop();
          }            
      }
  
      protected override void OnStop()
      {
          try
          {
              Library.LogData("Stopped: " + DateTime.UtcNow);
              executeThread.Abort();
          }
          catch (Exception e)
          {
              MessageBox.Show(e.Message);
          }            
      }
  
      public void Execute()
      {
          try
          {
              Library.LogData("Started");
              while (true)
              {
                  Library.LogData("Logging Execute" + DateTime.UtcNow);
                  Thread.Sleep(5000);
              }                 
          }
          catch (Exception e)
          {
              Library.LogData("Error : " + DateTime.UtcNow + " " + e.Message);
              OnStop();
          }
          Library.LogData("Out of Execute" + DateTime.UtcNow);
      }
  
  }
  </code></pre>
  
  <p>Logging code called by windows service</p>
  
  <pre><code>public static class Library
  {
      public static void LogData(string Message)
      {
          StreamWriter sw = null;
          GetData getData = new GetData();
          var dataClasslist = new List&lt;DataClass&gt;
          {
              new DataClass {}
          };
          try
          {
  
              dataClasslist = getData.ReadData();
              using (sw = new StreamWriter("E:\\DataLogs\\LogFile.csv", true))
              using (var csv = new CsvWriter(sw))
              {
                  csv.Configuration.RegisterClassMap&lt;DataClassMap&gt;();
                  csv.WriteHeader&lt;DataClass&gt;();
                  csv.Configuration.TrimHeaders = true;
                  csv.Configuration.HasHeaderRecord = false;
                  csv.WriteRecords(dataClasslist);
                  sw.Flush();
                  sw.Close();
              }
          }
          catch (Exception e)
          {
              var err = new StreamWriter("E:\\DataLogs\\Errors", true);
              err.WriteLine(DateTime.Now.ToString() + ": " + e.Message.ToString());
              err.Flush();
              err.Close();
          }
  
      }
  
  }
  </code></pre>