forked from GoogleCloudPlatform/functions-framework-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoggerTypeNameHelper.cs
More file actions
81 lines (68 loc) · 3.1 KB
/
Copy pathLoggerTypeNameHelper.cs
File metadata and controls
81 lines (68 loc) · 3.1 KB
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
// Copyright 2020, Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Microsoft.Extensions.Logging;
using System;
using System.Reflection;
namespace OpenFunction.Testing
{
/// <summary>
/// Helper method to determine the category for an ILogger{T}, given the type T.
/// This isn't just the full name of the type, because generics are made prettier - but
/// the method used isn't easily accessible.
/// </summary>
internal static class LoggerTypeNameHelper
{
internal static string GetCategoryNameForType<T>()
{
var wrapper = new Logger<T>(NoOpLoggerFactory.Instance);
return ((NoOpLogger) wrapper.BeginScope("")).CategoryName;
}
internal static string GetCategoryNameForType(Type type)
{
// Annoyingly, we have to use reflection here...
var method = typeof(LoggerTypeNameHelper).GetMethod(
nameof(GetCategoryNameForType), 1, BindingFlags.NonPublic | BindingFlags.Static,
binder: null, Type.EmptyTypes, modifiers: null);
method = method!.MakeGenericMethod(type);
return (string?) method!.Invoke(null, null) ?? throw new InvalidOperationException("Method returned null");
}
private class NoOpLoggerFactory : ILoggerFactory
{
internal static ILoggerFactory Instance { get; } = new NoOpLoggerFactory();
public void AddProvider(ILoggerProvider provider) =>
throw new NotImplementedException();
public ILogger CreateLogger(string categoryName) =>
new NoOpLogger(categoryName);
public void Dispose() =>
throw new NotImplementedException();
}
/// <summary>
/// Logger which returns itself when BeginScope is called, allowing us
/// to get at the category. Yes, this is very weird.
/// </summary>
private class NoOpLogger : ILogger, IDisposable
{
internal string CategoryName { get; }
internal NoOpLogger(string categoryName) =>
CategoryName = categoryName;
public IDisposable BeginScope<TState>(TState state) => this;
public void Dispose() =>
throw new NotImplementedException();
public bool IsEnabled(LogLevel logLevel) =>
throw new NotImplementedException();
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) =>
throw new NotImplementedException();
}
}
}