Skip to content

Logging

MilleBo edited this page Mar 3, 2017 · 1 revision

Testura.Android provide logging by something call listeners. With listeners you can add your own way of logging and it's to make sure the logging is flexible and fit your soluion.

You start by implementing the ILogListener interface:

public interface ILogListener
{
	/// <summary>
	/// Write a new log message
	/// </summary>
	/// <param name="className">Name of the calling class</param>
	/// <param name="memberName">Method or property name of the calling class</param>
	/// <param name="message">Message from the calling class</param>
	void Log(string className, string memberName, string message);
}

Then you add your implementation to the DeviceLoggerClass:

DeviceLogger.AddListener(new YourImplementation());

Now you're done and Testura.Android will provide you with log messages.

Example of implementation

/// <summary>
/// A simple console log listener that write log messages
/// to the console.
/// </summary>
public class ConsoleLogListener : ILogListener
{
	/// <summary>
	/// Write log message to the console
	/// </summary>
	/// <param name="className">Name of the calling class</param>
	/// <param name="memberName">Method or property name of the calling class</param>
	/// <param name="message">Message from the calling class</param>
	public void Log(string className, string memberName, string message)
	{
		Console.WriteLine($"[{DateTime.Now}][{className}][{memberName}]: {message}");
	}
}

DeviceLogger methods

/// <summary>
/// Add a new log listener to our log listener list
/// </summary>
/// <param name="logListener">Log listener to add</param>
public static void AddListener(ILogListener logListener)

/// <summary>
/// Get all current log listeners
/// </summary>
/// <returns>A list with all log listeners</returns>
public static IReadOnlyList<ILogListener> GetListeners()

/// <summary>
/// Remove all log listeners
/// </summary>
/// <param name="logListener">Log listener to remove</param>
public static void RemoveListener(ILogListener logListener)
Clone this wiki locally