StreamLink for Silverlight
Caplin.StreamLink.Logging Namespace
StreamLink for SilverlightCaplin.StreamLink.Logging
The StreamLink.Logging namespace provides logging facilities for StreamLink.

An API user must also implement the ILogger interface in order to receive log messages from StreamLink.

Declaration Syntax
C#Visual BasicVisual C++
namespace Caplin.StreamLink.Logging
Namespace Caplin.StreamLink.Logging
namespace Caplin.StreamLink.Logging
Types
All TypesClassesStructuresEnumerations
IconTypeDescription
ConsoleLogger
An implementation of ILogger that simply logs exceptions and other log messages to the console.

FileLogger
This FileLogger is provided for compatibility purposes only.

FileLoggerConfiguration
The configuration settings for the StreamLink file logger.

SourceLevels
Enumeration of levels used in StreamLink logging.

StreamLinkLoggingCategories
Defines the logging categories that are used internally by the StreamLink library.

Remarks

An example ConsoleLogger is supplied with StreamLink. However it is recommended that you only use this implementation for internal debugging purposes while you are developing your StreamLink enabled application. Your deployed application should use a more fully functioned logging framework, such as Apache log4net, which offers a wider range of logging options.

Examples
The following example shows how to register an ILogger with StreamLink. In this case the Ilogger is the ConsoleLogger, provided with StreamLink.
CopyC#
using System;
using System.Collections.Generic;
using System.Text;

using Caplin.StreamLink;
using Caplin.StreamLink.Logging;
using System.Diagnostics;
using Caplin.StreamLink.Authentication;

namespace StreamLinkExamples.Basics
{
    /// <summary>
    /// Example inner class that implements ILogger and
    /// a an outline of how it may be used.
    /// </summary>
    public class SettingUpALogger
    {
        /// <summary>
        /// Example of a class that implements ILogger and
        /// a an outline of how it may be used.
        /// </summary>
        public static void SettingUpALoggerExample()
        {
            IStreamLink myStreamLink = new StreamLink();

            // Set the logger property to start using the console logger
            myStreamLink.Logger = new ConsoleLogger();
        }
    }
}
The previous example used the ConsoleLogger supplied by StreamLink, whose code has been reproduced below for convenience:
CopyC#
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections.Specialized;

namespace Caplin.StreamLink.Logging
{
    /// <summary>
    /// Used to handle logging and simply logs exceptions to the console.
    /// </summary>
    public class ExampleLogger : ILogger, IExceptionHandler
    {
        private List<string> _logCategoryFilter;
        private SourceLevels level = SourceLevels.All;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
        /// </summary>
        public ExampleLogger() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
        /// </summary>
        /// <param name="logCategoryFilter">The log category filter.</param>
        public ExampleLogger(string[] logCategoryFilter)
        {
            _logCategoryFilter = new List<string>();
            _logCategoryFilter.AddRange(logCategoryFilter);
        }

        /// <summary>
        /// The level that messages are logged at,
        /// for example if the level is set to SourceLevels.Information then only messages at information level and above will be logged 
        /// (i.e. Critical, Error, Warning, Information).
        /// The default value is SourceLevels.All which will log all messages.
        /// </summary>
        /// <value>The level above which to log messages</value>
        public SourceLevels Level
        {
            get { return level; }
            set { level = value; }
        }

        #region ILogger Members

        /// <summary>
        /// Logs at the specified level.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <param name="category">The category.</param>
        /// <param name="message">The message.</param>
        public void Log(SourceLevels level, string category, string message)
        {
            Log(level, new string[] { category }, message);
        }

        /// <summary>
        /// Logs at the specified level.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <param name="category">The category.</param>
        /// <param name="message">The message.</param>
        /// <param name="msgParts">The MSG parts.</param>
        public void Log(SourceLevels level, string category, string message, params string[] msgParts)
        {
            this.Log(level, category, string.Format(message, msgParts));
        }

        /// <summary>
        /// Logs at the specified level.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <param name="categories">The categories.</param>
        /// <param name="message">The message.</param>
        public void Log(SourceLevels level, string[] categories, string message)
        {
            // check level of message and only log if < defined level
            if (this.level != SourceLevels.All && level.CompareTo(this.level) > 0)
            {
                return;
            }

            bool log = true;

            if (this._logCategoryFilter != null)
            {
                log = false;
                foreach (string category in categories)
                {
                    if (this._logCategoryFilter.Contains(category) == true)
                    {
                        log = true;
                        break;
                    }
                }
            }

            if(log == true)
            {
                string msg = string.Format("{0}: {1}: {2}: {3}",
                             DateTime.Now.ToString("u"),
                             level.ToString(),
                             String.Join(",", categories),
                             message);

                Console.WriteLine(msg);
            }
        }

        /// <summary>
        /// Logs the specified level.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <param name="categories">The categories.</param>
        /// <param name="message">The message.</param>
        /// <param name="msgParts">The message parts to be formatted into <paramref name="message"/>.</param>
        public void Log(SourceLevels level, string[] categories, string message, params string[] msgParts)
        {
            this.Log(level, categories, string.Format(message, msgParts));
        }
        #endregion

        #region IExceptionHandler Members

        /// <summary>
        /// Logs an exeception
        /// </summary>
        /// <param name="ex">The ex.</param>
        public void HandleException(Exception ex)
        {
            this.Log(SourceLevels.Error, "Exception", ex.ToString());
        }

        #endregion
    }
}