// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TraceHelper.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Trace helper class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
namespace Catel.Diagnostics
{
/// <summary>
/// Trace helper class.
/// </summary>
public static class TraceHelper
{
#region Variables
/// <summary>
/// Available trace levels.
/// </summary>
private static Dictionary<TraceEventType, TraceLevel> _traceLevels;
/// <summary>
/// Dictionary of currently living stopwatch traces.
/// </summary>
private static readonly Dictionary<string, Stopwatch> _stopwatchTracers = new Dictionary<string, Stopwatch>();
#endregion
#region Methods
/// <summary>
/// Starts the trace of a stopwatch trace. Should be ended with a call to <see cref="StopStopwatchTrace"/>.
/// </summary>
/// <param name="name">The name of the stopwatch.</param>
public static void StartStopwatchTrace(string name)
{
StartStopwatchTrace(name, true);
}
/// <summary>
/// Starts the trace of a stopwatch trace. Should be ended with a call to <see cref="StopStopwatchTrace"/>.
/// </summary>
/// <param name="name">The name of the stopwatch.</param>
/// <param name="traceNameOnStart">if set to <c>true</c>, the name will be traced both on the start and stop of the stopwatch.</param>
public static void StartStopwatchTrace(string name, bool traceNameOnStart)
{
if (_stopwatchTracers.ContainsKey(name))
{
_stopwatchTracers[name].Stop();
_stopwatchTracers[name].Reset();
_stopwatchTracers[name].Start();
}
else
{
_stopwatchTracers.Add(name, new Stopwatch());
_stopwatchTracers[name].Start();
}
if (traceNameOnStart)
{
Trace.TraceInformation(name);
}
}
/// <summary>
/// Stops the stopwatch trace and writes the duration as Verbose to the trace.
/// </summary>
/// <param name="name">The name of the stopwatch.</param>
public static void StopStopwatchTrace(string name)
{
if (!_stopwatchTracers.ContainsKey(name))
{
return;
}
_stopwatchTracers[name].Stop();
Trace.TraceInformation("{0} took {1}", name, _stopwatchTracers[name].Elapsed);
}
/// <summary>
/// Converts a <see cref="TraceEventType"/> to a <see cref="TraceLevel"/>.
/// </summary>
/// <param name="eventType"><see cref="TraceEventType"/> to convert.</param>
/// <returns><see cref="TraceLevel"/> that represents a <see cref="TraceEventType"/>.</returns>
public static TraceLevel ConvertTraceEventTypeToTraceLevel(TraceEventType eventType)
{
if (_traceLevels == null)
{
_traceLevels = new Dictionary<TraceEventType, TraceLevel>();
_traceLevels.Add(TraceEventType.Critical, TraceLevel.Error);
_traceLevels.Add(TraceEventType.Error, TraceLevel.Error);
_traceLevels.Add(TraceEventType.Warning, TraceLevel.Warning);
_traceLevels.Add(TraceEventType.Information, TraceLevel.Info);
_traceLevels.Add(TraceEventType.Verbose, TraceLevel.Verbose);
}
return _traceLevels.ContainsKey(eventType) ? _traceLevels[eventType] : TraceLevel.Off;
}
/// <summary>
/// Traces an error message with details.
/// </summary>
/// <param name="message">Message of the trace.</param>
/// <param name="details">Additional details which will be listed later in the trace.</param>
public static void TraceErrorWithDetails(string message, string details)
{
TraceWithDetails(TraceLevel.Error, message, details);
}
/// <summary>
/// Traces a warning message with details.
/// </summary>
/// <param name="message">Message of the trace.</param>
/// <param name="details">Additional details which will be listed later in the trace.</param>
public static void TraceWarningWithDetails(string message, string details)
{
TraceWithDetails(TraceLevel.Warning, message, details);
}
/// <summary>
/// Traces an information message with details.
/// </summary>
/// <param name="message">Message of the trace.</param>
/// <param name="details">Additional details which will be listed later in the trace.</param>
public static void TraceInformationWithDetails(string message, string details)
{
TraceWithDetails(TraceLevel.Info, message, details);
}
/// <summary>
/// Traces a verbose message with details.
/// </summary>
/// <param name="message">Message of the trace.</param>
/// <param name="details">Additional details which will be listed later in the trace.</param>
public static void TraceVerboseWithDetails(string message, string details)
{
TraceWithDetails(TraceLevel.Verbose, message, details);
}
/// <summary>
/// Traces a message with details.
/// </summary>
/// <param name="level"><see cref="TraceLevel"/> to write.</param>
/// <param name="message">Message of the trace.</param>
/// <param name="details">Additional details which will be listed later in the trace.</param>
public static void TraceWithDetails(TraceLevel level, string message, string details)
{
string traceMessage = string.Format(CultureInfo.CurrentUICulture, message, details);
switch (level)
{
case TraceLevel.Error:
Trace.TraceError(traceMessage);
break;
case TraceLevel.Warning:
Trace.TraceWarning(traceMessage);
break;
case TraceLevel.Info:
Trace.TraceInformation(traceMessage);
break;
case TraceLevel.Verbose:
Trace.Write(traceMessage);
break;
}
}
#endregion
}
}