Hi,
Can someone help me on this.
I need to make here a log when request is start and when is end. I want to see how much time is taken for request/response.

WebResponse response = webRequest.GetResponse();
                                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                                // Get the stream containing content returned by the server.
                                Stream dataStreamResponse = response.GetResponseStream();
                                // Open the stream using a StreamReader for easy access.
                                StreamReader reader = new StreamReader(dataStreamResponse);
                                // Read the content.
                                string responseFromServer = reader.ReadToEnd();
                                // Display the content.
                                Console.WriteLine(responseFromServer);  //<----
                                // Clean up the streams.
                                reader.Close();
                                dataStreamResponse.Close();
                                response.Close();

Do you want to log the time or do u watn to find the time difference between your start and end, and log the difference into your log.
to get the time difference,
Datetime dt = Datetime.Now;
//Call your function which does the start of the logging.
TimeSpan ts = Datetime.Now.Substract(dt);
ts will give the total time elapsed in Datetime format.

I just want to know when is start and when is end with request.
In log must write something like:
req start: 10:15:32 am
req end: 10:15:36 am
...

Edited 4 Years Ago by JasonWung

now i use this and is work fine .. write every request in this log file.. can someone help and modify to write when request start and when end ?

               WebResponse response = webRequest.GetResponse();
                Stream dataStreamResponse = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStreamResponse);
                string responseFromServer = reader.ReadToEnd();

                Console.WriteLine(responseFromServer);  //<----
                reader.Close();
                dataStreamResponse.Close();
                response.Close();


                StreamWriter log;

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

                log.WriteLine(DateTime.Now);
                log.WriteLine(responseFromServer);
                log.WriteLine();       
                log.Close();

Hi, check this out, I suppose this what u wanted.
strMessage is the message that has to be written to the file.
put this in a method and call that method whenever you feel there needs to be a log entry.

if (!File.Exists(strLog))
                        {
                            FileStream fs = new FileStream(strLog, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                            StreamWriter swWriteServiceLog = new StreamWriter(fs);
                            swWriteServiceLog.WriteLine(DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString() + ": " + strMessage);
                            swWriteServiceLog.Flush();
                            swWriteServiceLog.Close();
                        }
                        else
                        {
                            StreamWriter swWriteServiceLog = new StreamWriter(strLog, true);
                            swWriteServiceLog.WriteLine(DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString() + ": " + strMessage);
                            swWriteServiceLog.Flush();
                            swWriteServiceLog.Close();
                        }
This article has been dead for over six months. Start a new discussion instead.