Wednesday, September 4, 2013

Custom Error Logging in C#

1. Cratre a ErrorLogging.txt text file in C:\TestFolder folder
2. if ErrorLogging.txt Exceed 4MB then move this file to C:\TestFolder\BackUpFiles folder and    rename as ErrorLogging_<TimeStamp>.txt.
3. as usally create a new ErrorLogging.txt text file in C:\TestFolder
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ErrorLogging
{
    class ErrorLoggingProgram
    {
        static void Main(string[] args)
        {
            string directory = @"C:\TestFolder";
            try
            {
                if (!Directory.Exists(directory))
                {
                    DirectoryInfo di = Directory.CreateDirectory(directory);
                }
            }
            catch (IOException ioex)
            {
                Console.WriteLine(ioex.Message);
            }
            string errormsg = "first line";
            if (File.Exists(@"C:\TestFolder\ErrorLogging.txt"))
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\TestFolder\ErrorLogging.txt", true))
                {
                    file.WriteLine(errormsg);
                    file.Flush();
                    file.Close();
                    FileInfo fi = new FileInfo(@"C:\TestFolder\ErrorLogging.txt");
                    var size = fi.Length;
                    if (File.ReadAllBytes(@"C:\TestFolder\ErrorLogging.txt").Length >= 4 * 1024 * 1024) // (4MB) File to big? Create new
                    {
                        Console.WriteLine("File Size in Bytes: {0} greater than 10kb", size);
                        moveFile();
                    }
                    Console.WriteLine("File Size in Bytes: {0}", size);
                }
            else
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\TestFolder\ErrorLogging.txt", true))
                {
                    file.WriteLine(errormsg);
                    Console.WriteLine("Does not Exist" + "\t" + @"C:\TestFolder\ErrorLogging.txt");
                    file.Flush();
                    file.Close();
                }
            Console.ReadKey();
        }
        private static void moveFile()
        {
            string directory = @"C:\TestFolder\BackUpFiles";
            try
            {
                if (!Directory.Exists(directory))
                {
                    DirectoryInfo di = Directory.CreateDirectory(directory);
                }
            }
            catch (IOException ioex)
            {
                Console.WriteLine(ioex.Message);
            }
            string sourceFile = @"C:\TestFolder\ErrorLogging.txt";
            string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            string destinationFile = @"C:\TestFolder\backupfiles\ErrorLogging_" + timeStamp + ".txt";
            System.IO.File.Move(sourceFile, destinationFile);
        }
    }
}

No comments:

Post a Comment

Featured Post

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

Protect an API by using OAuth 2.0 with Azure Active Directory and API Management https://docs.microsoft.com/en-us/azure/api-management/api-...

Popular posts