Saturday 19 October 2013

Create Log file in asp.net


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

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...