-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyLogger.cs
44 lines (38 loc) · 1.1 KB
/
MyLogger.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
namespace aspnet_logging_05_cli
{
using System;
using System.IO;
using Microsoft.Extensions.Logging;
public class MyLogger : ILogger
{
private readonly string _categoryName;
private readonly string _path;
public MyLogger(string categoryName, string basePath)
{
_path = Path.Combine(basePath, "mylog.txt");
_categoryName = categoryName;
}
public void Log(LogLevel logLevel, int eventId, object state, Exception exception,
Func<object, Exception, string> formatter)
{
using (var writer = File.AppendText(_path))
{
writer.WriteLine($"{logLevel} :: {_categoryName} :: {formatter(state, exception)}");
}
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public IDisposable BeginScopeImpl(object state)
{
return new NoopDisposable();
}
private class NoopDisposable : IDisposable
{
public void Dispose()
{
}
}
}
}