The StreamLink.Logging namespace provides logging facilities for StreamLink.NET.
An API user must implement the ILogger interface defined within this namespace in order to receive log messages from StreamLink.NET.
![](../icons/collapse_all.gif)
C# | Visual Basic | Visual C++ |
namespace Caplin.StreamLink.Logging
Namespace Caplin.StreamLink.Logging
namespace Caplin.StreamLink.Logging
![](../icons/collapse_all.gif)
All Types | Classes | Structures |
Icon | Type | Description |
---|---|---|
![]() | ConsoleLogger |
An implementation of ILogger that simply logs exceptions
and other log messages to the console.
|
![]() | FileLoggerConfiguration |
The configuration settings for the StreamLink file logger.
|
![]() | LoggingCategories |
Defines the logging categories that are used internally by the StreamLink library.
|
![](../icons/collapse_all.gif)
An example ConsoleLogger has been supplied, however it is recommended that this only used for internal debugging purposes and that a logging framework such as log4net is used which offers many features such as Windows Event Log logging as well as file and console logging.
![](../icons/collapse_all.gif)
An example of how to register a Logger with streamlink is as follows:
CopyC#
The previous example used the ConsoleLogger supplied by StreamLink.NET, whose code has been reproduced
below for convenience:
CopyC#
![](../icons/CopyCode.gif)
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() { StreamLink myStreamLink = new StreamLink(); // Set the logger property to start using the console logger myStreamLink.Logger = new ConsoleLogger(); } } }
![](../icons/CopyCode.gif)
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 StringCollection _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 StringCollection(); _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 } }