-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The change aims to add detailed tracing to the extension's code. Currently, we use logging to the VSs output window, but this has limitations and should be used for conveying basic information. Additionally, the output window is visible to users of the extension, so it should not contain too many entries and details. The place where details should be logged is tracing. - Capability for dynamically enabling and disabling tracing - When tracing is disabled, it does not impact performance - Ability to filter entries additionally - File logging (possibility to easy add custom targets where entries are logged) - Ability to view state of the extension in real time using external tools   --------- Co-authored-by: Tomasz Gołębiowski <tgolebiowski@virtuslab.com>
- Loading branch information
1 parent
6e5a6a4
commit 860b7f4
Showing
10 changed files
with
382 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cody.Core.Trace | ||
{ | ||
public class FileTraceListener : TraceListener | ||
{ | ||
private StreamWriter writer; | ||
|
||
public FileTraceListener(string fileName) | ||
{ | ||
FileName = fileName; | ||
} | ||
|
||
public string FileName { get; } | ||
|
||
protected override void Initialize() | ||
{ | ||
var stream = File.Open(FileName, FileMode.Append, FileAccess.Write, FileShare.Read); | ||
writer = new StreamWriter(stream); | ||
writer.AutoFlush = true; | ||
} | ||
|
||
protected string FormatTraceEvent(TraceEvent traceEvent) | ||
{ | ||
var sb = new StringBuilder(); | ||
var eventName = string.IsNullOrEmpty(traceEvent.EventName) ? "<none>" : traceEvent.EventName; | ||
sb.AppendFormat("{0:yyyy-MM-dd HH:mm:ss.fff} [{1,2}] {2}.{3}: ", traceEvent.Timestamp, traceEvent.ThreadId, traceEvent.LoggerName, eventName); | ||
|
||
if (!string.IsNullOrEmpty(traceEvent.Message)) | ||
{ | ||
sb.AppendFormat(traceEvent.Message, traceEvent.MessageArgs); | ||
} | ||
|
||
if (traceEvent.Data != null) | ||
{ | ||
var output = JsonConvert.SerializeObject(traceEvent.Data); | ||
sb.Append(output); | ||
} | ||
|
||
if (traceEvent.Exception != null) | ||
{ | ||
sb.Append(traceEvent.Exception); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
protected override void Write(TraceEvent traceEvent) | ||
{ | ||
var formatedTraceEvent = FormatTraceEvent(traceEvent); | ||
writer.WriteLine(formatedTraceEvent); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cody.Core.Trace | ||
{ | ||
public class LogioTraceListener : TraceListener | ||
{ | ||
private TcpClient client; | ||
private NetworkStream stream; | ||
private HashSet<string> inputs = new HashSet<string>(); | ||
|
||
public LogioTraceListener(string hostname, int port) | ||
{ | ||
Hostname = hostname; | ||
Port = port; | ||
} | ||
|
||
public string Hostname { get; } | ||
public int Port { get; } | ||
|
||
protected override void Initialize() | ||
{ | ||
client = new TcpClient(); | ||
client.Connect(Hostname, Port); | ||
stream = client.GetStream(); | ||
} | ||
|
||
protected override void Write(TraceEvent traceEvent) | ||
{ | ||
var eventName = string.IsNullOrEmpty(traceEvent.EventName) ? "<none>" : traceEvent.EventName; | ||
var input = $"{traceEvent.LoggerName}|{eventName}"; | ||
if (!inputs.Contains(input)) | ||
{ | ||
var newInputMsg = $"+input|{input}\0"; | ||
var newInputBytes = Encoding.UTF8.GetBytes(newInputMsg); | ||
|
||
stream.Write(newInputBytes, 0, newInputBytes.Length); | ||
|
||
inputs.Add(input); | ||
} | ||
|
||
var sb = new StringBuilder(); | ||
sb.AppendFormat("[{1}]", traceEvent.ThreadId); | ||
if (!string.IsNullOrEmpty(traceEvent.Message)) | ||
{ | ||
sb.Append(" "); | ||
sb.AppendFormat(traceEvent.Message, traceEvent.MessageArgs); | ||
} | ||
|
||
if (traceEvent.Data != null) | ||
{ | ||
var output = JsonConvert.SerializeObject(traceEvent.Data); | ||
sb.Append(" "); | ||
sb.Append(output); | ||
} | ||
|
||
if (traceEvent.Exception != null) | ||
{ | ||
sb.Append(" "); | ||
sb.Append(traceEvent.Exception); | ||
} | ||
|
||
var msg = $"+msg|{input}|{sb}\0"; | ||
var msgBytes = Encoding.UTF8.GetBytes(msg); | ||
|
||
stream.Write(msgBytes, 0, msgBytes.Length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cody.Core.Trace | ||
{ | ||
public class TraceEvent | ||
{ | ||
public TraceEvent(string loggerName) | ||
{ | ||
Timestamp = DateTime.Now; | ||
ThreadId = Environment.CurrentManagedThreadId; | ||
LoggerName = loggerName; | ||
} | ||
|
||
public string LoggerName { get; } | ||
|
||
public DateTime Timestamp { get; } | ||
|
||
public int ThreadId { get; } | ||
|
||
public string EventName { get; set; } | ||
|
||
public string Message { get; set; } | ||
|
||
public object[] MessageArgs { get; set; } | ||
|
||
public object Data { get; set; } | ||
|
||
public Exception Exception { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cody.Core.Trace | ||
{ | ||
public abstract class TraceListener | ||
{ | ||
public bool Enabled { get; set; } = true; | ||
|
||
public Func<TraceEvent, bool> Filter { get; set; } | ||
|
||
private bool? successfullyInitialized; | ||
|
||
protected abstract void Initialize(); | ||
|
||
public void WriteTraceEvent(TraceEvent traceEvent) | ||
{ | ||
if (Enabled && traceEvent != null) | ||
{ | ||
if (Filter != null && !Filter(traceEvent)) return; | ||
|
||
if (successfullyInitialized == true) | ||
{ | ||
try | ||
{ | ||
Write(traceEvent); | ||
return; | ||
} | ||
catch { } | ||
} | ||
|
||
if (successfullyInitialized == null) | ||
{ | ||
try | ||
{ | ||
Initialize(); | ||
successfullyInitialized = true; | ||
Write(traceEvent); | ||
} | ||
catch | ||
{ | ||
successfullyInitialized = false; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
protected abstract void Write(TraceEvent traceEvent); | ||
} | ||
} |
Oops, something went wrong.