User Manual

Adding Logging Services

The Mapzania SDk provides detailed logging for all its operations.

These logs include:

  • Error Log - Exceptions are logged to this log
  • Debug Log - Detailed debugging information is logged to this log
  • Message Log - Messages relating to service events are logged to this log
  • Custom Logs - A number of custom events are logged to these logs. These include events such as WebApi calls, Metrics and Usage.

Logging services are provided via the registration of a concrete logging class that implements the Mapzania.Logging.ILogger interface in the Mapzania.Sdk libraries.

To implement these using your preferred logging platform (e.g. NLog or log4net), a concrete class of the above interface needs to be passed to Mapzania at startup.

This process is explained in detail below:

Add a concrete logging class to your project

The first step is to create a logger class that implements the Mapzania.Logging.ILogger interface and add it to your solution.

Below is example source code for an NLog implementation of this class. If this does not suit your purposes, you can write your own implementation using this as an example.

NLog implementation

NLogLogger.cs

using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Generic;

namespace Mapzania.Logging
{
    public class NLogLogger : Mapzania.Logging.ILogger
    {
        List<string> _customNames = new List<string>();
        string _dir;

        public NLogLogger(string dir)
        {
            if (dir == null)
                throw new ArgumentNullException("dir");

            _dir = dir;

            makeTarget("Message");
            makeTarget("Debug");
            makeTarget("Error");
        }

        public void Message(string msg, string module)
        {
            log("Message", msg, module);
        }

        public void Debug(string msg, string module)
        {
            log("Debug", msg, module);
        }

        public void Error(string msg, string module)
        {
            log("Error", msg, module);
        }

        public void Custom(string logName, string msg, string module)
        {
            if (!_customNames.Contains(logName))
                makeTarget(logName);

            log(logName, msg, module);
        }

        private void log(string logName, string msg, string module)
        {
            var logger = LogManager.GetLogger(logName);
            logger.Log(LogLevel.Info, string.Format("{0}|{1}", module, msg));
        }

        private void makeTarget(string name)
        {
            var config = LogManager.Configuration ?? new LoggingConfiguration();
            var target = new FileTarget();

            config.AddTarget(name, target);

            target.FileName = string.Format("{0}\\{1}.log", _dir, name);
            target.Layout = "${date}|${message}";

            var rule = new LoggingRule(name, LogLevel.Info, target);

            config.LoggingRules.Add(rule);

            LogManager.Configuration = config;
        }
    }
}

Register the logger with the Mapzania.Service at startup

To register an instance of the logger that you created above with the Mapzania.Service add the highlighted code to your global.asax file before starting the Mapzania Service:


public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        var logPath = @"c:\logs";
        var logger = new Mapzania.Logging.NLogLogger(logPath);
        Mapzania.Services.RegisterLogger(logger);

        Mapzania.Services.Start(this);
    
        AreaRegistration.RegisterAllAreas();
    
        GlobalConfiguration.Configure(WebApiConfig.Register);
    
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

Using the TraceLogger

Mapzania also has a built-in logger (Mapzania.Logging.TraceLogger) that can be used in a development environment to send logging to the Output window.


public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        var logger = new Mapzania.Logging.TraceLogger();
        Mapzania.Services.RegisterLogger(logger);

        Mapzania.Services.Start(this);
    
        AreaRegistration.RegisterAllAreas();
    
        GlobalConfiguration.Configure(WebApiConfig.Register);
    
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

Registering Multiple Loggers

It is also possible to register multiple loggers as in the example below:


public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        var logPath = @"c:\logs";
        var logger = new NLogLogger(logPath);
        Mapzania.Services.RegisterLogger(logger);

        var logger2 = new Log4NetLogger();
        Mapzania.Services.RegisterLogger(logger2);
    
        Mapzania.Services.Start(this);
    
        AreaRegistration.RegisterAllAreas();
    
        GlobalConfiguration.Configure(WebApiConfig.Register);
    
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

See Mapzania.Services.RegisterLogger for reference documentation.