Monday, March 8, 2010

C# - Example to do logging in Web Service

Below is an example to write log file in Web Service:

1) Call the following function is a Web Service

strLog = "UserName=[" + strUser + "]. PW=[" + strUserPW + "]";
LogToFile(this.Server.MapPath(".\\"), strLog);

2) Provide function of LogToFile as below:

private static void LogToFile(string strURL,
string strMess)
{
DateTime Now = System.DateTime.Now;
string strNow = Now.ToString("yyyyMMdd");

string REQUEST_LOG = strURL + "\\TextFile_" + strNow + ".txt";
StreamWriter sr = File.Exists(REQUEST_LOG) ? File.AppendText
(REQUEST_LOG) : File.CreateText(REQUEST_LOG);
sr.WriteLine(strMess);
sr.Close();
}

No comments: