Skip to content

Instantly share code, notes, and snippets.

@bruno-garcia
Last active April 14, 2020 01:06
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 bruno-garcia/5ae7486ab0e7ae63900c0773a9ab119c to your computer and use it in GitHub Desktop.
Save bruno-garcia/5ae7486ab0e7ae63900c0773a9ab119c to your computer and use it in GitHub Desktop.
using System;
using System.Globalization;
using System.IO;
using Sentry.Extensibility;
using Sentry.Protocol;
public class FileAppenderDiagnosticLogger : IDiagnosticLogger
{
private readonly SentryLevel _minimalLevel;
private readonly StreamWriter _writer;
public FileAppenderDiagnosticLogger(SentryLevel minimalLevel)
{
_minimalLevel = minimalLevel;
var fileName = Guid.NewGuid().ToString("n") + ".txt";
_writer = new StreamWriter(fileName);
}
public bool IsEnabled(SentryLevel level) => level >= _minimalLevel;
public void Log(SentryLevel logLevel, string message, Exception exception = null, params object[] args)
{
lock (_writer)
{
_writer.Write("{0} - {1}: ",DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ss.ffffZ", DateTimeFormatInfo.InvariantInfo), logLevel);
_writer.Write("");
_writer.Write(message, args);
if (exception is Exception)
{
_writer.Write("Exception: ");
_writer.Write(exception);
}
_writer.WriteLine();
_writer.Flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment