TranceLayer 

A fast and flexible logging and/or diagnostics framework for .NET
It allows you to plug it into an existing or new application with minimal changes to the code.
Or it can be used as in classic scenarios of logging things along the way at different levels.

Features

The following features are proven to work within TranceLayer:

Light - The size of the library itself is 14K + the persister(s) you need. This is configurable, unneeded code is not loaded.
Fast     - Light means (both in direct and philosophical ways) no complexity => works as fast as it could get executed by the CLR.
Flexible - The architecture for TranceLayer logging framework is made in a way that it will only do what you tell it to, both at code loading and functionality.

Architecture

To give you a quick understanding of how it works take a look at the logo, make an imaginary arrow from Blue to Pink and from Green to Orange,
and you’ll have it all.

Adapters - Components (assemblies) that help with logging integration tasks
Modifiers - Components (assemblies) that help with logging scheduling and scaling tasks
Persisters - Components (assemblies) that help with storing the logs

Once you grab these simple concepts, everything else is up to you. These components can be mixed up as you want, so you can build the pipe
through where the logs flow. Let’s give some examples, they make the digestion easier.


Example 1 – Log to a text file
  • download the assemblies or sources
  • reference the "Logging" assembly in your project
  • modify the configSections of the App.config or Web.config to look like this
    <configSections>
        <section name="Logging.Config"
                 type="Logging.Config, Logging" />
        <section name="PlainFile.Config"
                 type="Logging.Persisters.PlainFile.Config, Logging.Persisters.PlainFile" />
    </configSections>
  • parameter the TranceLayer by adding these lines to App.config or Web.config
    <Logging.Config
        verbosity="eDebugs"
        persister="Logging.Persisters.PlainFile" />

    <PlainFile.Config
        logFile="C:\\log.log" />
  • write some logs, the effect is that they will go to the storages (persisters) that are indicated in the config file
    using Logging;
    ...
    public static void Main()
    { 
         ILogger logger = LogManager.GetLogger();
                 logger.LogInfo( "Message of logical category" );
                 logger.LogTechnicalInfo( "Message of technical category" );
    }
 
Example 2 – Hooking into System.Diagnostics and redirect those logs to a text file
  • download the assemblies or sources
  • reference the “Logging” assembly in your project
  • modify the configSections to look like this
    <configSections>
        <section name="Logging.Config"
                 type="Logging.Config, Logging" />
        <section name="PlainFile.Config"
                 type="Logging.Persisters.PlainFile.Config, Logging.Persisters.PlainFile" />
    </configSections>
  • register a new trace listener
    <system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="TraceListener.Logger"
                     type="Logging.Adapters.In.TraceListener.Logger, Logging.Adapters.In.TraceListener" />
            </listeners>
        </trace>
    </system.diagnostics>
  • parameter the TranceLayer
    <Logging.Config
        verbosity="eDebugs"
        persister="Logging.Persisters.PlainFile" />

    <PlainFile.Config
        logFile="C:\\log.log" />
  • Test the Trace , and you should have all the logs in a file.
    using System.Diagnostics;
    ...
    public static void Main()
    {
            Trace.WriteLine( "Start" );
            Trace.Indent();
                Trace.WriteLine( "aaaaaaaa" );
                Trace.WriteLine( "aaaaaaaa" );
                Trace.WriteLine( "aaaaaaaa" );
                Trace.Indent();
                    Trace.WriteLine( "bbbbbbbb" );
                    Trace.WriteLine( "bbbbbbbb" );
                    Trace.WriteLine( "bbbbbbbb" );
                Trace.Unindent();
            Trace.Unindent();
            Trace.WriteLine( "End" );
    }
 
Example 3 – Asynchronous logging to different storages(persisters) + Email notification (just another persister)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <!-- Logging Definition
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <configSections>
       
        <section name="Logging.Config"
                 type="Logging.Config, Logging" />

        <section name="Async.Config"
                 type="Logging.Modifiers.Async.Config, Logging.Modifiers.Async" />

        <section name="Multi.Config"
                 type="Logging.Modifiers.Multi.Config, Logging.Modifiers.Multi" />
       
        <section name="PlainFile.Config"
                 type="Logging.Persisters.PlainFile.Config, Logging.Persisters.PlainFile" />

        <section name="WindowsEventLog.Config"
                 type="Logging.Persisters.WindowsEventLog.Config, Logging.Persisters.WindowsEventLog" />

        <section name="Email.Config"
                 type="Logging.Persisters.Email.Config, Logging.Persisters.Email" />

    </configSections>

   
    <!-- Logging Configuration
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <Logging.Config
        verbosity="eDebugs"
        persister="Logging.Modifiers.Async" /> <!-- Means we want all logs till levele "Debug" to flow to the psersister "Async" -->

    <Async.Config
        persister="Logging.Modifiers.Multi" /> <!-- Means the "Async" will pass what it gets to the "Multi" in an asynchronous way -->

    <Multi.Config                              <!-- Multi is specialized to pass what he gets to a list of persisters so they can actually store the logs -->
        persisters="Logging.Persisters.PlainFile;Logging.Persisters.SystemConsole;Logging.Persisters.WindowsEventLog;Logging.Persisters.Email" />

    <PlainFile.Config
        logFile="C:\\log.log" />

    <WindowsEventLog.Config
        source="Logging.Tester" />

    <Email.Config
        mailServer  ="192.168.1.59"
        fromMail    ="user@domain.com"
        fromUser    ="user"
        fromPassword="password"
        to          ="targetUser1@domain.com:targetUser2@domain.com" />

</configuration>


What happens with a config like this is that you have in your hands the control to build and shape the pipe through which the logs flow.
Step 1: We want an non blocking call to log API for that reason we use Async module
Step 2: Then by using Multi we want to stock the logs into several storages ( a practical use would be log file and send an email with the error )
Step 3: Each module specified in Multi gets its specific parameters it need to function
Step 4: Done, we start log things we need at Trace level, Debug level and other intermediary levels ....


 

Extending with custom modules 

The framework is extensible enough to give you a quick boost to add new things you need or combine existing modules for the things you have.
If you need some additional functionality that is not covered in the .vsd file provided with the sources, I'll be glad to know it.

To write your own Adapter, Modifier or Persister is easy. Take a look at one provided in the package but adapt, modify or store as you need.

Last edited Apr 21, 2011 at 12:42 AM by Nicolae, version 33