Skip to content

Commit

Permalink
Merge pull request #39 from purview-dev/add-diagnostics-and-log-attribs
Browse files Browse the repository at this point in the history
Add diagnostics and log attribs
  • Loading branch information
kieronlanning authored May 30, 2024
2 parents 1162596 + e4b6b28 commit 09d1795
Show file tree
Hide file tree
Showing 1,546 changed files with 117,087 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SOLUTION_FILE = $(ROOT_FOLDER)Purview.Telemetry.SourceGenerator.sln
TEST_PROJECT = $(ROOT_FOLDER)Purview.Telemetry.SourceGenerator.sln
CONFIGURATION = Release

PACK_VERSION = 1.0.7
PACK_VERSION = 1.0.9
ARTIFACT_FOLDER = p:/sync-projects/.local-nuget/

# Targets
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace Purview.Telemetry.SourceGenerator.Logging;

partial class TelemetrySourceGeneratorLoggingTests
{
[Theory]
[MemberData(nameof(SpecificLogAttributeTypes))]
public async Task Generate_GivenInterfaceWithSpecificLogAttribute_GenerateLoggerWithThatLevel(string attribute)
{
// Arrange
string basicLogger = @$"
using Purview.Telemetry.Logging;
namespace Testing;
[Logger]
public interface ITestLogger
{{
[{attribute}]
void Log(string stringParam, int intParam, bool boolParam);
}}
";

// Act
var generationResult = await GenerateAsync(basicLogger);

// Assert
await TestHelpers.Verify(generationResult, c => c.UseHashedParameters(attribute));
}

[Theory]
[MemberData(nameof(SpecificLogAttributeTypes))]
public async Task Generate_GivenInterfaceWithSpecificTypesAndSpecificParameters_GenerateLoggerWithThatLevelAndParameter(string attribute)
{
// Arrange
string basicLogger = @$"
using Purview.Telemetry.Logging;
namespace Testing;
[Logger]
public interface ITestLogger
{{
[{attribute}]
void Log(string stringParam, int intParam, bool boolParam);
[{attribute}(eventId: 100)]
void Log_EventId_1(string stringParam, int intParam, bool boolParam);
[{attribute}(100)]
void Log_EventId_3(string stringParam, int intParam, bool boolParam);
[{attribute}(messageTemplate: ""template"")]
void Log_MessageTemplate_1(string stringParam, int intParam, bool boolParam);
[{attribute}(MessageTemplate = ""template"")]
void Log_MessageTemplate_2(string stringParam, int intParam, bool boolParam);
[{attribute}(""template"")]
void Log_MessageTemplate_3(string stringParam, int intParam, bool boolParam);
}}
";

// Act
var generationResult = await GenerateAsync(basicLogger);

// Assert
await TestHelpers.Verify(generationResult, c => c.UseHashedParameters(attribute));
}

public static TheoryData<string> SpecificLogAttributeTypes
{
get
{
TheoryData<string> data = [];

data.Add("Trace");
data.Add("Debug");
data.Add("Info");
data.Add("Warning");
data.Add("Error");
data.Add("Critical");

return data;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//HintName: CriticalAttribute.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Purview.Telemetry.SourceGenerator
// on {Scrubbed}.
//
// Changes to this file may cause incorrect behaviour and will be lost
// when the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable

namespace Purview.Telemetry.Logging;

/// <summary>
/// Marker attribute used as an alternative to <see cref="LogAttribute"/>, where the <see cref="LogAttribute.Level"/>
/// is set to <see cref="Microsoft.Extensions.Logging.LogLevel.Critical"/>.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
[System.Diagnostics.Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class CriticalAttribute : System.Attribute
{
/// <summary>
/// Creates a new instance of the <see cref="CriticalAttribute"/>, specifying the <see cref="MessageTemplate"/>.
/// </summary>
/// <param name="messageTemplate">Specifies the <see cref="MessageTemplate"/>.</param>
public CriticalAttribute(string messageTemplate)
{
MessageTemplate = messageTemplate;
}

/// <summary>
/// Creates a new instance of the <see cref="CriticalAttribute"/>, specifying the <see cref="EventId"/>.
/// </summary>
/// <param name="eventId">Specifies the <see cref="EventId"/>.</param>
public CriticalAttribute(int eventId)
{
EventId = eventId;
}

/// <summary>
/// Creates a new instance of the <see cref="CriticalAttribute"/>,
/// optionally the <see cref="MessageTemplate"/> and <see cref="Name"/>.
/// </summary>
/// <param name="messageTemplate">Optionally specifies the <see cref="MessageTemplate"/>.</param>
/// <param name="name">Optionally specifies the <see cref="Name"/>.</param>
public CriticalAttribute(string? messageTemplate = null, string? name = null)
{
MessageTemplate = messageTemplate;
Name = name;
}

/// <summary>
/// Creates a new instance of the <see cref="CriticalAttribute"/>, specifying the <see cref="EventId"/>
/// and the <see cref="Level"/>, optionally the <see cref="MessageTemplate"/> and <see cref="Name"/>.
/// </summary>
/// <param name="eventId">Specifies the <see cref="EventId"/>.</param>
/// <param name="messageTemplate">Optionally specifies the <see cref="MessageTemplate"/>.</param>
/// <param name="name">Optionally specifies the <see cref="Name"/>.</param>
public CriticalAttribute(int eventId, string? messageTemplate = null, string? name = null)
{
MessageTemplate = messageTemplate;
EventId = eventId;
Name = name;
}

/// <summary>
/// Optional. The message template used for the log entry, otherwise one is
/// generated based on the parameters.
/// </summary>
public string? MessageTemplate { get; set; }

/// <summary>
/// Optional. The event Id for this log entry. If one is not specified, one is automatically generated.
/// </summary>
public int? EventId { get; set; }

/// <summary>
/// Optional. Gets/ set the name of the log entry. If one is not specified, the method name is used.
/// </summary>
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//HintName: DebugAttribute.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Purview.Telemetry.SourceGenerator
// on {Scrubbed}.
//
// Changes to this file may cause incorrect behaviour and will be lost
// when the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable

namespace Purview.Telemetry.Logging;

/// <summary>
/// Marker attribute used as an alternative to <see cref="LogAttribute"/>, where the <see cref="LogAttribute.Level"/>
/// is set to <see cref="Microsoft.Extensions.Logging.LogLevel.Debug"/>.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
[System.Diagnostics.Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class DebugAttribute : System.Attribute
{
/// <summary>
/// Creates a new instance of the <see cref="DebugAttribute"/>, specifying the <see cref="MessageTemplate"/>.
/// </summary>
/// <param name="messageTemplate">Specifies the <see cref="MessageTemplate"/>.</param>
public DebugAttribute(string messageTemplate)
{
MessageTemplate = messageTemplate;
}

/// <summary>
/// Creates a new instance of the <see cref="DebugAttribute"/>, specifying the <see cref="EventId"/>.
/// </summary>
/// <param name="eventId">Specifies the <see cref="EventId"/>.</param>
public DebugAttribute(int eventId)
{
EventId = eventId;
}

/// <summary>
/// Creates a new instance of the <see cref="DebugAttribute"/>,
/// optionally the <see cref="MessageTemplate"/> and <see cref="Name"/>.
/// </summary>
/// <param name="messageTemplate">Optionally specifies the <see cref="MessageTemplate"/>.</param>
/// <param name="name">Optionally specifies the <see cref="Name"/>.</param>
public DebugAttribute(string? messageTemplate = null, string? name = null)
{
MessageTemplate = messageTemplate;
Name = name;
}

/// <summary>
/// Creates a new instance of the <see cref="DebugAttribute"/>, specifying the <see cref="EventId"/>
/// and the <see cref="Level"/>, optionally the <see cref="MessageTemplate"/> and <see cref="Name"/>.
/// </summary>
/// <param name="eventId">Specifies the <see cref="EventId"/>.</param>
/// <param name="messageTemplate">Optionally specifies the <see cref="MessageTemplate"/>.</param>
/// <param name="name">Optionally specifies the <see cref="Name"/>.</param>
public DebugAttribute(int eventId, string? messageTemplate = null, string? name = null)
{
MessageTemplate = messageTemplate;
EventId = eventId;
Name = name;
}

/// <summary>
/// Optional. The message template used for the log entry, otherwise one is
/// generated based on the parameters.
/// </summary>
public string? MessageTemplate { get; set; }

/// <summary>
/// Optional. The event Id for this log entry. If one is not specified, one is automatically generated.
/// </summary>
public int? EventId { get; set; }

/// <summary>
/// Optional. Gets/ set the name of the log entry. If one is not specified, the method name is used.
/// </summary>
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//HintName: ErrorAttribute.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the Purview.Telemetry.SourceGenerator
// on {Scrubbed}.
//
// Changes to this file may cause incorrect behaviour and will be lost
// when the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable

namespace Purview.Telemetry.Logging;

/// <summary>
/// Marker attribute used as an alternative to <see cref="LogAttribute"/>, where the <see cref="LogAttribute.Level"/>
/// is set to <see cref="Microsoft.Extensions.Logging.LogLevel.Error"/>.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
[System.Diagnostics.Conditional("PURVIEW_TELEMETRY_ATTRIBUTES")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1019:Define accessors for attribute arguments")]
sealed class ErrorAttribute : System.Attribute
{
/// <summary>
/// Creates a new instance of the <see cref="ErrorAttribute"/>, specifying the <see cref="MessageTemplate"/>.
/// </summary>
/// <param name="messageTemplate">Specifies the <see cref="MessageTemplate"/>.</param>
public ErrorAttribute(string messageTemplate)
{
MessageTemplate = messageTemplate;
}

/// <summary>
/// Creates a new instance of the <see cref="ErrorAttribute"/>, specifying the <see cref="EventId"/>.
/// </summary>
/// <param name="eventId">Specifies the <see cref="EventId"/>.</param>
public ErrorAttribute(int eventId)
{
EventId = eventId;
}

/// <summary>
/// Creates a new instance of the <see cref="ErrorAttribute"/>,
/// optionally the <see cref="MessageTemplate"/> and <see cref="Name"/>.
/// </summary>
/// <param name="messageTemplate">Optionally specifies the <see cref="MessageTemplate"/>.</param>
/// <param name="name">Optionally specifies the <see cref="Name"/>.</param>
public ErrorAttribute(string? messageTemplate = null, string? name = null)
{
MessageTemplate = messageTemplate;
Name = name;
}

/// <summary>
/// Creates a new instance of the <see cref="ErrorAttribute"/>, specifying the <see cref="EventId"/>
/// and the <see cref="Level"/>, optionally the <see cref="MessageTemplate"/> and <see cref="Name"/>.
/// </summary>
/// <param name="eventId">Specifies the <see cref="EventId"/>.</param>
/// <param name="messageTemplate">Optionally specifies the <see cref="MessageTemplate"/>.</param>
/// <param name="name">Optionally specifies the <see cref="Name"/>.</param>
public ErrorAttribute(int eventId, string? messageTemplate = null, string? name = null)
{
MessageTemplate = messageTemplate;
EventId = eventId;
Name = name;
}

/// <summary>
/// Optional. The message template used for the log entry, otherwise one is
/// generated based on the parameters.
/// </summary>
public string? MessageTemplate { get; set; }

/// <summary>
/// Optional. The event Id for this log entry. If one is not specified, one is automatically generated.
/// </summary>
public int? EventId { get; set; }

/// <summary>
/// Optional. Gets/ set the name of the log entry. If one is not specified, the method name is used.
/// </summary>
public string? Name { get; set; }
}
Loading

0 comments on commit 09d1795

Please sign in to comment.