Difference between revisions of "C"
From Richard's Wiki
(→TimingScope) |
(→TimingScope) |
||
Line 61: | Line 61: | ||
<code> | <code> | ||
− | using System; | + | using System; |
− | using System.Diagnostics; | + | using System.Diagnostics; |
namespace Common | namespace Common | ||
{ | { | ||
Line 70: | Line 70: | ||
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 77: | ||
_timer = new Stopwatch(); | _timer = new Stopwatch(); | ||
} | } | ||
− | + | ||
public void Start() | public void Start() | ||
{ | { | ||
Line 83: | Line 83: | ||
_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 90: | ||
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 99: | ||
} | } | ||
} | } | ||
− | + | ||
public void Stop() | public void Stop() | ||
{ | { | ||
Line 105: | Line 105: | ||
_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() | ||
{ | { |
Revision as of 18:15, 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
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; } } } }