#23 Add managed thread Id to ChronicleRecord

parent a9acff44
Pipeline #100 passed with stages
in 7 minutes and 31 seconds
......@@ -43,7 +43,7 @@ namespace KSharp.NChronicle.Core.Tests.ForChronicleLibrary
public void ThenTheDefaultPatternIsAsExpected()
{
var result = this._library.ResolveMessageOutputWithDefaultPattern(this._record, TimeZoneInfo.Utc);
Assert.AreEqual($"{this._record.UtcTime.ToString("yyyy/MM/dd HH:mm:ss.fff")} [{Thread.CurrentThread.ManagedThreadId}] {this._record.Message} \n{this._record.Exception}\n[{String.Join(", ", this._record.Tags)}]", result);
Assert.AreEqual($"{this._record.UtcTime.ToString("yyyy/MM/dd HH:mm:ss.fff")} [{this._record.ThreadId}] {this._record.Message} \n{this._record.Exception}\n[{String.Join(", ", this._record.Tags)}]", result);
}
[TestMethod]
......@@ -57,7 +57,7 @@ namespace KSharp.NChronicle.Core.Tests.ForChronicleLibrary
public void ThenRecordThreadCanBeRendered()
{
var result = this._library.ResolveMessageOutput(this._record, TimeZoneInfo.Utc, "{TH}");
Assert.AreEqual(Thread.CurrentThread.ManagedThreadId.ToString(), result);
Assert.AreEqual(this._record.ThreadId.ToString(), result);
}
[TestMethod]
......@@ -122,7 +122,7 @@ namespace KSharp.NChronicle.Core.Tests.ForChronicleLibrary
{
this._record = new ChronicleRecord(ChronicleLevel.Debug, null, this._exception);
var result = this._library.ResolveMessageOutput(this._record, TimeZoneInfo.Utc, "{EXC?{TH}}{MSG?{LVL}}");
Assert.AreEqual(Thread.CurrentThread.ManagedThreadId.ToString(), result);
Assert.AreEqual(this._record.ThreadId.ToString(), result);
}
[TestMethod]
......
......@@ -54,6 +54,13 @@ namespace KSharp.NChronicle.Core.Tests.ForChronicleRecord
Assert.IsFalse(this._recordOne.Equals(this._recordTwo), "The records are incorrectly considered equal.");
}
[TestMethod]
public void AndAllButTheThreadIdAreEqualThenTheChronicleRecordsAreNotEqual()
{
this._recordOne.ThreadId = 99;
Assert.IsFalse(this._recordOne.Equals(this._recordTwo), "The records are incorrectly considered equal.");
}
[TestMethod]
public void AndAllButTheExceptionsAreEqualThenTheChronicleRecordsAreNotEqual()
{
......
......@@ -46,6 +46,13 @@ namespace KSharp.NChronicle.Core.Tests.ForChronicleRecord
Assert.AreEqual(this._recordOne.GetHashCode(), this._recordTwo.GetHashCode(), "The hash codes are not equal.");
}
[TestMethod]
public void AndAllButTheThreadIdAreEqualThenTheHashCodesAreEqual()
{
this._recordOne.ThreadId = 99;
Assert.AreEqual(this._recordOne.GetHashCode(), this._recordTwo.GetHashCode(), "The hash codes are not equal.");
}
[TestMethod]
public void AndAllButTheTimeAreEqualThenTheHashCodesAreEqual()
{
......
......@@ -50,6 +50,7 @@ namespace KSharp.NChronicle.Core.Tests.ForChronicleRecord
Assert.IsNotNull(deserializedRecord.Exception, "The exception was not deserialized.");
Assert.IsNotNull(deserializedRecord.Tags, "The tags were not deserialized.");
Assert.AreEqual(originalRecord.ThreadId, deserializedRecord.ThreadId, "The message was deserialized but incorrectly.");
Assert.AreEqual(originalRecord.Message, deserializedRecord.Message, "The message was deserialized but incorrectly.");
Assert.AreEqual(originalRecord.UtcTime, deserializedRecord.UtcTime, "The time was not deserialized correctly.");
Assert.AreEqual(originalRecord.Level, deserializedRecord.Level, "The level was not deserialized correctly.");
......
......@@ -286,7 +286,7 @@ namespace KSharp.NChronicle.Core.Abstractions
private string ThreadKeyHandler(ChronicleRecord record)
{
return Thread.CurrentThread.ManagedThreadId.ToString();
return record.ThreadId.ToString();
}
private string TagsKeyHandler(ChronicleRecord record)
......
......@@ -11,6 +11,11 @@ namespace KSharp.NChronicle.Core.Abstractions
public interface IChronicleRecord
{
/// <summary>
/// The managed thread Id for the thread on which this record was created.
/// </summary>
int ThreadId { get; }
/// <summary>
/// The date and time of when this record was created in UTC.
/// </summary>
......
......@@ -4,6 +4,7 @@ using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace KSharp.NChronicle.Core.Model
{
......@@ -17,6 +18,7 @@ namespace KSharp.NChronicle.Core.Model
internal ChronicleRecord()
{
this.ThreadId = Thread.CurrentThread.ManagedThreadId;
this.UtcTime = DateTime.UtcNow;
this.Message = null;
this.Tags = new List<string>().AsReadOnly();
......@@ -35,6 +37,7 @@ namespace KSharp.NChronicle.Core.Model
/// <param name="tags">Tags to append to this record. Optional.</param>
public ChronicleRecord(ChronicleLevel level, string message = null, Exception exception = null, params string[] tags)
{
this.ThreadId = Thread.CurrentThread.ManagedThreadId;
this.UtcTime = DateTime.UtcNow;
this.Message = message;
this.Tags = tags?.ToList().AsReadOnly() ?? new List<string>().AsReadOnly();
......@@ -42,6 +45,11 @@ namespace KSharp.NChronicle.Core.Model
this.Level = level;
}
/// <summary>
/// The managed thread Id for the thread on which this record was created.
/// </summary>
public int ThreadId { get; internal set; }
/// <summary>
/// The date and time of when this record was created in UTC.
/// </summary>
......@@ -67,6 +75,7 @@ namespace KSharp.NChronicle.Core.Model
/// </summary>
public ChronicleException Exception { get; internal set; }
/// <summary>
/// Get a boolean value indicating whether this Chronicle Record can
/// be considered equal to <paramref name="object"/> by
......@@ -99,6 +108,7 @@ namespace KSharp.NChronicle.Core.Model
&& (this.Exception == null) == (anotherRecord.Exception == null)
&& (this.Exception == null || this.Exception.Equals(anotherRecord.Exception))
&& this.Message == anotherRecord.Message
&& this.ThreadId == anotherRecord.ThreadId
&& this.Level == anotherRecord.Level
&& (this.Tags == null) == (anotherRecord.Tags == null)
&& (this.Tags == null || new HashSet<string>(this.Tags).SetEquals(anotherRecord.Tags));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment