public static Task ProcessItemsInParallel <T>(this IEnumerable <T> items, Action <T> doWork, Action <T> ok = null, Action <T, Exception> failure = null, Action <T> finished = null, ITimingBucket timings = null) { var tasks = new List <Task>(); items.ProcessItemsInternal <T>((item) => { var task = Task.Run <Exception>(() => { try { doWork(item); return(null); } catch (Exception ex) { return(ex); } }); tasks.Add(task); return(task); }, ok, failure, finished, timings); // wait... var log = LogSet.GetLog(typeof(CollectionsExtender)); if (log.IsInfoEnabled) { log.InfoFormat("{0}: waiting for threads...", typeof(T).Name); } return(Task.WhenAll(tasks.ToArray())); }
public void DumpToLog(ILog log = null) { if (log == null) { log = LogSet.GetLog <ProcessRunResults>(); } if (log.IsInfoEnabled) { log.Info(this.ToString()); } }
private void menuLogTestMessage_Click(object sender, System.EventArgs e) { ILog log = LogSet.GetLog(this.GetType()); log.Debug("I am a debug message"); log.Info("I am a info message"); log.Warn("I am a warning message"); log.Error("I am a error message"); log.Fatal("I am a fatal message"); // tell... Alert.ShowInformation(this, "Messages have been written to all categories."); }
protected override void OnResize(EventArgs e) { base.OnResize(e); if (this.DesignMode) { return; } try { // get... Image image = this.ResolvedImage; Size size = Size.Empty; if (image == null) { size = new Size(32, 32); } else { size = image.Size; } // get... Rectangle all = new Rectangle(this.ClientRectangle.Location, this.ClientRectangle.Size); const int padding = 5; all.Inflate(0 - padding, 0 - padding); // image... _imageRectangle = new Rectangle(all.Right - size.Width - padding, all.Top + (all.Height / 2) - (size.Height / 2), size.Width, size.Height); _textRectangle = new Rectangle(all.Left, all.Top, all.Right - _imageRectangle.Width - (2 * padding), all.Height); // invalidate... this.Invalidate(); } catch (Exception ex) { ILog log = LogSet.GetLog(this.GetType()); if (log.IsWarnEnabled) { log.Warn("Failed when resizing.", ex); } } }
internal static void ProcessItemsInternal <T>(this IEnumerable <T> items, Func <T, Task <Exception> > doWork, Action <T> ok, Action <T, Exception> failure, Action <T> finished, ITimingBucket bucket) { if (doWork == null) { throw new ArgumentNullException("doWork"); } var log = LogSet.GetLog(typeof(CollectionsExtender)); if (bucket == null) { bucket = NullTimingBucket.Instance; } var name = typeof(T).Name; var result = new ProcessItemsResult <T>(DateTime.UtcNow); if (items.Any()) { log.LogTrace(() => string.Format("{0}: processing '{1}' items(s)...", name, items.Count())); var tasks = new List <Task>(); foreach (var item in items) { try { var timer = new AccurateTimer(); timer.Start(); var error = doWork(item); timer.Stop(); if (error == null) { throw new InvalidOperationException("Work delegate returned null."); } // error? error.Wait(); if (error.Result != null) { throw new InvalidOperationException("Item processing failed.", error.Result); } // set... result.Timings[item] = timer.DurationAsDecimal; if (ok != null) { ok(item); } } catch (Exception ex) { if (log.IsErrorEnabled) { if (typeof(T).IsAssignableFrom(typeof(IEntityId))) { log.Error(string.Format("Failed to process item of type '{0}' with ID #{1}.", name, ((IEntityId)item).Id), ex); } else { log.Error(string.Format("Failed to process item of type '{0}'.", name), ex); } } if (failure != null) { failure(item, ex); } } finally { if (finished != null) { finished(item); } } } log.LogTrace(() => string.Format("{0}: finished.", name)); } else { log.LogTrace(() => string.Format("{0}: nothing to do.", name)); } }