Git Product home page Git Product logo

errorlogger's Introduction

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 :-)

errorlogger's People

Contributors

markhotchkiss avatar

Stargazers

Bryan Cleland avatar  avatar Hambalkó Bence avatar

Watchers

James Cloos avatar

Forkers

arvindrajt

errorlogger's Issues

SQL Logger causes memory leak

When logging to an SQL Database, every time a new log is created, the process execute time gets longer and longer until it no longer works. Possible memory leak.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.