Changeset 6862 for trunk/sources
- Timestamp:
- 10/03/11 18:34:23 (13 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/Log.cs
r6423 r6862 71 71 } 72 72 public virtual void LogMessage(string message) { 73 string s = DateTime.Now.ToString() + "\t" + message;73 string s = FormatLogMessage(message); 74 74 messages.Add(s); 75 75 CapMessages(); … … 77 77 } 78 78 public virtual void LogException(Exception ex) { 79 string s = DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex);79 string s = FormatException(ex); 80 80 messages.Add(s); 81 81 CapMessages(); … … 86 86 messages.RemoveAt(0); 87 87 } 88 } 89 protected virtual string FormatLogMessage(string message) { 90 return DateTime.Now.ToString() + "\t" + message; 91 } 92 protected virtual string FormatException(Exception ex) { 93 return DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex); 88 94 } 89 95 -
trunk/sources/HeuristicLab.Core/3.3/ThreadSafeLog.cs
r6705 r6862 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.Linq; 26 25 using System.Threading; 27 26 using HeuristicLab.Common; 28 using HeuristicLab.Common.Resources;29 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 28 … … 32 30 [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")] 33 31 [StorableClass] 34 public class ThreadSafeLog : Item, ILog, IStorableContent{32 public class ThreadSafeLog : Log, IDeepCloneable { 35 33 protected ReaderWriterLockSlim locker = new ReaderWriterLockSlim(); 36 34 37 public string Filename { get; set; } 38 39 public override Image ItemImage { 40 get { return VSImageLibrary.File; } 41 } 42 43 public IEnumerable<string> Messages { 35 public override IEnumerable<string> Messages { 44 36 get { 45 37 locker.EnterReadLock(); 46 38 try { 47 return log.Messages.ToArray(); // return copy of messages 48 } finally { locker.ExitReadLock(); } 39 return messages.ToArray(); // return copy of messages 40 } 41 finally { locker.ExitReadLock(); } 49 42 } 50 43 } 51 44 52 [Storable]53 protected ILog log;54 55 45 [StorableConstructor] 56 46 protected ThreadSafeLog(bool deserializing) : base(deserializing) { } 57 public ThreadSafeLog() 58 : base() { 59 this.log = new Log(); 60 RegisterLogEvents(); 61 } 62 public ThreadSafeLog(ILog log) 63 : base() { 64 this.log = log; 65 RegisterLogEvents(); 47 public ThreadSafeLog(long maxMessageCount = -1) 48 : base(maxMessageCount) { 66 49 } 67 50 68 protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner) 69 : base(original, cloner) { 51 protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner) { 70 52 original.locker.EnterReadLock(); 71 53 try { 72 log = cloner.Clone(original.log); 73 } finally { original.locker.ExitReadLock(); } 54 cloner.RegisterClonedObject(original, this); 55 this.messages = new List<string>(original.messages); 56 this.maxMessageCount = original.maxMessageCount; 57 } 58 finally { original.locker.ExitReadLock(); } 74 59 } 60 75 61 public override IDeepCloneable Clone(Cloner cloner) { 76 62 return new ThreadSafeLog(this, cloner); 77 63 } 78 64 79 public virtualvoid Clear() {65 public override void Clear() { 80 66 locker.EnterWriteLock(); 81 67 try { 82 log.Clear(); 83 } finally { locker.ExitWriteLock(); } 68 messages.Clear(); 69 } 70 finally { locker.ExitWriteLock(); } 71 OnCleared(); 84 72 } 85 73 86 public virtual void LogMessage(string message) { 74 public override void LogMessage(string message) { 75 string s = FormatLogMessage(message); 87 76 locker.EnterWriteLock(); 88 77 try { 89 log.LogMessage(message); 90 } finally { locker.ExitWriteLock(); } 78 messages.Add(s); 79 CapMessages(); 80 } 81 finally { locker.ExitWriteLock(); } 82 OnMessageAdded(s); 91 83 } 92 84 93 public virtual void LogException(Exception ex) { 85 public override void LogException(Exception ex) { 86 string s = FormatException(ex); 94 87 locker.EnterWriteLock(); 95 88 try { 96 log.LogException(ex); 97 } finally { locker.ExitWriteLock(); } 89 messages.Add(s); 90 CapMessages(); 91 } 92 finally { locker.ExitWriteLock(); } 93 OnMessageAdded(s); 98 94 } 99 100 #region Log Events101 private void RegisterLogEvents() {102 this.log.Cleared += new EventHandler(log_Cleared);103 this.log.MessageAdded += new EventHandler<EventArgs<string>>(log_MessageAdded);104 this.log.ToStringChanged += new EventHandler(log_ToStringChanged);105 }106 107 private void log_ToStringChanged(object sender, EventArgs e) {108 OnToStringChanged();109 }110 111 private void log_MessageAdded(object sender, EventArgs<string> e) {112 OnMessageAdded(e.Value);113 }114 115 private void log_Cleared(object sender, EventArgs e) {116 OnCleared();117 }118 #endregion119 120 #region Event Handler121 public event EventHandler<EventArgs<string>> MessageAdded;122 protected virtual void OnMessageAdded(string message) {123 EventHandler<EventArgs<string>> handler = MessageAdded;124 if (handler != null) handler(this, new EventArgs<string>(message));125 }126 public event EventHandler Cleared;127 protected virtual void OnCleared() {128 EventHandler handler = Cleared;129 if (handler != null) handler(this, EventArgs.Empty);130 }131 #endregion132 133 134 95 } 135 96 } -
trunk/sources/HeuristicLab/3.3/Tests/ThreadSafeLogTest.cs
r6551 r6862 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using System.Threading.Tasks; 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; 25 27 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 41 43 Assert.AreEqual(count, log.Messages.Count()); 42 44 } 45 46 private ThreadSafeLog recursionInLogViewTestLog; 47 [TestMethod] 48 public void ThreadSafeLogRecursionInLogViewTest() { 49 int count = 10; 50 recursionInLogViewTestLog = new ThreadSafeLog(); 51 recursionInLogViewTestLog.MessageAdded += new EventHandler<EventArgs<string>>(log_MessageAdded); 52 53 for (int i = 0; i < count; i++) { 54 recursionInLogViewTestLog.LogMessage("Message " + i); 55 } 56 } 57 void log_MessageAdded(object sender, EventArgs<string> e) { 58 //access Messages like LogView does 59 Console.WriteLine(string.Join(Environment.NewLine, recursionInLogViewTestLog.Messages.ToArray())); 60 } 43 61 } 44 62 }
Note: See TracChangeset
for help on using the changeset viewer.