Skip to content

markhotchkiss/ErrorLogger

Repository files navigation

Error Logger

This is an Error Logger for C# Applications. It uses a standard class library to allow you to easily log all errors to a single Text File, SQLite or SQL. You can pass in parameters to allow the text file to be created in a specific location with any given name. It also takes care of Archiving where appropriate and even gives you options for reading your log file back in one easy, simple command.

The library has now been updated to include more "Read" options, including reading the last X records, Logs by Category and returning all logs between two dates.

Throw this in your Exceptions for easy exception logging or pass in a string for a static error message.

Check the Usage section below for more info...

Setup

Either clone this repository using your local tools, or download from NuGet.

PM> Install-Package MJH.ErrorLogger

Usage

XML Setup

When you download the NuGet package to your project, you should receive an XML file called "LoggerConfig.xml". All the configuration required to use this package is contained within this file.

Below is an annotated version to get you started!

TextFile Debug (local)\MSSQL2012 ErrorLogger sa Pa55word 0 Activity.db D:\Tests\Logger 0 Activity.log D:\Tests\Logger D:\Tests\Logger\Archive 2 1

Library Usage

The library uses static types therefore there is no need to instantiate the library in your code. You can use a single line, as below, to log an error to either a TextFile, SQLite or SQL.

using MJH.ErrorLogger;
using MJH.ErrorLogger.Models;

    public class MyApplication
    {
        public void DoStuff()
        {
            try
            {
                //Do Stuff
                Logger.LogInfo(LogCategory.Service, "Stuff has been done!")
            }
            catch(Exception e)
            {
                Logger.LogError(LogCategory.Service, e)
            }      
        }
    }

As well as writing to your destination as required, you can also read the Error file using the following...

   var logs = Logger.Read();
   foreach(var log in logs)
   {
      //Do Something.
   }

The Read() method will return your TextFile, SQL Table or SQLite Table as an object of type IReadOnlyCollection which means that you can Iterate the logs and filter/order as needed. The CSV TextFile also gets returned as an Object for ease :-)

Programmatically Change the Configuration

In order to allow you to change the config of the ErrorLogger directly from code, you can do so by Instantiating the ConfigurationHandler() to override any aspect of the configuration. Once you Load it in, you can make your changes and write it back.

Example

Reading the Configuration

using MJH.BusinessLogic.Configuration;

        public void Read()
        {
            var config = new ConfigurationHandler().Read();
            config.LoggingLevel = LoggingLevel.Debug;
            config.LoggerType = LogOutputType.Sql;
        }

Writing the Configuration

using MJH.BusinessLogic.Configuration;

        public void Write(LoggerConfig config)
        {
            var writer = new ConfigurationHandler().Write(config); //Config from the Read() method above after changes.
        }

Other

Feel free to Fork/Download the project and take a look at the UnitTests. These will give you a good idea of the accepted usage of all available methods. If there are any issues/bugs, feel free to log these on GitHub and I will look at them as soon as I can!

Thanks :-)

About

A small library used to initialize a standard logger into any C# application. This will enable you to output Exceptions to a single Text file in any given location.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages