Simplifying your Software Design: Logging

The LogNut open-source project on GitHub is a facility that I created to do one thing: to simplify this one aspect of software-design. Logging.

NutWithMagnifier

In some of your best programming-environments you can type your code, make it RUN — and watch what it does. But not always. Sometimes you add logging statements to your code just to have some way to see what is happening. What you don’t want, is for that logging facility itself to be adding to the complexity of your endeavor.

I prefer my tools to be spartan and simple. I want them to just work. Always, and reliably. Without a lot of prep, reading, and thought — “just flip’n do it and don’t get in my way!”

This discussion is about the C# version of LogNut, on the Microsoft Windows platform, but the general design aspects apply to it’s other versions (F#, ASP.NET-MVC, Xamarin.Forms, Java, Swift, C++).

With a .NET application, the very minimum that you have to do to use a library (called as ‘assembly’ in the .NET world) is to reference it, and then call it’s API. Most logging frameworks have multiple other things that you must reference, configuration-files to prepare, documentation to read in order to figure out where those config-files must be located and where your output will go, etc.

I’ve eliminated the need for most of that. You can still do it (control the output-location, make use of config-files, perform extensive configuration of every detail) but you don’t *have* to. For example, this is a perfectly acceptable bit of code to output the text “Hey – this code is working” into a log file (a simple text file):

using Hurst.LogNut;
...
LogManager.LogInfo("Hey - this code is working");

And that is it. Where does it go? Since you did not specify, it goes into the one safe place that is always there for you — your ‘My Documents’ folder, into a folder called “Logs”. Since you also did not specify the name of the file, it is named after the name of your program that you are running, with “_Log.txt” appended so that you can open it by simply double-clicking on that file just like any other text file.

This is all clearly spelled-out in the library’s introductory documentation, it’s sample-code, and within the API itself.

What if you specify some other place to write the log to, but that place is not there (such as a flash-drive that has been removed)? It ‘fails-over’ and writes it to that same, fall-back location (‘My Documents\Logs’) but with “_REDIRECTED” inserted into the filename.

LogNut is a full-power logging library; you can totally do full-feature stuff such as:

var myLogger = LogManager.GetLogger("Message-Q");
myLogger.LogTrace("This is normal");
myLogger.LogDebug("Why is this executing?");
myLogger.LogError("Uh oh!");

and your exception-handling blocks can be accomplished with one simple line of code…

}
catch (Exception x)
{
MyLogger.LogException(x);
}

If you’re starting a new software-project in C# or any other of the supported platforms/languages and are contemplating what to select for your logging library, check it out:

https://github.com/JamesWHurst/LogNut

Advertisements

About James W Hurst

a professional software designer since the beginning days of the desktop cptr and uC-controlled avionics, I today am focusing on Java, Swift, C# and F# for building mobile and desktop and online applications under Android, Xamarin.Forms, iOS, WPF, and ASP.NET-MVC along with the requisite HTML/CSS/JavaScript/Ajax for web applications. My database expertise has covered a panoply of different database-engines and modeling approaches, and my main area of academic interest is Artificial Intelligence and vision.
This entry was posted in C#, Software Design, The Development Process and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s