On Event--
string thisDir = Server.MapPath("./ExLogFile");
//String Format : DateTime,Action,Userid,UserRole,Status,Attempt, FileName,empid,empname, username, adminactivity
string logMessage = DateTime.Now.ToUniversalTime() + "," + "Logout," + Session["UName"] + "," + Session["user_RoleName"] + "," + "logged out" + "," + "," + "," + "," + "," + "," + "," ;
writeToLogFile(logMessage, thisDir);
Method-
public static void writeToLogFile(string logMessage, string thisDir)
{
StreamWriter swLog=null;
try
{
string strLogMessage = string.Empty;
string strLogFile = "" + thisDir + "\\logFile.txt";
//strLogMessage = string.Format("{0}: {1}", DateTime.Now, logMessage);
strLogMessage = string.Format(logMessage);
if (Directory.Exists(thisDir)) { }
else { System.IO.Directory.CreateDirectory(thisDir); }
if (!File.Exists(strLogFile))
{
swLog = new StreamWriter(strLogFile);
}
else
{
swLog = File.AppendText(strLogFile);
}
swLog.WriteLine(strLogMessage);
swLog.WriteLine();
swLog.Close();
swLog.Dispose();
}
catch (Exception)
{
swLog.Dispose();
throw;
}
finally { swLog.Dispose(); }
}
No comments:
Post a Comment