-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement logging provider to dump logs to a stream. Make Obsidian.ConsoleApp dump logs to a FileStream. * Add more detailed exceptions to logs * Allow log files to be read while they are being written
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 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,74 @@ | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Obsidian.Hosting; | ||
public class StreamLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly StreamWriter _streamWriter; | ||
|
||
public StreamLoggerProvider(Stream stream) | ||
{ | ||
_streamWriter = new StreamWriter(stream); | ||
} | ||
|
||
public ILogger CreateLogger(string category) | ||
{ | ||
return new StreamLogger(category, _streamWriter); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_streamWriter.Dispose(); | ||
} | ||
} | ||
|
||
public class StreamLogger : ILogger | ||
{ | ||
private readonly string _category; | ||
private readonly StreamWriter _streamWriter; | ||
|
||
public StreamLogger(string category, StreamWriter streamWriter) | ||
{ | ||
_category = category; | ||
_streamWriter = streamWriter; | ||
} | ||
|
||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null; | ||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return true; | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
if (!IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
var msg = formatter(state, exception); | ||
|
||
if (string.IsNullOrEmpty(msg)) | ||
{ | ||
return; | ||
} | ||
|
||
var dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); | ||
|
||
if (exception != null) | ||
{ | ||
_streamWriter.WriteLine("[{0}] [{1}] ({2})\n--------------------\n{3}\n--------------------", dateTime, logLevel, _category, exception.ToString()); | ||
} | ||
else | ||
{ | ||
_streamWriter.WriteLine("[{0}] [{1}] ({2}) {3}", dateTime, logLevel, _category, msg); | ||
} | ||
|
||
_streamWriter.Flush(); | ||
} | ||
} |
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