Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / C#

An MSN Messenger Log Listener

Rate me:
Please Sign up or sign in to vote.
4.66/5 (19 votes)
6 Feb 20066 min read 131.4K   1.5K   71  
A custom log listener for the Microsoft Enterprise Library Logging framework.
//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Logging and Instrumentation Application Block
//===============================================================================
// Copyright � Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;

using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Design.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.Design.TraceListeners;

using WCPierce.Practices.EnterpriseLibrary.Logging.MsnMessenger.Configuration;
using WCPierce.Practices.EnterpriseLibrary.Logging.MsnMessenger.Configuration.Design.Properties;

namespace WCPierce.Practices.EnterpriseLibrary.Logging.MsnMessenger.Configuration.Design
{
  /// <summary>
  /// Represents a <see cref="FormattedDatabaseTraceListenerData"/> configuraiton element.
  /// </summary> 
  public sealed class LoggingMsnMessengerNode : TraceListenerNode
  {
    string _sSenderAccount;
    string _sSenderPassword;
    string _sReceiverAccounts;
    int _iConnectionRetryInterval;
    int _iInvitationRetryInterval;
    int _iMaxMessageQueue;
    string _sFormatterName;
    private FormatterNode _formatterNode;

    /// <summary>
    /// Initialize a new instance of the <see cref="LoggingDatabaseNode"/> class.
    /// </summary>
    public LoggingMsnMessengerNode()
      : this(new FormattedMsnMessengerTraceListenerData())
    {
    }

    /// <summary>
    /// Initialize a new instance of the <see cref="LoggingDatabaseNode"/> class with a <see cref="FormattedDatabaseTraceListenerData"/> instance.
    /// </summary>
    /// <param name="traceListenerData">A <see cref="FormattedDatabaseTraceListenerData"/> instance</param>
    public LoggingMsnMessengerNode(FormattedMsnMessengerTraceListenerData traceListenerData)
    {
      if (null == traceListenerData) throw new ArgumentNullException("traceListenerData");

      Rename(traceListenerData.Name);
      TraceOutputOptions = traceListenerData.TraceOutputOptions;

      _sSenderAccount = traceListenerData.SenderAccount;
      _sSenderPassword = traceListenerData.SenderPassword;
      _sReceiverAccounts = traceListenerData.ReceiverAccounts;
      _iConnectionRetryInterval = traceListenerData.ConnectionRetryInterval;
      _iInvitationRetryInterval = traceListenerData.InvitationRetryInterval;
      _iMaxMessageQueue = traceListenerData.MaxMessageQueue;
      _sFormatterName = traceListenerData.Formatter;
    }

    /// <summary>
    /// The account used to send Log messages
    /// </summary>
    [Required]
    [SRDescription("SenderAccount", typeof(Resources))]
    [SRCategory("CategoryGeneral", typeof(Resources))]
    public string SenderAccount
    {
      get { return _sSenderAccount; }
      set { _sSenderAccount = value; }
    }

    /// <summary>
    /// The password for sSenderAccount
    /// </summary>
    [Required]
    [SRDescription("SenderPassword", typeof(Resources))]
    [SRCategory("CategoryGeneral", typeof(Resources))]
    public string SenderPassword
    {
      get { return _sSenderPassword; }
      set { _sSenderPassword = value; }
    }

    /// <summary>
    /// A semi-colon (;) delimited list of accounts that will receive Log messages
    /// </summary>
    [Required]
    [SRDescription("ReceiverAccounts", typeof(Resources))]
    [SRCategory("CategoryGeneral", typeof(Resources))]
    public string ReceiverAccounts
    {
      get { return _sReceiverAccounts; }
      set { _sReceiverAccounts = value; }
    }

    /// <summary>
    /// (Optional) The time in Minutes between Connection attempts.  DefaultValue = 5 Minutes
    /// </summary>
    [SRDescription("ConnectionRetryInterval", typeof(Resources))]
    [SRCategory("CategoryGeneral", typeof(Resources))]
    public int ConnectionRetryInterval
    {
      get { return _iConnectionRetryInterval; }
      set { _iConnectionRetryInterval = value; }
    }

    /// <summary>
    /// (Optional) The time in Minutes between Invitation attempts.  DefaultValue = 5 Minutes
    /// </summary>
    [SRDescription("InvitationRetryInterval", typeof(Resources))]
    [SRCategory("CategoryGeneral", typeof(Resources))]
    public int InvitationRetryInterval
    {
      get { return _iInvitationRetryInterval; }
      set { _iInvitationRetryInterval = value; }
    }

    /// <summary>
    /// (Optional) The maximum number of messages to queue for delivery.  If this value is 
    /// exceeded, the oldest message is removed from the queue before adding the latest message.  
    /// DefaultValue = 5 Messages
    /// </summary>
    [SRDescription("MaxMessageQueue", typeof(Resources))]
    [SRCategory("CategoryGeneral", typeof(Resources))]
    public int MaxMessageQueue
    {
      get { return _iMaxMessageQueue; }
      set { _iMaxMessageQueue = value; }
    }

    /// <summary>
    /// Gets or sets the formatter to use.
    /// </summary>
    /// <value>
    /// The formatter to use.
    /// </value>
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(FormatterNode))]
    [SRDescription("FormatDescription", typeof(Resources))]
    [SRCategory("CategoryGeneral", typeof(Resources))]
    public FormatterNode Formatter
    {
      get { return _formatterNode; }
      set
      {
        _formatterNode = LinkNodeHelper.CreateReference<FormatterNode>(
          _formatterNode,
          value,
          OnFormatterNodeRemoved,
          OnFormatterNodeRenamed);

        _sFormatterName = _formatterNode == null ? string.Empty : _formatterNode.Name;
      }
    }

    /// <summary>
    /// Gets the <see cref="FormattedDatabaseTraceListenerData"/> this node represents.
    /// </summary>
    /// <value>
    /// The <see cref="FormattedDatabaseTraceListenerData"/> this node represents.
    /// </value>
    public override TraceListenerData TraceListenerData
    {
      get
      {
        return new FormattedMsnMessengerTraceListenerData(
          Name,
          _sSenderAccount,
          _sSenderPassword,
          _sReceiverAccounts,
          _iConnectionRetryInterval,
          _iInvitationRetryInterval,
          _iMaxMessageQueue,
          _sFormatterName);
      }
    }

    /// <summary>
    /// Sets the formatter to use for this listener.
    /// </summary>
    /// <param name="formatterNodeReference">
    /// A <see cref="FormatterNode"/> reference or <see langword="null"/> if no formatter is defined.
    /// </param>
    protected override void SetFormatterReference(ConfigurationNode formatterNodeReference)
    {
      if (_sFormatterName == formatterNodeReference.Name) Formatter = (FormatterNode)formatterNodeReference;
    }

    private void OnFormatterNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
      _sFormatterName = null;
    }

    private void OnFormatterNodeRenamed(object sender, ConfigurationNodeChangedEventArgs e)
    {
      _sFormatterName = e.Node.Name;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions