Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6862 for trunk/sources


Ignore:
Timestamp:
10/03/11 18:34:23 (13 years ago)
Author:
ascheibe
Message:

#1631 inherited ThreadSafeLog from Log to fix the LockRecursionException problem

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/Log.cs

    r6423 r6862  
    7171    }
    7272    public virtual void LogMessage(string message) {
    73       string s = DateTime.Now.ToString() + "\t" + message;
     73      string s = FormatLogMessage(message);
    7474      messages.Add(s);
    7575      CapMessages();
     
    7777    }
    7878    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);
    8080      messages.Add(s);
    8181      CapMessages();
     
    8686        messages.RemoveAt(0);
    8787      }
     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);
    8894    }
    8995
  • trunk/sources/HeuristicLab.Core/3.3/ThreadSafeLog.cs

    r6705 r6862  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Drawing;
    2524using System.Linq;
    2625using System.Threading;
    2726using HeuristicLab.Common;
    28 using HeuristicLab.Common.Resources;
    2927using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3028
     
    3230  [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
    3331  [StorableClass]
    34   public class ThreadSafeLog : Item, ILog, IStorableContent {
     32  public class ThreadSafeLog : Log, IDeepCloneable {
    3533    protected ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
    3634
    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 {
    4436      get {
    4537        locker.EnterReadLock();
    4638        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(); }
    4942      }
    5043    }
    5144
    52     [Storable]
    53     protected ILog log;
    54 
    5545    [StorableConstructor]
    5646    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) {
    6649    }
    6750
    68     protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
    69       : base(original, cloner) {
     51    protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner) {
    7052      original.locker.EnterReadLock();
    7153      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(); }
    7459    }
     60
    7561    public override IDeepCloneable Clone(Cloner cloner) {
    7662      return new ThreadSafeLog(this, cloner);
    7763    }
    7864
    79     public virtual void Clear() {
     65    public override void Clear() {
    8066      locker.EnterWriteLock();
    8167      try {
    82         log.Clear();
    83       } finally { locker.ExitWriteLock(); }
     68        messages.Clear();
     69      }
     70      finally { locker.ExitWriteLock(); }
     71      OnCleared();
    8472    }
    8573
    86     public virtual void LogMessage(string message) {
     74    public override void LogMessage(string message) {
     75      string s = FormatLogMessage(message);
    8776      locker.EnterWriteLock();
    8877      try {
    89         log.LogMessage(message);
    90       } finally { locker.ExitWriteLock(); }
     78        messages.Add(s);
     79        CapMessages();
     80      }
     81      finally { locker.ExitWriteLock(); }
     82      OnMessageAdded(s);
    9183    }
    9284
    93     public virtual void LogException(Exception ex) {
     85    public override void LogException(Exception ex) {
     86      string s = FormatException(ex);
    9487      locker.EnterWriteLock();
    9588      try {
    96         log.LogException(ex);
    97       } finally { locker.ExitWriteLock(); }
     89        messages.Add(s);
     90        CapMessages();
     91      }
     92      finally { locker.ExitWriteLock(); }
     93      OnMessageAdded(s);
    9894    }
    99 
    100     #region Log Events
    101     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     #endregion
    119 
    120     #region Event Handler
    121     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     #endregion
    132 
    133 
    13495  }
    13596}
  • trunk/sources/HeuristicLab/3.3/Tests/ThreadSafeLogTest.cs

    r6551 r6862  
    2020#endregion
    2121
     22using System;
    2223using System.Linq;
    2324using System.Threading.Tasks;
     25using HeuristicLab.Common;
    2426using HeuristicLab.Core;
    2527using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    4143      Assert.AreEqual(count, log.Messages.Count());
    4244    }
     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    }
    4361  }
    4462}
Note: See TracChangeset for help on using the changeset viewer.