How to use log4net as logger

.NET library for SMPP protocol
Locked
Stephan
Posts: 1
Joined: Mon Sep 22, 2014 8:44 am

How to use log4net as logger

Post by Stephan » Mon Sep 22, 2014 8:49 am

In case anyone else will be needing this, here is the code I use for InetLab logging to use an existing log4net configuration.

Use:

Code: Select all

// Setup inetlab to use log4net by using an adapter
Inetlab.SMPP.Logging.LogManager.SetLoggerFactory(loggerName => new Log4NetToInetLabLoggingAdapter(log4net.LogManager.GetLogger(loggerName)));
Adapter class:

Code: Select all

/// <summary>
	/// Enables InetLab framework to use 
	/// </summary>
	public class Log4NetToInetLabLoggingAdapter : Inetlab.SMPP.Logging.ILog
	{
		private readonly ILog _log;
		public Log4NetToInetLabLoggingAdapter(log4net.ILog log4netLogger)
		{
			_log = log4netLogger;
		}

		public void Error(object message, Exception ex)
		{
			_log.Error(message, ex);
		}

		public void Error(object message)
		{
			_log.Error(message);
		}

		public void Warn(object message, Exception ex)
		{
			_log.Warn(message, ex);
		}

		public void Warn(object message)
		{
			_log.Warn(message);
		}

		public void Info(object message)
		{
			// Ignore info logging from smtp
		}

		public void Debug(object message)
		{
			// Ignore debug logging from smtp
		}

		public void Trace(object message)
		{
			// Ignore trace logging from smtp
		}

		public void Trace(object message, byte[] data)
		{
			// Ignore trace logging from smtp
		}
	}
Locked