/
FileLogger.cs
37 lines (34 loc) · 955 Bytes
/
FileLogger.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
using Microsoft.Extensions.Logging;
using System;
using System.IO;
namespace learnCore
{
public class FileLogger : ILogger
{
private string filePath;
private object _lock = new object();
public FileLogger(string path)
{
filePath = path;
}
public IDisposable BeginScope<TState>(TState state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
//return logLevel == LogLevel.Trace;
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (formatter != null)
{
lock (_lock)
{
File.AppendAllText(filePath, formatter(state, exception) + Environment.NewLine);
}
}
}
}
}