Log file create using c# in MS crm

#region NameSpaces
using System.IO;
using System;
using System.Windows.Forms;
#endregion
namespace EmailAttachments
{

    //This is Log File
    class Log
    {
        static string baseDirectory = CustomException.baseDirectory;
        static string LogFile = "";


        #region Constructor
        /// <summary>
        /// Parameterized Constructor
        /// </summary>
        public Log(string logFile)
        {
            try
            {
                if (CustomException.HasPermissions(baseDirectory))
                {
                    if (!logFile.Contains(".txt"))
                    {
                        logFile = logFile + ".txt";
                    }
                    LogFile = baseDirectory + logFile;//+"_"+DateTime.Now.ToString("MMddyyyyhhmm");

                    if (!Directory.Exists(baseDirectory)) { System.IO.Directory.CreateDirectory(baseDirectory); }

                    if (!File.Exists(LogFile))
                    {

                        StreamWriter sw = File.CreateText(LogFile);
                        sw.WriteLine("Log File Created: " + DateTime.Now.ToString());
                        sw.Close();

                    }
                    else
                    {
                        File.Delete(LogFile);
                        StreamWriter sw = File.CreateText(LogFile);
                        sw.WriteLine("Log File Created: " + DateTime.Now.ToString());
                        sw.Close();
                    }
                }
               
            }
            catch (Exception ex)
            {
               // CustomException.WriteExceptionLogXML(ex);
            }
        }
        #endregion

        /// <summary>
        /// Writes the input message to the server event log
        /// </summary>
        /// <param name="message">the message</param>
        public static void Write(string text)
        {
            try
            {
                StreamWriter sw = File.AppendText(LogFile);
                sw.WriteLine(text);
                sw.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }
}

Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest