Usage

Here is a simplistic example of how to use LogNut to create a simple log output:

1. Include a reference to LogNut.dll and it's dependencies within your project.

2. Add "using LogNut;" to your source-file in which you want to make a call to LogNut.

3. Add the following code to output to a log file in C:\Logs

LogManager.DefaultFileOutputFolder = @"C:\Logs";

var myLogger = LogManager.GetLogger();
myLogger.LogInfo("This is something I want to log.");

Logging Exceptions

You log exceptions the same way, with one of LogNut's Log* methods. With a simple call, it provides a wealth of contextual information to your developers.

Here is an example:

try
{
// do some stuff
}
catch (Exception x)
{
LogManager.LogError(x);
}


LogNut uses the properties that are already built into the C# Exception class to include the source-code file, the class, the method, etc. within your log record. You save the coding effort, and the memory usage by not having to hard-code the strings that you would otherwise need to add to provide that information.

LogNut uses the StackTrace property of the Exception itself to log the location within your source-code (unless you disable that using the LogManager property IsShowingFullStackTrace). If you don't want to log the entire stack-trace (which can be quite verbose), if you are running on .NET Framework version 4.5 it will use the Caller-Info Attributes feature that was introduced in C# 5.0 to log the source-code file path, the class member, and the line within the file that originated that exception.

The lovely thing about all that, is that it saves a heck of a lot of typing and the memory that string literals would consume in your program. Instead of typing in all that contextual information for every single exception handler..

logger.LogError(x, "within method MyVeryLargeClass.MyMethod");

you can supply all of that with simply..

logger.LogError(x);

When logging an exception, you can choose which level to log it as (Info, Warn, Error, Fatal), and you can still include additional information if you want to. For example, to log an exception as level Warn..

logger.LogWarning(x, "The value of Z is " + Z);

Migrating from Log4Net

If you have a large codebase that already uses Log4Net but want to change over to LogNut, you can use many of the exact same API calls. There is an interface provided, ILog, which is compatible with that provided with Log4Net (except that it is better commented), and LogNut's Logger class implements that to provide the exact same logging methods as Log4Net. The configuration task is different of course, as that was one of the original motivations for creating a new logging library.


jh

Last edited Oct 12, 2012 at 5:47 AM by JamesHurst, version 7

Comments

No comments yet.