示例#1
0
        /// <summary>
        /// Formats the <see cref="PerformanceEntry"/> object by replacing tokens with values writing the format result
        /// to a <see cref="StringBuilder"/>.
        /// </summary>
        /// <param name="templateBuilder">The <see cref="StringBuilder"/> that holds the formatting result.</param>
        /// <param name="performance">Log entry to format.</param>
        /// <returns>Formatted string with tokens replaced with property values.</returns>
        protected virtual string Format(StringBuilder templateBuilder, PerformanceEntry performance)
        {
            templateBuilder.Replace(TimeStampToken, performance.TimeStampString);
            templateBuilder.Replace(TitleToken, performance.Title);
            templateBuilder.Replace(MessageToken, performance.Message);
            templateBuilder.Replace(EventIdToken, performance.EventId.ToString());
            templateBuilder.Replace(PriorityToken, performance.Priority.ToString());
            templateBuilder.Replace(SeverityToken, performance.Severity.ToString());
            templateBuilder.Replace(ErrorMessagesToke, performance.ErrorMessages);

            templateBuilder.Replace(MachineToken, performance.MachineName);
            templateBuilder.Replace(AppDomainNameToken, performance.AppDomainName);
            templateBuilder.Replace(ProcessIdToken, performance.ProcessId);
            templateBuilder.Replace(ProcessNameToken, performance.ProcessName);
            templateBuilder.Replace(ThreadNameToken, performance.ManagedThreadName);
            templateBuilder.Replace(Win32ThreadIdToken, performance.Win32ThreadId);
            templateBuilder.Replace(ActivityidToken, performance.ActivityId.ToString("D"));

            templateBuilder.Replace(CategoryToken, FormatCategoriesCollection(performance.Categories));

            FormatTokenFunctions(templateBuilder, performance);

            templateBuilder.Replace(NewLineToken, Environment.NewLine);
            templateBuilder.Replace(TabToken, "\t");

            return(templateBuilder.ToString());
        }
示例#2
0
 private void FormatTokenFunctions(StringBuilder templateBuilder, PerformanceEntry performance)
 {
     foreach (TokenFunction token in _tokenFunctions)
     {
         token.Format(templateBuilder, performance);
     }
 }
示例#3
0
        private void WriteTraceMessage(string message, string entryTitle, TraceEventType eventType)
        {
            var extendedProperties = new Dictionary <string, object>();
            var entry = new PerformanceEntry(message, PeekLogicalOperationStack() as string, Priority, EventId, eventType, entryTitle, extendedProperties);

            GetWriter().Write(entry);
        }
示例#4
0
        /// <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="performance">Log entry containing the extended properties dictionary.</param>
        /// <returns>Repeated template for each key/value pair.</returns>
        public override string FormatToken(string tokenTemplate, PerformanceEntry performance)
        {
            var dictionaryBuilder = new StringBuilder();

            foreach (KeyValuePair <string, object> entry in performance.ExtendedProperties)
            {
                var    singlePair = new StringBuilder(tokenTemplate);
                string keyName    = string.Empty;
                if (entry.Key != null)
                {
                    keyName = entry.Key;
                }

                singlePair.Replace(DictionaryKeyToken, keyName);

                string keyValue = string.Empty;
                if (entry.Value != null)
                {
                    keyValue = entry.Value.ToString();
                }

                singlePair.Replace(DictionaryValueToken, keyValue);

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

            return(dictionaryBuilder.ToString());
        }
        /// <summary>
        /// Creates a new <see cref="PerformanceEntry"/> that is a copy of the current instance.
        /// </summary>
        /// <remarks>
        /// If the dictionary contained in <see cref="ExtendedProperties"/> implements <see cref="ICloneable"/>, the resulting
        /// <see cref="PerformanceEntry"/> will have its ExtendedProperties set by calling <c>Clone()</c>. Otherwise the resulting
        /// <see cref="PerformanceEntry"/> will have its ExtendedProperties set to <see langword="null"/>.
        /// </remarks>
        /// <implements>ICloneable.Clone</implements>
        /// <returns>A new <c>PerformanceEntry</c> that is a copy of the current instance.</returns>
        public object Clone()
        {
            var result = new PerformanceEntry
            {
                Message           = Message,
                EventId           = EventId,
                Title             = Title,
                Severity          = Severity,
                Priority          = Priority,
                TimeStamp         = TimeStamp,
                MachineName       = MachineName,
                AppDomainName     = AppDomainName,
                ProcessId         = ProcessId,
                ProcessName       = ProcessName,
                ManagedThreadName = ManagedThreadName,
                ActivityId        = ActivityId,
                Categories        = new List <string>(Categories)
            };

            // clone categories

            // clone extended properties
            if (_extendedProperties != null)
            {
                result.ExtendedProperties = new Dictionary <string, object>(_extendedProperties);
            }

            // clone error messages
            if (_errorMessages != null)
            {
                result._errorMessages = new StringBuilder(_errorMessages.ToString());
            }

            return(result);
        }
示例#6
0
        /// <summary>
        /// Searches for the reflected property and returns its value as a string
        /// </summary>
        /// <param name="tokenTemplate">
        /// The token Template.
        /// </param>
        /// <param name="performance">
        /// The performance.
        /// </param>
        /// <returns>
        /// The format token.
        /// </returns>
        public override string FormatToken(string tokenTemplate, PerformanceEntry performance)
        {
            Type         logType  = performance.GetType();
            PropertyInfo property = logType.GetProperty(tokenTemplate);

            if (property != null)
            {
                object value = property.GetValue(performance, null);
                return(value != null?value.ToString() : string.Empty);
            }

            return(String.Format("Error: property {0} not found", tokenTemplate));
        }
示例#7
0
        private static EventLogEntryType GetEventLogEntryType(PerformanceEntry performance)
        {
            switch (performance.Severity)
            {
            case TraceEventType.Critical:
            case TraceEventType.Error:
                return(EventLogEntryType.Error);

            case TraceEventType.Warning:
                return(EventLogEntryType.Warning);

            default:
                return(EventLogEntryType.Information);
            }
        }
示例#8
0
        /// <summary>
        /// Searches for token functions in the message and replace all with formatted values.
        /// </summary>
        /// <param name="messageBuilder">Message template containing tokens.</param>
        /// <param name="performance">Log entry containing properties to format.</param>
        public virtual void Format(StringBuilder messageBuilder, PerformanceEntry performance)
        {
            int pos = 0;

            while (pos < messageBuilder.Length)
            {
                string messageString = messageBuilder.ToString();
                if (messageString.IndexOf(_startDelimiter) == -1)
                {
                    break;
                }

                string tokenTemplate  = GetInnerTemplate(pos, messageString);
                string tokenToReplace = _startDelimiter + tokenTemplate + _endDelimiter;
                pos = messageBuilder.ToString().IndexOf(tokenToReplace);

                string tokenValue = FormatToken(tokenTemplate, performance);

                messageBuilder.Replace(tokenToReplace, tokenValue);
            }
        }
        /// <summary>
        /// Formats the timestamp property with the specified date time format string.
        /// </summary>
        /// <param name="tokenTemplate">Date time format string.</param>
        /// <param name="performance">Log entry containing the timestamp.</param>
        /// <returns>Returns the formatted time stamp.</returns>
        public override string FormatToken(string tokenTemplate, PerformanceEntry performance)
        {
            string result;

            if (tokenTemplate.Equals(LocalStartDelimiter, System.StringComparison.InvariantCultureIgnoreCase))
            {
                System.DateTime localTime = performance.TimeStamp.ToLocalTime();
                result = localTime.ToString();
            }
            else if (tokenTemplate.StartsWith(LocalStartDelimiterWithFormat, System.StringComparison.InvariantCultureIgnoreCase))
            {
                string          formatTemplate = tokenTemplate.Substring(LocalStartDelimiterWithFormat.Length);
                System.DateTime localTime      = performance.TimeStamp.ToLocalTime();
                result = localTime.ToString(formatTemplate, CultureInfo.CurrentCulture);
            }
            else
            {
                result = performance.TimeStamp.ToString(tokenTemplate, CultureInfo.CurrentCulture);
            }

            return(result);
        }
示例#10
0
 /// <overloads>
 /// Formats the <see cref="PerformanceEntry"/> object by replacing tokens with values
 /// </overloads>
 /// <summary>
 /// Formats the <see cref="PerformanceEntry"/> object by replacing tokens with values.
 /// </summary>
 /// <param name="performance">Log entry to format.</param>
 /// <returns>Formatted string with tokens replaced with property values.</returns>
 public override string Format(PerformanceEntry performance)
 {
     return(Format(CreateTemplateBuilder(), performance));
 }
示例#11
0
 public void Write(PerformanceEntry performance)
 {
     EventLog.WriteEntry(performance.Title, performance.Message, GetEventLogEntryType(performance));
 }
 public void Write(PerformanceEntry performance)
 {
     Trace.WriteLine(performance.Message, performance.Title);
 }
示例#13
0
 /// <summary>
 /// Abstract method to process the token value between the start and end delimiter.
 /// </summary>
 /// <param name="tokenTemplate">Token value between the start and end delimiters.</param>
 /// <param name="performance">Log entry to process.</param>
 /// <returns>Formatted value to replace the token.</returns>
 public abstract string FormatToken(string tokenTemplate, PerformanceEntry performance);