Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

An Extension to the Enterprise Library Logging Application Block

Rate me:
Please Sign up or sign in to vote.
4.76/5 (18 votes)
28 Oct 200612 min read 73K   1.5K   51  
Provide more control over logging by extending the Enterprise Library Logging Application Block.
//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Logging 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.Collections;
using System.Text;
using System.Collections.Generic;

namespace Microsoft.Practices.EnterpriseLibrary.Logging.Formatters
{
    /// <summary>
    /// Formats a dictionary token by iterating through the dictionary and displays 
    /// the key and value for each entry.
    /// </summary>
    public class DictionaryToken : TokenFunction
    {
        private const string DictionaryKeyToken = "{key}";
        private const string DictionaryValueToken = "{value}";

        /// <summary>
        /// Initializes a new instance of a <see cref="DictionaryToken"/>.
        /// </summary>
        public DictionaryToken() : base("{dictionary(")
        {
        }

        /// <summary>
        /// Iterates through each entry in the dictionary and display the key and/or value.
        /// </summary>
        /// <param name="tokenTemplate">Template to repeat for each key/value pair.</param>
        /// <param name="log">Log entry containing the extended properties dictionary.</param>
        /// <returns>Repeated template for each key/value pair.</returns>
        public override string FormatToken(string tokenTemplate, LogEntry log)
        {
            StringBuilder dictionaryBuilder = new StringBuilder();
            foreach (KeyValuePair<string, object> entry in log.ExtendedProperties)
            {
                StringBuilder singlePair = new StringBuilder(tokenTemplate);
                string keyName = "";
                if (entry.Key != null)
                {
                    keyName = entry.Key.ToString();
                }
                singlePair.Replace(DictionaryKeyToken, keyName);

                string keyValue = "";
                if (entry.Value != null)
                {
                    keyValue = entry.Value.ToString();
                }
                singlePair.Replace(DictionaryValueToken, keyValue);

                dictionaryBuilder.Append(singlePair.ToString());
            }

            return dictionaryBuilder.ToString();
        }
    }
}

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
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions