-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
IDiagnosticsLogger.cs
181 lines (159 loc) · 8.11 KB
/
IDiagnosticsLogger.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
#nullable enable
namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
/// <para>
/// Combines <see cref="ILogger" /> and <see cref="DiagnosticSource" />
/// for use by all EF Core logging so that events can be sent to both <see cref="ILogger" />
/// for ASP.NET and <see cref="DiagnosticSource" /> for everything else.
/// </para>
/// <para>
/// The service lifetime is <see cref="ServiceLifetime.Singleton" />. This means a single instance
/// is used by many <see cref="DbContext" /> instances. The implementation must be thread-safe.
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped" />.
/// </para>
/// </summary>
public interface IDiagnosticsLogger
{
/// <summary>
/// Entity Framework logging options.
/// </summary>
ILoggingOptions Options { get; }
/// <summary>
/// Caching for logging definitions.
/// </summary>
LoggingDefinitions Definitions { get; }
/// <summary>
/// Gets a value indicating whether sensitive information should be written
/// to the underlying logger. This also has the side effect of writing a warning
/// to the log the first time sensitive data is logged.
/// </summary>
bool ShouldLogSensitiveData();
/// <summary>
/// The underlying <see cref="ILogger" />.
/// </summary>
ILogger Logger { get; }
/// <summary>
/// The <see cref="DiagnosticSource" />.
/// </summary>
DiagnosticSource DiagnosticSource { get; }
/// <summary>
/// The <see cref="IDbContextLogger" />.
/// </summary>
IDbContextLogger DbContextLogger { get; }
/// <summary>
/// Holds registered interceptors, if any.
/// </summary>
IInterceptors? Interceptors { get; }
/// <summary>
/// Checks whether or not the message should be sent to the <see cref="ILogger" />.
/// </summary>
/// <param name="definition"> The definition of the event to log. </param>
/// <returns>
/// <see langword="true" /> if <see cref="ILogger" /> logging is enabled and the event should not be ignored;
/// <see langword="false" /> otherwise.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // Because hot path for logging
bool ShouldLog([NotNull] EventDefinitionBase definition)
// No null checks; low-level code in hot path for logging.
=> definition.WarningBehavior == WarningBehavior.Throw
|| (Logger.IsEnabled(definition.Level)
&& definition.WarningBehavior != WarningBehavior.Ignore);
/// <summary>
/// Dispatches the given <see cref="EventData" /> to a <see cref="DiagnosticSource" />, if enabled, and
/// a <see cref="IDbContextLogger" />, if enabled.
/// </summary>
/// <param name="definition"> The definition of the event to log. </param>
/// <param name="eventData"> The event data. </param>
/// <param name="diagnosticSourceEnabled"> True to dispatch to a <see cref="DiagnosticSource" />; <see langword="false" /> otherwise. </param>
/// <param name="simpleLogEnabled"> True to dispatch to a <see cref="IDbContextLogger" />; <see langword="false" /> otherwise. </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // Because hot path for logging
void DispatchEventData(
[NotNull] EventDefinitionBase definition,
[NotNull] EventData eventData,
bool diagnosticSourceEnabled,
bool simpleLogEnabled)
{
// No null checks; low-level code in hot path for logging.
if (diagnosticSourceEnabled)
{
DiagnosticSource.Write(definition.EventId.Name!, eventData);
}
if (simpleLogEnabled)
{
DbContextLogger.Log(eventData);
}
}
/// <summary>
/// Determines whether or not an <see cref="EventData" /> instance is needed based on whether or
/// not there is a <see cref="DiagnosticSource" /> or an <see cref="IDbContextLogger" /> enabled for
/// the given event.
/// </summary>
/// <param name="definition"> The definition of the event. </param>
/// <param name="diagnosticSourceEnabled">
/// Set to <see langword="true" /> if a <see cref="DiagnosticSource" /> is enabled;
/// <see langword="false" /> otherwise.
/// </param>
/// <param name="simpleLogEnabled">
/// True to <see langword="true" /> if a <see cref="IDbContextLogger" /> is enabled; <see langword="false" />
/// otherwise.
/// </param>
/// <returns> <see langword="true" /> if either a diagnostic source or a LogTo logger is enabled; <see langword="false" /> otherwise. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // Because hot path for logging
bool NeedsEventData(
[NotNull] EventDefinitionBase definition,
out bool diagnosticSourceEnabled,
out bool simpleLogEnabled)
{
// No null checks; low-level code in hot path for logging.
diagnosticSourceEnabled = DiagnosticSource.IsEnabled(definition.EventId.Name!);
simpleLogEnabled = definition.WarningBehavior == WarningBehavior.Log
&& DbContextLogger.ShouldLog(definition.EventId, definition.Level);
return diagnosticSourceEnabled
|| simpleLogEnabled;
}
/// <summary>
/// Determines whether or not an <see cref="EventData" /> instance is needed based on whether or
/// not there is a <see cref="DiagnosticSource" />, an <see cref="IDbContextLogger" />, or an <see cref="IInterceptor" /> enabled for
/// the given event.
/// </summary>
/// <param name="definition"> The definition of the event. </param>
/// <param name="interceptor"> The <see cref="IInterceptor" /> to use if enabled; otherwise null. </param>
/// <param name="diagnosticSourceEnabled">
/// Set to <see langword="true" /> if a <see cref="DiagnosticSource" /> is enabled;
/// <see langword="false" /> otherwise.
/// </param>
/// <param name="simpleLogEnabled">
/// True to <see langword="true" /> if a <see cref="IDbContextLogger" /> is enabled; <see langword="false" />
/// otherwise.
/// </param>
/// <returns>
/// <see langword="true" /> if either a diagnostic source, a LogTo logger, or an interceptor is enabled; <see langword="false" />
/// otherwise.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // Because hot path for logging
bool NeedsEventData<TInterceptor>(
[NotNull] EventDefinitionBase definition,
[CanBeNull] out TInterceptor? interceptor,
out bool diagnosticSourceEnabled,
out bool simpleLogEnabled)
where TInterceptor : class, IInterceptor
{
// No null checks; low-level code in hot path for logging.
diagnosticSourceEnabled = DiagnosticSource.IsEnabled(definition.EventId.Name!);
interceptor = Interceptors?.Aggregate<TInterceptor>();
simpleLogEnabled = definition.WarningBehavior == WarningBehavior.Log
&& DbContextLogger.ShouldLog(definition.EventId, definition.Level);
return diagnosticSourceEnabled
|| simpleLogEnabled
|| interceptor != null;
}
}
}