-
Notifications
You must be signed in to change notification settings - Fork 6
/
SimpleConsoleLogger.cs
44 lines (34 loc) · 1.14 KB
/
SimpleConsoleLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Licensed under the Apache License, Version 2.0 (the "License").
// See the LICENSE file in the project root for more information.
using System;
using System.Reactive.Disposables;
using Microsoft.Extensions.Logging;
namespace HIDDevices.Sample;
public class SimpleConsoleLogger<T> : ILogger<T>
{
public readonly string Name;
public SimpleConsoleLogger(LogLevel logLevel, string? name = null)
{
Name = name ?? typeof(T).Name;
LogLevel = logLevel;
}
public LogLevel LogLevel { get; set; }
/// <inheritdoc />
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
var message = formatter(state, exception);
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine(message);
}
}
/// <inheritdoc />
public bool IsEnabled(LogLevel logLevel) => LogLevel <= logLevel;
/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state) where TState : notnull => Disposable.Empty;
}