XcoAppSpace Logging Mechanisms

The XcoAppSpace uses very simple yet powerful and extensible logging mechanisms. These mechanisms can help you find problems in your application by looking at log messages that are produced by the application space. By default logging is turned off and only error messages will be shown in the console. This page shows what logging possibilities you have when using the XcoAppSpace.

Default Logging Functionality

By default the XcoAppSpace uses the System.Diagnostics.TraceSource class for logging. This class is available directly in the .Net Framework und provides everything that is needed, including different log levels and the possibility to set different targets where the messages should be logged to. Additionally we chose TraceSource because we want the XcoAppSpace to depend on as few additional libraries as possible (at least when being used with the default configuration options).
Configuring the TraceSource logging is very easy by using the app.config. The XcoAppSpace uses several different source names so that you could even turn on logging just for a certain part of the XcoAppSpace if you want. Here is an example how you can configure logging in the app.config:

    <source name="XcoAppSpaces.Core" switchName="DefaultSwitch"/>
    <source name="XcoAppSpaces.Core.Communication" switchName="DefaultSwitch"/>
    <source name="XcoAppSpaces.Core.Communication.WorkerResolve" switchName="DefaultSwitch"/>
    <source name="XcoAppSpaces.Transport.Sockets" switchName="DefaultSwitch"/>
    <source name="XcoAppSpaces.Transport.WCF" switchName="DefaultSwitch"/>
    <source name="XcoAppSpaces.Transport.MSMQ" switchName="DefaultSwitch"/>
    <source name="XcoAppSpaces.Transport.Jabber" switchName="DefaultSwitch"/>
    <source name="XcoAppSpaces.Security.Basic" switchName="DefaultSwitch"/>
    <!-- Debug Level = "Verbose", Turn Off = "Off" -->
    <add name="DefaultSwitch" value="Verbose" />
    <add name="DefaultListener" type="System.Diagnostics.DefaultTraceListener"/>
This configuration sets the logging level to "Verbose" (which is equal to "Debug" in other logging frameworks) for the complete XcoAppSpace. You could of course define different switches and assign these to some of the sources to only activate logging for parts of the XcoAppSpace. Most of the source names are probably self explanatory (you may notice that they are equal to the namespaces where the according functionality is hosted), but here is a short list with infos about them:
  • XcoAppSpaces.Core: Logs general information about startup, shutdown and when a worker is run or a connection to a worker has been established.
  • XcoAppSpaces.Core.Communication: Logs detailed information about all messages that are sent and received, remote ports that are registered/unregistered and basic information about permission requests to local workers.
  • XcoAppSpaces.Core.Communication.WorkerResolve: Logs detailed information about connection attempts from remote spaces to local workers, as well as attempts from the local space to remote workers.
  • XcoAppSpaces.Transport.*: Logs information specific to the transport service (e.g. opening and closing of connections).
  • XcoAppSpaces.Security.Basic: Logs information specific to the XcoBasicSecurityService.
You can find more info about logging with TraceSource for example here.

Implementing Custom Logging

Maybe you already have a different logging facility in use (e.g. log4net) and want the XcoAppSpace to use it as well? This is no problem, the XcoAppSpace logging functionality can simple be replaced. To do that, you only need to know two classes: The class XcoLoggerFactory and the interface IXcoLogger (both can be found in the namespace XcoAppSpaces.Contracts.Logging). The static XcoLoggerFactory is used by all XcoAppSpace classes to get a logger instance by using its static CreateInstance method, which takes a Type parameter that should define which class is requesting the logger, and with that information creates a new instance of IXcoLogger. What is special about the XcoLoggerFactory is that the CreateInstance method is actually a delegate of type Func<Type, IXcoLogger>. This way, you can easily replace the CreateInstance method with your own version. The following example shows how the logging functionality could be replaced by logger that simply logs all messages to the console:

class ConsoleLogger : IXcoLogger //custom logger implementation
	public void Debug(string msg)
	{ Console.WriteLine("Logging Debug: {0}", msg); }

	public void Info(string msg)
	{ Console.WriteLine("Logging Info: {0}", msg); }

	public void Warn(string msg)
	{ Console.WriteLine("Logging Warn: {0}", msg); }

	public void Error(string msg, Exception ex)
	{ Console.WriteLine("Logging Error: {0}\n{1}", msg, ex.Message); }

	public void Error(string msg)
	{ Console.WriteLine("Logging Error: {0}", msg); }

	public void Fatal(string msg, Exception ex)
	{ Console.WriteLine("Logging Fatal: {0}\n{1}", msg, ex.Message); }

	public void Fatal(string msg)
	{ Console.WriteLine("Logging Fatal: {0}", msg); }

//this is how you replace the default logger (has to be done before instantiating XcoAppSpace!)
XcoLoggerFactory.CreateInstance = (t => new ConsoleLogger());

//an XcoAppSpace component will resolve a new logger instance on startup like this:
IXcoLogger log = XcoLoggerFactory.CreateInstance(this.GetType());

Last edited Feb 26, 2010 at 11:54 AM by thomass, version 4


No comments yet.