Last active
April 14, 2020 01:06
-
-
Save bruno-garcia/5ae7486ab0e7ae63900c0773a9ab119c to your computer and use it in GitHub Desktop.
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
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