Skip to content

Instantly share code, notes, and snippets.

@GeorgeTsiokos
Created February 21, 2019 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgeTsiokos/0d2e38d7f9faa1d493d386aedc060c01 to your computer and use it in GitHub Desktop.
Save GeorgeTsiokos/0d2e38d7f9faa1d493d386aedc060c01 to your computer and use it in GitHub Desktop.
Simple AI file logger
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