Exemple #1
0
        private static void Log(Exception ex)
        {
            Console.WriteLine(DateTime.Now + " - " + ex.Message);

            using (
                FileStream sourceStream = File.Open(RootDir + @"\fileSyncLog.log", FileMode.OpenOrCreate))
            {
                byte[] header = new UTF8Encoding(true).GetBytes(Environment.NewLine + DateTime.Now + " ");
                byte[] info = new UTF8Encoding(true).GetBytes(ex.ToString());
                IEnumerable<byte> outBytes = header.Concat(info);

                if (sourceStream.Length < LogMaxLength)
                {
                    sourceStream.Seek(0, SeekOrigin.End);
                    var enumerable = outBytes as byte[] ?? outBytes.ToArray();
                    sourceStream.Write(enumerable, 0, enumerable.Length);
                }
                else
                {
                    sourceStream.Close();
                    var fileInfo = new FileInfo(sourceStream.Name);
                    fileInfo.Delete();
                    Log(ex);
                }
            }
        }