forked from microsoft/garnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarkLoggerProvider.cs
99 lines (85 loc) · 3.4 KB
/
BenchmarkLoggerProvider.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
namespace Resp.benchmark
{
/// <summary>
/// ILogger implementation class for Benchmarks output
/// the Resp.benchmark app uses Console.Out
/// </summary>
public class BenchmarkLoggerProvider : ILoggerProvider
{
private readonly TextWriter textWriter;
static readonly string[] lvl = new string[]
{
"trce",
"dbug",
"info",
"warn",
"errr",
"crit",
};
public BenchmarkLoggerProvider(TextWriter textWriter)
{
this.textWriter = textWriter;
}
public ILogger CreateLogger(string categoryName) => new BenchmarkLogger(categoryName, textWriter);
public void Dispose()
{
textWriter.Dispose();
GC.SuppressFinalize(this);
}
private class BenchmarkLogger : ILogger
{
private readonly string categoryName;
private readonly TextWriter textWriter;
public BenchmarkLogger(string categoryName, TextWriter textWriter)
{
this.categoryName = categoryName;
this.textWriter = textWriter;
}
public IDisposable BeginScope<TState>(TState state) => default!;
public bool IsEnabled(LogLevel logLevel) => true;
private static string GetLevelStr(LogLevel ll) => lvl[(int)ll];
/// <summary>
/// Use this method for customization of the log
/// messages format.
/// </summary>
/// <typeparam name="TState"></typeparam>
/// <param name="logLevel"></param>
/// <param name="eventId"></param>
/// <param name="state"></param>
/// <param name="exception"></param>
/// <param name="formatter"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
string msg = logLevel switch
{
LogLevel.Information => string.Format("[{0:D3}.{1}.({2})] |{3}| {4}\n",
eventId.Id,
DateTime.Now.ToString("hh:mm:ss"),
GetLevelStr(logLevel),
categoryName,
state),
_ => string.Format("[{0:D3}.{1}.({2})] |{3}| <{4}> {5}\n",
eventId.Id,
DateTime.Now.ToString("hh:mm:ss"),
GetLevelStr(logLevel),
categoryName,
exception,
state),
};
textWriter.Write(msg);
}
}
}
}