Difference between revisions of "C"

From Richard's Wiki
Jump to: navigation, search
(TimingScope)
(TimingScope)
 
(2 intermediate revisions by the same user not shown)
Line 59: Line 59:
  
 
===== TimingScope =====
 
===== TimingScope =====
 
+
Usage:
 
<code>
 
<code>
using System;
+
using (TimingScope.StartNew(string.Format("Extract Output XML: {0} {1}", foo, bar), Logger)) {
using System.Diagnostics;
+
 +
}
 +
</code>
 +
Code:
 +
<code>
 +
using System;
 +
using System.Diagnostics;
 
  namespace Common
 
  namespace Common
 
  {
 
  {
Line 70: Line 76:
 
         private readonly string _name;
 
         private readonly string _name;
 
         private readonly Stopwatch _timer;
 
         private readonly Stopwatch _timer;
 
+
 
         public TimingScope(string name, log4net.ILog logger)
 
         public TimingScope(string name, log4net.ILog logger)
 
         {
 
         {
Line 77: Line 83:
 
             _timer = new Stopwatch();
 
             _timer = new Stopwatch();
 
         }
 
         }
 
+
 
         public void Start()
 
         public void Start()
 
         {
 
         {
Line 83: Line 89:
 
             _timer.Start();
 
             _timer.Start();
 
         }
 
         }
 
+
 
         public static TimingScope StartNew(string name, log4net.ILog logger)
 
         public static TimingScope StartNew(string name, log4net.ILog logger)
 
         {
 
         {
Line 90: Line 96:
 
             return scope;
 
             return scope;
 
         }
 
         }
 
+
 
         public static TimeSpan Time(string name, log4net.ILog logger, Action operation)
 
         public static TimeSpan Time(string name, log4net.ILog logger, Action operation)
 
         {
 
         {
Line 99: Line 105:
 
             }
 
             }
 
         }
 
         }
 
+
 
         public void Stop()
 
         public void Stop()
 
         {
 
         {
Line 105: Line 111:
 
             _logger.DebugFormat("{0} completed in {1:0.000} s", _name, _timer.ElapsedMilliseconds / 1000.0);
 
             _logger.DebugFormat("{0} completed in {1:0.000} s", _name, _timer.ElapsedMilliseconds / 1000.0);
 
         }
 
         }
 
+
 
         public void Dispose()
 
         public void Dispose()
 
         {
 
         {
 
             Stop();
 
             Stop();
 
         }
 
         }
 
+
 
         public TimeSpan Elapsed
 
         public TimeSpan Elapsed
 
         {
 
         {

Latest revision as of 18:21, 26 October 2015

Get Property Name via Lambda Expression

To use it do something like this.PropertyName(()=>this.Property);

using System;
using System.Linq.Expressions;
using System.Reflection;
private string PropertyName<T>(Expression<Func<T>> property)
{
    var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
    if (propertyInfo == null)
    {
      throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
    }
    var propertyName = propertyInfo.Name;
    return propertyName;
}

SerializeToXml
       using System.IO;
       using System.Xml;
       /// <summary>
       /// Serializes to XML.
       /// </summary>
       /// <param name="firstClassElement">The first class element to serialize.</param>
       /// <returns>The XML string that represents the passed first class element.</returns>
       public virtual string SerializeToXml(FirstClassElementType firstClassElement)
       {
           string xmlData;
           var memoryStream = new MemoryStream();
           XmlTextWriter writer = null;
           try
           {
               writer = new XmlTextWriter(memoryStream, Encoding.Unicode)
                            {
                                Formatting = Formatting.Indented,
                                Indentation = 0
                            };
               var xmlSerializer = new XmlSerializer(typeof (FirstClassElementType));
               xmlSerializer.Serialize(writer, firstClassElement);
               writer.Flush();
           }
           finally
           {
               memoryStream.Seek(0, SeekOrigin.Begin);
               var textReader = new StreamReader(memoryStream);
               xmlData = textReader.ReadToEnd();
               textReader.Close();
               if (null != writer)
                   writer.Close();
           }
           return xmlData;
       }
TimingScope

Usage:

using (TimingScope.StartNew(string.Format("Extract Output XML: {0} {1}", foo, bar), Logger)) {

}

Code:

using System;
using System.Diagnostics;
namespace Common
{
   public class TimingScope : IDisposable
   {
       private readonly log4net.ILog _logger;
       private readonly string _name;
       private readonly Stopwatch _timer;

       public TimingScope(string name, log4net.ILog logger)
       {
           _name = name;
           _logger = logger;
           _timer = new Stopwatch();
       }

       public void Start()
       {
           _logger.DebugFormat("{0} starting", _name);
           _timer.Start();
       }

       public static TimingScope StartNew(string name, log4net.ILog logger)
       {
           var scope = new TimingScope(name, logger);
           scope.Start();
           return scope;
       }

       public static TimeSpan Time(string name, log4net.ILog logger, Action operation)
       {
           using (var timer = StartNew(name, logger))
           {
               operation();
               return timer.Elapsed;
           }
       }

       public void Stop()
       {
           _timer.Stop();
           _logger.DebugFormat("{0} completed in {1:0.000} s", _name, _timer.ElapsedMilliseconds / 1000.0);
       }

       public void Dispose()
       {
           Stop();
       }

       public TimeSpan Elapsed
       {
           get { return _timer.Elapsed; }
       }
   }
}