Created
February 21, 2019 23:52
-
-
Save GeorgeTsiokos/0d2e38d7f9faa1d493d386aedc060c01 to your computer and use it in GitHub Desktop.
Simple AI file logger
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
public sealed class FileTelemetryChannel : ITelemetryChannel | |
{ | |
private readonly ISerializationWriter _serializationWriter; | |
private readonly StreamWriter _streamWriter; | |
public FileTelemetryChannel(string fileName = null) | |
{ | |
EndpointAddress = fileName; | |
_streamWriter = new StreamWriter( | |
fileName ?? $"{Path.GetTempPath()}ai-{DateTime.Now:yyyy-M-d}.jsonl", | |
true); | |
_serializationWriter = CreateSerializationWriter(_streamWriter); | |
} | |
public void Dispose() => _streamWriter.Dispose(); | |
public void Send(ITelemetry item) | |
{ | |
_serializationWriter.WriteStartObject(); | |
item.SerializeData(_serializationWriter); | |
_serializationWriter.WriteEndObject(); | |
_streamWriter.WriteLine(); | |
} | |
public void Flush() => _streamWriter.Flush(); | |
public bool? DeveloperMode { get; set; } | |
public string EndpointAddress { get; set; } | |
private static ISerializationWriter CreateSerializationWriter(TextWriter textWriter) | |
{ | |
var type = Type.GetType( | |
"Microsoft.ApplicationInsights.Extensibility.Implementation.JsonSerializationWriter, Microsoft.ApplicationInsights", | |
true, | |
false); | |
return (ISerializationWriter) Activator.CreateInstance(type, textWriter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment