Search This Blog

Monday, September 4, 2017

Create a Log File Write in Text File in C#

1- Method:
 
private static string NGOresourcePath = @"D:\NGO_Logs\";
 
public const string LogFile = "NGO.log";
 
Log(LogFile, "NGO Udated  by : ", SPContext.Current.Web.CurrentUser.LoginName.ToString());
                       
 public void Log(string fileName, string methodname, string msg)
        {
            try
            {
                TextWriter writer = File.AppendText(NGOresourcePath + fileName);
                writer.WriteLine(DateTime.Now.ToString("yyyyMMdd-HH:mm:ss") + ": " + methodname + " -- " + msg);
                writer.WriteLine("---------------------------------------------------------------------------------");
                writer.Close();
            }
            catch
            {
            }
        }
 
 
2- Method:
 
 
       string ErrorLibraryName = string.Empty;
        public void WriteLog(string siteurl, string strLog)
       {
           GetErrorLibraryName objGetLibraryName = new GetErrorLibraryName();
            ErrorLibraryName = objGetLibraryName.GetLibraryNameMethod();
        
            using (SPSite oSPsite = new SPSite(siteurl))
           {
               using (SPWeb oSPWeb = oSPsite.OpenWeb())
               {
                   oSPWeb.AllowUnsafeUpdates = true;
                   // Fetch the List
                   SPList list = oSPWeb.Lists[ErrorLibraryName];
                   //Add a new item in the List
                   SPListItem itemToAdd = list.Items.Add();
                   itemToAdd["Title"] = "ErrorLog _" + System.DateTime.Today.ToString("dd-MM-yyyy");
                   itemToAdd["ErrorDescription"] = strLog;
                   itemToAdd.Update();
                   oSPWeb.AllowUnsafeUpdates = false;
               }
           }
 
 
           // Code is Commented on  1 Aug 2016
           //System.IO.StreamWriter log;
           //FileStream fileStream = null;
           //DirectoryInfo logDirInfo = null;
           //FileInfo logFileInfo;
           ////string Folderpath = WebConfigurationManager.AppSettings["ErrorFolderpath"].ToString();
           //string logFilePath = "C:\\Logs\\";
           //// string logFilePath = Folderpath;
           //logFilePath = logFilePath + "NGO" + "_" + System.DateTime.Today.ToString("dd-MM-yyyy") + "." + "txt";
           //logFileInfo = new FileInfo(logFilePath);
           //logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
           //if (!logDirInfo.Exists) logDirInfo.Create();
           //if (!logFileInfo.Exists)
           //{
           //    fileStream = logFileInfo.Create();
           //}
           //else
           //{
           //    fileStream = new FileStream(logFilePath, FileMode.Append);
           //}
           //log = new StreamWriter(fileStream);
           //log.WriteLine("*******************************" + DateTime.Now + "********************************");
           //log.WriteLine(strLog);
 
           //log.Close();
       }
    }
}

No comments:

Post a Comment