Skip to content

Commit

Permalink
Fix/bind log trace (#30)
Browse files Browse the repository at this point in the history
* Bind logentry and trace line. Add content information to audit trace line.

* Write log entry in a thread safe way.

* Eventlogger abstraction +event logging to file.

* IAuditEventWriter feature.

* Test for AuditEventWriter feature.

* Increment component version to x.x.x.1

* Fix typos and add the missing IEventLogger interface to the abstract SnEventloggerBase class.

* Increased thread safety + refactor.

* Create custom log directory if it does not exist.

* AuditLog event is Information. The discriminative property is the Category.

* "SnLog_WriteAndReload_SnEventLogger" removed because it is an integration test.
  • Loading branch information
kavics authored Aug 3, 2018
1 parent 6e906db commit 837ac73
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 62 deletions.
77 changes: 26 additions & 51 deletions src/SenseNet.Tools.Tests/SnLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,57 +523,6 @@ private string GetGuidAndMessageFromTrace(string line)
return bindingInfo.Replace("AUDIT ", "").Replace(", Id:-, Path:-", "").Replace("#", "").Replace(":", "").Replace(" ", "|");
}

[TestMethod]
public void SnLog_WriteAndReload_SnEventLogger()
{
var testValue = Guid.NewGuid().ToString();

// action
new SnEventLogger("SenseNet", "SenseNetInstrumentation")
.Write(testValue, null, 0, 0, TraceEventType.Information, null,
new Dictionary<string, object> { { "a", "b" }, { "x", "y" } });

// assert
var logs = EventLog.GetEventLogs();
var log = logs.FirstOrDefault(l => l.LogDisplayName == "SenseNet");
Assert.IsNotNull(log);
var entries = new List<EventLogEntry>();
foreach (EventLogEntry entry in log.Entries)
entries.Add(entry);
var lastEntry = entries.Last();

var entryData = ParseEventlogEntryData(lastEntry.Message);

Assert.AreEqual(testValue, entryData["Message"]);
Assert.AreEqual("Information", entryData["Severity"]);
Assert.AreEqual(Environment.MachineName, entryData["Machine"]);
Assert.AreEqual("a - b, x - y", entryData["Extended Properties"]);
}
private Dictionary<string, string> ParseEventlogEntryData(string text)
{
var result = new Dictionary<string, string>();
var fields = text.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
var index = 0;
while (true)
{
var field = fields[index++];
var p = field.IndexOf(':');
var name = field.Substring(0, p);
var value = field.Length > p ? field.Substring(p + 1).Trim() : string.Empty;
if (name != "Extended Properties")
{
result.Add(name, value);
continue;
}
var extendedValue = new StringBuilder(value);
for (int i = index; i < fields.Length; i++)
extendedValue.Append(", ").Append(fields[i]);
result.Add(name, extendedValue.ToString());
break;
}
return result;
}

[TestMethod]
public void SnLog_WriteAndReload_SnSnFileSystemEventLogger()
{
Expand Down Expand Up @@ -683,5 +632,31 @@ public void SnLog_AuditEventWriter()
Assert.AreEqual("Msg3, Msg4", writerEntries);
}

/* ========================================================================= */

private Dictionary<string, string> ParseEventlogEntryData(string text)
{
var result = new Dictionary<string, string>();
var fields = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var index = 0;
while (true)
{
var field = fields[index++];
var p = field.IndexOf(':');
var name = field.Substring(0, p);
var value = field.Length > p ? field.Substring(p + 1).Trim() : string.Empty;
if (name != "Extended Properties")
{
result.Add(name, value);
continue;
}
var extendedValue = new StringBuilder(value);
for (int i = index; i < fields.Length; i++)
extendedValue.Append(", ").Append(fields[i]);
result.Add(name, extendedValue.ToString());
break;
}
return result;
}
}
}
3 changes: 0 additions & 3 deletions src/SenseNet.Tools/Diagnostics/SnEventloggerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public abstract class SnEventloggerBase : IEventLogger
public virtual void Write(object message, ICollection<string> categories, int priority, int eventId, TraceEventType severity, string title,
IDictionary<string, object> properties)
{
if (severity == TraceEventType.Verbose)
return;

EventLogEntryType entryType;
switch (severity)
{
Expand Down
14 changes: 6 additions & 8 deletions src/SenseNet.Tools/Diagnostics/SnLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,18 @@ private static void BindLogEntryAndTrace(string message, TraceEventType severity
var eventTypeName = severity.ToString().ToUpper();
var traceId = "#" + Guid.NewGuid();
properties["SnTrace"] = traceId;
if (severity <= TraceEventType.Information) // Critical = 1, Error = 2, Warning = 4, Information = 8
{
SnTrace.Event.Write("{0} {1}: {2}", eventTypeName, traceId, message);
}
else
if(categories.Count == 1 && categories[0] == "Audit")
{
properties.TryGetValue("Id", out var contentId);
properties.TryGetValue("Path", out var path);

if (categories.Count == 1 && categories[0] == "Audit")
eventTypeName = "AUDIT";
eventTypeName = "AUDIT";

SnTrace.Event.Write("{0} {1}: {2}, Id:{3}, Path:{4}", eventTypeName, traceId, message, contentId ?? "-", path ?? "-");
}
else
{
SnTrace.Event.Write("{0} {1}: {2}", eventTypeName, traceId, message);
}
}

private static TraceEventType GetEventType(Exception e)
Expand Down

0 comments on commit 837ac73

Please sign in to comment.