/
LoggerMessageAttribute.cs
114 lines (103 loc) · 4.3 KB
/
LoggerMessageAttribute.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Microsoft.Extensions.Logging
{
/// <summary>
/// Provides information to guide the production of a strongly-typed logging method.
/// </summary>
/// <remarks>
/// <para>The method this attribute is applied to:</para>
/// <para> - Must be a partial method.</para>
/// <para> - Must return <c>void</c>.</para>
/// <para> - Must not be generic.</para>
/// <para> - Must have an <see cref="ILogger"/> as one of its parameters.</para>
/// <para> - Must have a <see cref="Microsoft.Extensions.Logging.LogLevel"/> as one of its parameters.</para>
/// <para> - None of the parameters can be generic.</para>
/// </remarks>
/// <example>
/// <format type="text/markdown"><![CDATA[
/// ```csharp
/// static partial class Log
/// {
/// [LoggerMessage(EventId = 0, Message = "Could not open socket for {hostName}")]
/// static partial void CouldNotOpenSocket(ILogger logger, LogLevel level, string hostName);
/// }
/// ```
/// ]]></format>
/// </example>
[AttributeUsage(AttributeTargets.Method)]
public sealed class LoggerMessageAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="LoggerMessageAttribute"/> class
/// which is used to guide the production of a strongly-typed logging method.
/// </summary>
public LoggerMessageAttribute() { }
/// <summary>
/// Initializes a new instance of the <see cref="LoggerMessageAttribute"/> class
/// which is used to guide the production of a strongly-typed logging method.
/// </summary>
/// <param name="eventId">The log event Id.</param>
/// <param name="level">The log level.</param>
/// <param name="message">Format string of the log message.</param>
public LoggerMessageAttribute(int eventId, LogLevel level, string message)
{
EventId = eventId;
Level = level;
Message = message;
}
/// <summary>
/// Initializes a new instance of the <see cref="LoggerMessageAttribute"/> class
/// which is used to guide the production of a strongly-typed logging method.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">Format string of the log message.</param>
public LoggerMessageAttribute(LogLevel level, string message)
{
Level = level;
Message = message;
}
/// <summary>
/// Initializes a new instance of the <see cref="LoggerMessageAttribute"/> class
/// which is used to guide the production of a strongly-typed logging method.
/// </summary>
/// <param name="level">The log level.</param>
public LoggerMessageAttribute(LogLevel level)
{
Level = level;
}
/// <summary>
/// Initializes a new instance of the <see cref="LoggerMessageAttribute"/> class
/// which is used to guide the production of a strongly-typed logging method.
/// </summary>
/// <param name="message">Format string of the log message.</param>
public LoggerMessageAttribute(string message)
{
Message = message;
}
/// <summary>
/// Gets the logging event id for the logging method.
/// </summary>
public int EventId { get; set; } = -1;
/// <summary>
/// Gets or sets the logging event name for the logging method.
/// </summary>
/// <remarks>
/// This will equal the method name if not specified.
/// </remarks>
public string? EventName { get; set; }
/// <summary>
/// Gets the logging level for the logging method.
/// </summary>
public LogLevel Level { get; set; } = LogLevel.None;
/// <summary>
/// Gets the message text for the logging method.
/// </summary>
public string Message { get; set; } = "";
/// <summary>
/// Gets the flag to skip IsEnabled check for the logging method.
/// </summary>
public bool SkipEnabledCheck { get; set; }
}
}