Log file in C#
-
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(); }
-
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(); }
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
-
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
-
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?
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
-
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(); }
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.....
-
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.....
You are still using the objects
start1
andstw1
. One (or both) are null. Your code by itself will not solve this error.*Developer Day Scotland - Free community conference Delegate Registration Open
-
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?
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
isnull
orstart1
isnull
. 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
-
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?
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
-
You are still using the objects
start1
andstw1
. One (or both) are null. Your code by itself will not solve this error.*Developer Day Scotland - Free community conference Delegate Registration Open
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.....
-
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.....
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 aNullReferenceException
. Your code will still produce aNullReferenceException
for the reasons I gave in my previous post.*Developer Day Scotland - Free community conference Delegate Registration Open