| | 1 | | using System; |
| | 2 | | using Microsoft.Extensions.Logging; |
| | 3 | |
|
| | 4 | | namespace SharpHoundCommonLib |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// ILogger implementation that passes log entries through to the configured ILogger and prepends an identificat |
| | 8 | | /// string |
| | 9 | | /// </summary> |
| | 10 | | internal class PassThroughLogger : ILogger |
| | 11 | | { |
| | 12 | | private readonly string _name; |
| | 13 | |
|
| 15 | 14 | | public PassThroughLogger(string name) |
| 15 | 15 | | { |
| 15 | 16 | | _name = name; |
| 15 | 17 | | } |
| | 18 | |
|
| | 19 | | public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, |
| | 20 | | Func<TState, Exception, string> formatter) |
| 206 | 21 | | { |
| 206 | 22 | | var newLog = FormatLog(formatter(state, exception), exception); |
| 206 | 23 | | Logging.Logger.Log(logLevel, newLog); |
| 206 | 24 | | } |
| | 25 | |
|
| | 26 | | public bool IsEnabled(LogLevel logLevel) |
| 0 | 27 | | { |
| 0 | 28 | | return true; |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | public IDisposable BeginScope<TState>(TState state) |
| 0 | 32 | | { |
| 0 | 33 | | return default; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | private string FormatLog(string message, Exception e) |
| 206 | 37 | | { |
| 206 | 38 | | return $"[CommonLib {_name}]{message}{(e != null ? $"\n{e}" : "")}"; |
| 206 | 39 | | } |
| | 40 | | } |
| | 41 | | } |