Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Log file

Log file

Scheduled Pinned Locked Moved C#
sysadminhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrithula8
    wrote on last edited by
    #1

    Hi I want to write a message to the text file as soon as the server starts.I have written the following code which creates a new file but it does not write anything into it.I tested this code by starting only one server.It does not work.Please help me find where i went wrong.proclist contains the list of all servers.

    foreach (Process pr in proclist)
    {
    if (!File.Exists("logfile.txt"))
    {
    stw = new StreamWriter("logfile.txt");
    }
    else
    {
    stw = File.AppendText("logfile.txt");

                }
                DateTime started = pr.StartTime;
                stw.WriteLine("Server information:");
                stw.WriteLine(started.ToString());
    
            }
    
    A 1 Reply Last reply
    0
    • M mrithula8

      Hi I want to write a message to the text file as soon as the server starts.I have written the following code which creates a new file but it does not write anything into it.I tested this code by starting only one server.It does not work.Please help me find where i went wrong.proclist contains the list of all servers.

      foreach (Process pr in proclist)
      {
      if (!File.Exists("logfile.txt"))
      {
      stw = new StreamWriter("logfile.txt");
      }
      else
      {
      stw = File.AppendText("logfile.txt");

                  }
                  DateTime started = pr.StartTime;
                  stw.WriteLine("Server information:");
                  stw.WriteLine(started.ToString());
      
              }
      
      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      Hi, stw.Close(); ?? or possibly stw.Flush(); stw.Close(); Although it might be simpler to forget the StreamWriter and use File.AppendAllText("logfile.txt", String.Format("Server Info:\r\n {0}\r\n", started)); Alan.

      V M 2 Replies Last reply
      0
      • A Alan N

        Hi, stw.Close(); ?? or possibly stw.Flush(); stw.Close(); Although it might be simpler to forget the StreamWriter and use File.AppendAllText("logfile.txt", String.Format("Server Info:\r\n {0}\r\n", started)); Alan.

        V Offline
        V Offline
        Vitaliy Tsvayer
        wrote on last edited by
        #3

        Yes it seems like Flush() is missing. But for logging you should better use log4net[^]. It is very easy to configure and start writing log messages.

        Vitaliy Tsvayer Tikle

        1 Reply Last reply
        0
        • A Alan N

          Hi, stw.Close(); ?? or possibly stw.Flush(); stw.Close(); Although it might be simpler to forget the StreamWriter and use File.AppendAllText("logfile.txt", String.Format("Server Info:\r\n {0}\r\n", started)); Alan.

          M Offline
          M Offline
          mrithula8
          wrote on last edited by
          #4

          hi Thanks now im getting it right

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups