Skip to content

How to Log Conversation?

vijay

Asked Jul 7, 2009, 3:00 AM EDT2 answers
Share
LinkedInX
i am a beginner..
I developed an application in which server n clients communicate with each other,
i have to log the conversation between the client and server in a text file.
How to do that? Plz help me..
 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Niradhip Chakraborty
    0

    Create a textfile inside your application say "Log.txt";

     

    System.IO.StreamWriter objWriter;//Declare this as public.

    string LOG_FILE_NAME; //Declare this as public.

     

    protected void Page_Load(object sender, EventArgs e)

    {

      LOG_FILE_NAME = Server.MapPath("Log.txt");

    }

     

    Now you will be knowing what will be the conversation between server and client. Put this in a string and call it in the following function inside the proper event handler.

     

    string strMessage ="";//assign the conversation here

    WriteLogMessage (strMessage);

     

    public void WriteLogMessage(string strMessage)

        {

            try

            {

                if (System.IO.File.Exists(LOG_FILE_NAME) == true)

                {

                    objWriter = System.IO.File.AppendText(LOG_FILE_NAME);

                }

                else

                {

                    objWriter = System.IO.File.CreateText(LOG_FILE_NAME);

                }

     

                objWriter.Write(strMessage + Environment.NewLine);

            }

     

            catch (Exception ex)

            {

            }

            finally

            {

                objWriter.Flush();

                objWriter.Close();

            }

        }

    0
  • 0
    Thanks a lot Niradeep..
    ur response s very easy to understand and so useful.
    0