How to write a Log File in C# ?

 private static void WriteLog(string message)
        {
            try
            {
                var date = DateTime.Now;
                var day = date.Day.ToString();
                var month = date.Month.ToString();
                var year = date.Year.ToString();
                var fileName = day +"_"+ month +"_"+ year;
                string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LogFile_Log");
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }

                var streamWriter = new StreamWriter(Path.Combine(fullPath,fileName + ".txt"), true);


                streamWriter.WriteLine(message.Trim());
                streamWriter.Flush();
                streamWriter.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }




Post a Comment

0 Comments