Skip to content
Ridiculously simple, highly opinionated Logging library for .NET.
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Properties
Tests/Scale.Logger.Tests
.gitignore
ILogger.cs
ILoggerRegistry.cs
LICENSE
LoggerRegistry.cs
README.html
README.md
Scale.Logger.csproj
Scale.Logger.nuspec
Scale.Logger.sln
TraceLogger.cs
packages.config

README.md

Logger

Ridiculously simple, highly opinionated Logging library for .NET.

Prerequisite

.NET Framework 4.5.

Installation

Logger is a Nuget package.

PM> Install-Package FeatherAndScale.Logger

Usage

Instantiate a new LoggerRegistry and assign to a static property, or inject into your classes using IoC.

public static ILoggerRegistry LoggerRegistry = new LoggerRegistry();

Get an instance of ILogger by calling the Logger() method of LoggerRegistry. The Logger() method takes a key as string, which you should generate using the MakeKey() helper method provided. MakeKey() accepts a param array of Ids that will be concatenated into a key; something like "aaa.bbb.ccc".

private ILogger _log = LoggerRegistry.Logger(LoggerRegistry.MakeKey("MyProject", "MyClass", "SomeId"));

If an instance of ILogger with that key has been instantiated before (within the lifetime of the LoggerRegistry) it will be returned, otherwise a new instance will be created.

Now you can use the instance of ILogger to log messages in your code, e.g.

foreach (var image in images)
{
    _log.Message("Importing image {0}", new object[] { image });
    // ... etc
}

There are three methods (plus overloads) that support three log levels:

  • Message()
  • Info()
  • Error()

All accept a message string, which may be a format string. If you want to provide args for the format string, put them in an object array (param arrays are not supported by these methods).

Error() will also take just an Exception, or an Exception and a message. The exception's stack trace will be written to the log in full.

Each method also accepts arguments decorated with Caller Member Attributes These arguments will be set by the compiler, you do not need to set them explicitly.

Output

The log messages are formatted and written to the System.Diagnostics.Trace.WriteLine() method. You may configure where and how Trace information is logged by configuring TraceListener's. Otherwise the default TraceListener will be used. This usually means you will see the log output in your Output window in Visual Studio, or in your Test runner output and/or logs.

Sample output:

[TRACE] Example.Key - TraceLoggerTests.MessageTest(),18	This is a message
vstest.executionengine.x86.exe Information: 0 : [INFO]  Example.Key - TraceLoggerTests.InfoTest(),30	This is an Info message
vstest.executionengine.x86.exe Error: 0 : [ERROR] Example.Key - TraceLoggerTests.ErrorTestThrowException(),91	This is a thrown exception's message.
=======================================================(inner most exception)===
 (1) System.Exception
================================================================================
Thread ID : 10

Message:
"This is a thrown exception's message."

Stack Trace:
   at Scale.Logger.Tests.TraceLoggerTests.ErrorTestThrowException() in c:\Github\Logger\Tests\Scale.Logger.Tests\TraceLoggerTests.cs:line 86
================================================================================
You can’t perform that action at this time.