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 in C#

Log file in C#

Scheduled Pinned Locked Moved C#
csharp
10 Posts 4 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 have listed and then started all the exe files.As and when the exe starts a log file should be maintained which will contain the start time of the exe.So i used the following code.It gives" NullException Object reference not set to an instance of an object" for the following line

    stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());

    if (cb.Checked)
    {
    n1 = lb4.Text;
    // System.Diagnostics.Process.Start(n1);
    si1 = new ProcessStartInfo(n1);
    proc = Process.Start(si1);
    proclist.Add(proc);

                DateTime start1 = proc.StartTime;
                if (!File.Exists("startlog1.txt"))
                {
                    stw1 = new StreamWriter("startlog1.txt");
                }
                else
                {
                    File.AppendText("startlog1.txt");
                }
                stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());
                stw1.WriteLine("=======================================================================");
                stw1.Flush();
            }
    
            if (cb1.Checked)
            {
                n2 = lb5.Text;
                //System.Diagnostics.Process.Start(n2);
                ProcessStartInfo si2 = new ProcessStartInfo(n2);
                proc = Process.Start(si2);
                proclist.Add(proc);
    
                DateTime start2 = proc.StartTime;
                if (!File.Exists("startlog2.txt"))
                {
                    stw2 = new StreamWriter("startlog2.txt");
                }
                else
                {
                    File.AppendText("startlog2.txt");
                }
               
                stw2.WriteLine("UdpServer at port 10001 has started at:", start2.ToString());
                stw2.WriteLine("=======================================================================");
                stw2.Flush();
    
            }
    
    L K 2 Replies Last reply
    0
    • M mrithula8

      Hi I have listed and then started all the exe files.As and when the exe starts a log file should be maintained which will contain the start time of the exe.So i used the following code.It gives" NullException Object reference not set to an instance of an object" for the following line

      stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());

      if (cb.Checked)
      {
      n1 = lb4.Text;
      // System.Diagnostics.Process.Start(n1);
      si1 = new ProcessStartInfo(n1);
      proc = Process.Start(si1);
      proclist.Add(proc);

                  DateTime start1 = proc.StartTime;
                  if (!File.Exists("startlog1.txt"))
                  {
                      stw1 = new StreamWriter("startlog1.txt");
                  }
                  else
                  {
                      File.AppendText("startlog1.txt");
                  }
                  stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());
                  stw1.WriteLine("=======================================================================");
                  stw1.Flush();
              }
      
              if (cb1.Checked)
              {
                  n2 = lb5.Text;
                  //System.Diagnostics.Process.Start(n2);
                  ProcessStartInfo si2 = new ProcessStartInfo(n2);
                  proc = Process.Start(si2);
                  proclist.Add(proc);
      
                  DateTime start2 = proc.StartTime;
                  if (!File.Exists("startlog2.txt"))
                  {
                      stw2 = new StreamWriter("startlog2.txt");
                  }
                  else
                  {
                      File.AppendText("startlog2.txt");
                  }
                 
                  stw2.WriteLine("UdpServer at port 10001 has started at:", start2.ToString());
                  stw2.WriteLine("=======================================================================");
                  stw2.Flush();
      
              }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, how many ways are there for stw1.WriteLine("UdpServer...", start1.ToString()); to cause a NullReferenceException? has each of the relevant objects been assigned a value for all possible ways to reach this statement? :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, how many ways are there for stw1.WriteLine("UdpServer...", start1.ToString()); to cause a NullReferenceException? has each of the relevant objects been assigned a value for all possible ways to reach this statement? :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


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

        Hi Im not able to get you.Can you please explain me how i can write the start time of the corresponding servers to a text file?

        L C 3 Replies Last reply
        0
        • M mrithula8

          Hi Im not able to get you.Can you please explain me how i can write the start time of the corresponding servers to a text file?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, you have the code to do that, however it contains a minor mistake. I gave you the logic steps to pinpoint the mistake. if this is your code, you will have no trouble fixing it. if it is not, you probably won't understand the code, hence be unable to fix it. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          1 Reply Last reply
          0
          • M mrithula8

            Hi I have listed and then started all the exe files.As and when the exe starts a log file should be maintained which will contain the start time of the exe.So i used the following code.It gives" NullException Object reference not set to an instance of an object" for the following line

            stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());

            if (cb.Checked)
            {
            n1 = lb4.Text;
            // System.Diagnostics.Process.Start(n1);
            si1 = new ProcessStartInfo(n1);
            proc = Process.Start(si1);
            proclist.Add(proc);

                        DateTime start1 = proc.StartTime;
                        if (!File.Exists("startlog1.txt"))
                        {
                            stw1 = new StreamWriter("startlog1.txt");
                        }
                        else
                        {
                            File.AppendText("startlog1.txt");
                        }
                        stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());
                        stw1.WriteLine("=======================================================================");
                        stw1.Flush();
                    }
            
                    if (cb1.Checked)
                    {
                        n2 = lb5.Text;
                        //System.Diagnostics.Process.Start(n2);
                        ProcessStartInfo si2 = new ProcessStartInfo(n2);
                        proc = Process.Start(si2);
                        proclist.Add(proc);
            
                        DateTime start2 = proc.StartTime;
                        if (!File.Exists("startlog2.txt"))
                        {
                            stw2 = new StreamWriter("startlog2.txt");
                        }
                        else
                        {
                            File.AppendText("startlog2.txt");
                        }
                       
                        stw2.WriteLine("UdpServer at port 10001 has started at:", start2.ToString());
                        stw2.WriteLine("=======================================================================");
                        stw2.Flush();
            
                    }
            
            K Offline
            K Offline
            King Julien
            wrote on last edited by
            #5

            Hi... The best overloaded method for WriteLine() with two arguments are WriteLine(string,object) and WriteLine(string,object[]) But your method call supplies both the arguments as string... soo doo something like this...

            string logString = "Started time: " + start1.ToString();
            stw1.WriteLine(logString);

            This may help i believe...

            Have a Happy Coding.....

            C 1 Reply Last reply
            0
            • K King Julien

              Hi... The best overloaded method for WriteLine() with two arguments are WriteLine(string,object) and WriteLine(string,object[]) But your method call supplies both the arguments as string... soo doo something like this...

              string logString = "Started time: " + start1.ToString();
              stw1.WriteLine(logString);

              This may help i believe...

              Have a Happy Coding.....

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              You are still using the objects start1 and stw1. One (or both) are null. Your code by itself will not solve this error.

              *Developer Day Scotland - Free community conference Delegate Registration Open

              K 1 Reply Last reply
              0
              • M mrithula8

                Hi Im not able to get you.Can you please explain me how i can write the start time of the corresponding servers to a text file?

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                mrithula8 wrote:

                Can you please explain me how i can write the start time of the corresponding servers to a text file?

                He gave you the steps to work it out by yourself. In more detail he means that your code has two possible ways for a NullReferenceException to be raised.

                stw1.WriteLine("UdpServer at port 10000 has started at:", start1.ToString());

                Either stw1 is null or start1 is null. You must find out why they are null and ensure they have a value or not run that line of code. Once you have solved that you will find that the output is not what you expect. The value of start1 is not output as you have not included a placeholder for it in the text. The text string should probably read something like this:

                "UdpServer at port 10000 has started at: {0}"

                Note the addition of the {0}

                *Developer Day Scotland - Free community conference Delegate Registration Open

                1 Reply Last reply
                0
                • M mrithula8

                  Hi Im not able to get you.Can you please explain me how i can write the start time of the corresponding servers to a text file?

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  Here is a hint:

                  if (!File.Exists("startlog1.txt"))
                  {
                  stw1 = new StreamWriter("startlog1.txt");
                  }
                  else
                  {
                  File.AppendText("startlog1.txt");
                  }

                  What are you doing when the file exists vs. when the file does not exist? What is the value of stw1 after this if statement in the two scenarios.

                  *Developer Day Scotland - Free community conference Delegate Registration Open

                  1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    You are still using the objects start1 and stw1. One (or both) are null. Your code by itself will not solve this error.

                    *Developer Day Scotland - Free community conference Delegate Registration Open

                    K Offline
                    K Offline
                    King Julien
                    wrote on last edited by
                    #9

                    Hi Colin..... I dont get you... i just used the string value logString to be passed to the stream writer..... Why do you say it will not solve the problem??? suggest...

                    Have a Happy Coding.....

                    C 1 Reply Last reply
                    0
                    • K King Julien

                      Hi Colin..... I dont get you... i just used the string value logString to be passed to the stream writer..... Why do you say it will not solve the problem??? suggest...

                      Have a Happy Coding.....

                      C Offline
                      C Offline
                      Colin Angus Mackay
                      wrote on last edited by
                      #10

                      King Julien wrote:

                      i just used the string value logString to be passed to the stream writer.....

                      True, but all you did was move the original posters problem onto a different line of code. You moved his problem on to the line of code that generates the content for logString. The OP's problem was a NullReferenceException. Your code will still produce a NullReferenceException for the reasons I gave in my previous post.

                      *Developer Day Scotland - Free community conference Delegate Registration Open

                      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