| File: LoggerMessageAttribute.cs | Web Access |
| Project: src\runtime\src\libraries\Microsoft.Extensions.Logging.Abstractions\src\Microsoft.Extensions.Logging.Abstractions.csproj (Microsoft.Extensions.Logging.Abstractions) |
// 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 have a <see cref="Microsoft.Extensions.Logging.LogLevel"/> as one of its parameters, if the attribute does not specify it.</para> /// <para> - Must have access to an <see cref="ILogger"/>: as a parameter (which is required when the method is <c>static</c>), or, for an instance method, through an <see cref="ILogger"/> field or primary constructor parameter on the containing type.</para> /// <para> - Must not have a name that starts with an underscore, or any parameter whose name starts with an underscore.</para> /// <para> - Can be generic, but its type parameters cannot use the <c>allows ref struct</c> constraint.</para> /// <para> - Must not have parameters that use the <c>params</c>, <c>scoped</c>, or <c>out</c> modifiers, or that are <c>ref struct</c> types.</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 /// that's 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 /// that's 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 /// that's 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 /// that's 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 /// that's 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 or sets 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 or sets the logging level for the logging method. /// </summary> public LogLevel Level { get; set; } = LogLevel.None; /// <summary> /// Gets or sets the message text for the logging method. /// </summary> public string Message { get; set; } = ""; /// <summary> /// Gets or sets the flag to skip IsEnabled check for the logging method. /// </summary> public bool SkipEnabledCheck { get; set; } } }