Click here to Skip to main content
14,174,238 members
Click here to Skip to main content
Add your own
alternative version

Stats

7.6K views
15 bookmarked
Posted 20 Dec 2015
Licenced CPOL

A Plug and Play Logging Library for Dummies

, 20 Dec 2015
Rate this:
Please Sign up or sign in to vote.
An easy to implement yet powerful logging library which requires absolute zero learning curve

Introduction

Application development cycle includes so many phases and there are some activities which take high precedence, say design and architecture of coding. However, some are considered as least important until a major maintenance roadblock occurs. For many programmers, integrating proper exceptions details, errors messages and debug info into logging entries and handling them for extended use is considered as the least priority and comes as the last item in checklist. This is a sad fact that leads to a lot of additional maintenance and costs a lot to businesses.

Background

As you already know, .NET Framework doesn’t ship with logging libraries. Luckily, there is one provided with Enterprise Library which is used extensively and there are third party modules like Log4Net. These are the best available in the market and I use these two whenever there is an enterprise business requirement. However, when it comes to small applications and utilities developed by enthusiast developers or 1 week projects, these libraries are kind of heavyweight.

I did a little search for a plug and play logging framework yet includes all the required basic functionalities for a basic developer. Of course, there was not much coming up in Google search which matches what I had in mind and so I decided to develop one on my own and figured it could be a new idea which could be useful to some folks.

What is Special About It?

  1. Plug and Play - No need for config entries or initial values
  2. Built to work seamlessly with huge amount of logs implementing multiple threads
  3. Capable of logging parameter values from method the exception is caught
  4. Auto logging for unhandled Exceptions
  5. Wait for Log completion asynchronously
  6. Failsafe logging capabilities - Write to OS Application Event Log
  7. Standalone exception parser with timestamp

Using the Code

There is no need for adding configuration entries or specifying default parameters before using the logging library. If the required library can be configured to work with application configuration entries(App.config/web.config).

Sample:

static void Main(string[] args)
{
// This method can be the first entry in application and it will log entries to default file path.
Logging.AddExceptionEntry(new Exception("New custom Exception"), 
	"Custom Exception Header", args);
}

Approaches of Logging

  1. Three ways of logging:
    1. Logging.AddLogEntry
    2. Logging.AddDebugEntry
    3. Logging.AddExceptionEntry
  2. Customize via app.config or properties:
    1. LogFilepath, ExceptionFilePath, DebugFilePath
    2. DebugLogEnabled
    3. FileWriteWaitTimeoutSeconds
    4. WindowsEventsSectionName

Sample:

static void Main(string[] args)
{
    Logging.AddExceptionEntry(…);
    Logging.AddLogEntry(…);
    Logging. AddDebugEntry(…);
}

Optionally Logs Method Parameter Values

Possibly, this is one unique ability of this logging library. When you see an exception happened in an application, the most important thing you are missing will be the data which triggered the issue so that you can reproduce it. This library got helpers to add parameter information directly to error logs so that it can be utilized by developers when troubleshooting.

Sample:

Method Call

ExceptionHandler("f", "l", 200);

Exception Log Implementation

static void ExceptionHandler(string firstname,string lastname,int age)
{
      try
      {
            throw new ArgumentOutOfRangeException("dummy", 200, "Expect less that 130");
       }
       catch (Exception ex)
       {
// Parameter values can be passed to Exception logs and 
// this will be matched with method parameter names and argument types
             Logging.AddExceptionEntry
             (ex, "Exception on ExceptionHandlerCase1", firstname, lastname, age);
       }
}

Resulting Log Entry

11/15/2015 3:14:55 PM: Exception on ExceptionHandlerCase1 - Expect less that 130 
Parameter name: dummy Actual value was 200. - 
METHOD EXCEPTION CAUGHT = LoggerTest.Program.ExceptionHandler
( System.String firstname=f, System.String lastname=l, Int32 age=200). 
METHOD EXCEPTION TRIGGERED = LoggerTest.Program.ExceptionHandler
(System.String firstname, System.String lastname, Int32 age). STACK TRACE: 
--    at LoggerTest.Program.ExceptionHandler(String firstname, String lastname, Int32 age) 
in e:\Projects\SQLDBBaseGeneric\LoggerTest\Program.cs:line 76.

Logs Unhandled Exceptions

Once initialized or called at least for one time, this library logs exceptions even if it is not handled anywhere in the application before shutdown.

Sample:

static void Main(string[] args)
{
    Logging.AddLogEntry("End Application");
    throw new Exception("Test"); //this exception will be logged.
    Logging.WaitForLogComplete();
}

Multithreaded

Every log is a separate thread and will not block the main application. The library also provides a mechanism to know and wait for pending logs to complete in case required like wait to log complete before terminating process.

Sample:

static void Main(string[] args)
{
     Logging.AddExceptionEntry(new Exception("New custom Exception"), "Custom Exception Header", args);
     Logging.WaitForLogComplete();
}

Future Plans

As of now, I have focused only on desktop libraries and applications and focused a little on ASP.NET. Also, I have created this library to write into log files and got a method which returns log information from an exception object which can be fed into other sources like database.

In case this module is adopted by a fair amount of developers, I am planning to put some effort to add logging capabilities directly to Database (Oracle/SQLServer) and other sources if any based on feedback.

Let me know your thoughts and comments.

History

  • V1.0 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

shijo joseph
Technical Lead
United States United States
Started Programming career with VB 6 and VC++ and now Into .Net Development. Working with .Net since its first release (Version 1.0 Beta). Lucky enough to continue always with most updated versions of .Net and as of now; May 2007, fighting with .Net 3.0 (WPF,WCF...etc) and it's next most anticipated version LINQ.

Got familiarity up on the .Net Technologies and Guidelines like CAB, and Patterns and Practices, Enterprise Library and now with it's WPF version etc.

Specialized in Windows and Distributed and Service oriented applications.

You may also be interested in...

Pro

Comments and Discussions

 
-- No messages could be retrieved (timeout) --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190524.3 | Last Updated 20 Dec 2015
Article Copyright 2015 by shijo joseph
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid