-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathFileLogger.cs
37 lines (30 loc) · 1.13 KB
/
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.Collections.Generic;
using System.IO;
using System.Text;
namespace OneSTools.EventLog.Exporter.Core
{
public class FileLogger : ILogger
{
private static readonly object Locker = new object();
private readonly string _path;
private readonly string _categoryName;
public FileLogger(string path, string categoryName)
{
_path = path;
_categoryName = categoryName;
}
public IDisposable BeginScope<TState>(TState state)
=> null;
public bool IsEnabled(LogLevel logLevel)
=> true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var levelName = Enum.GetName(typeof(LogLevel), logLevel);
var message = $"{DateTime.Now:yyyy-MM-hh HH:mm:ss.fff} | {levelName} | {_categoryName}[{eventId.Id}]\n\t{ formatter(state, exception)}";
lock (Locker)
File.AppendAllText(_path, message + Environment.NewLine);
}
}
}