LogHandler

Date

June 2016

Category

Console Application/Library

Address

Git Repo Link

Technology Used

C#

About this project

The LogHandler is a C# application that I built which enables easy logging of an applications data in text files. The application can be added to a C# project in its entirety or compiled down to a DLL and referenced in the solution.

The application has 3 main logging methods, the Log(), LogError() and LogExceptionError() method.

The Log() method takes two string parameters, the first being a filename and the second being the content to be written down to the specified file. Currently the way the application is set up, a new directory will be created each day to make it easier to keep logs organized. Once a filename has been specified, the application checks to see if the file already exists in todays directory (today being the current date) and if not, the file and directory get created. However, if the file does exist in the directory, the file is then appended with the added new content passed to the method in the second parameter.

The LogError() method takes one string parameter being the text content to be stored. There is no need for a file name to be passed to this method as all error logs will be recorded in a text file with the current date as the file name. All logs through this method will be created in the default error log directory. As with the Log() method, if a file name with the current date does not exist in the directory, the application will create this file with the text from the string parameter as the files content. If the file does exist, the file will simply get appended with this new content.

The final method is the LogExceptionError() method. This method is overloaded and can take either string content or an exception that has been caught as a parameter. Once an exception has been caught, it is then passed to the method which then logs the inner message of the exception. Unlike the other two methods, all content logged through this method is currently stored in one text file, regardless of the date. This could easily be modified in case the application using the LogHandler requires different.

During execution as the application writes data to a file, the application will place a temporary lock on the text file so that no other application or thread could modify the file at the same exact time. This was done to prevent possible errors that could occur if for example a multiple thread application was trying to log data at the same time in more than one thread causing complications. Once each thread has completed the logging process, the following thread will then be able to write data down.

When using the LogHandler, the application will automatically timestamp the data.

Lastly the application comes with an app.config file. This allows for easy modification of the main log directories and also the file extension of the text file which is created during logs. If the app.config file is not included into the solution once the DLL has been compiled, the log classes have defaults set up being the root of C drive and also a .txt extension for all files.

The solution has been pushed to my GitHub account and available from here...