Posts tagged with: logging

Introducing my logging library

I’ve just published the first version (consider it a beta) of my logging library to the official nuget server(s).

Why another library when there are existing ones as log4net and nlog? Because it should be easier to get started and you should still have the power to customize the logging if needed. Griffin.Logging do not have a configuration file, everything is configured through code.

Simple syntax

The easiest approach only requires one line to configure the framework:

SimpleLogManager.AddFile(@"D:LogFilesMyLog.log");

You can swap the AddFile method to AddConsole if you would like.

Fluent syntax

Use the fluent syntax if you need more power:

var config = new FluentConfiguration()
  .LogNamespace("Fadd.Test").AndChildNamespaces.ToTargetNamed("Console")
  .LogNamespace("System").AndChildNamespaces.ToTargetNamed("DefaultFile")
  .AddTarget("Console")
	  .As.ConsoleLogger().Filter.OnLogLevelBetween(LogLevel.Info, LogLevel.Error)
	  .Done 
  .AddTarget("DefaultFile")
	  .As.FileLogger("ErrorsOnly").Filter.OnLogLevel(LogLevel.Error)
	  .As.FileLogger("Everything")
	  .Done
  .Build();

Usage

public class MyClass
{
    ILogger logger = LogManager.GetLogger<MyClass>();

    public MyClass()
    {
        // Built in formatting for cleaner syntax.
        logger.Info("Welcome {0}!.", user.Name);
    }
    public void HideError()
    {
        try
        {
            throw new InvalidOperationException("No no", new OutOfScopeException("Can't thing anymore"));
        }
        catch (Exception err)
        {
            // Log an exception. The inner exceptions are logged too (recursive)
            logger.Warning("Ooops, we got an exception.", ex);
        }
    }

}

Code

Check the homepage for more information or just install the nuget package “griffin.logging“.


Nlog configuration

I’ve switched from my own logging framework to nlog. NLog seems quite flexible is under active development and suits my needs perfectly. I’m logging both to the console and to files using the following configuration file:

  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>

  <nlog xmlns=" http://www.nlog-project.org/schemas/NLog.xsd"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true" internalLogFile=" Nlog.txt">
    <targets>
      <target name="file" xsi:type="File"
              layout="${longdate} ${threadid:padding=3} ${level:padding=-30} ${logger:padding=-30} ${message} ${exception:format=tostring}"
              fileName="${basedir}/logs/${shortdate}.txt"/>
      <target name="errors" xsi:type="File"
              layout="${longdate} ${threadid:padding=3} ${level:padding=-30} ${logger:padding=-30} ${message} ${exception:format=tostring}"
          fileName="${basedir}/logs/${shortdate}.errors.log" />
      <target name="console" xsi:type="ColoredConsole"
              layout="${date:format=HH:MM:ss} ${threadid:padding=3} ${logger:padding=-30} ${message}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="file, console" />
      <logger name="*" minLevel="Error" writeTo="errors" />
    </rules>
  </nlog>

The console get’s colored output looking like this:

10:07:41  10 AppDog.Monitor                 Checking AppDog.Monitoring.MonitoredApplication

And file syntax:

2010-07-03 10:22:56.4218  10 Trace           AppDog.Monitor                 Checking AppDog.Monitoring.MonitoredApplication