-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Enhance LoggerMessage attribute to support logging complex object and redaction #81730
Comments
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsBackground and motivation.NET uses source generation to provide high performance logging via LoggerMessage attribute. Source generation opens up a lot of flexibility to do more things in an efficient manner. We have a telemetry solution that is used by services across our orgs, as part of our solution we have expanded the LoggerMessage to support the following features
API ProposalIntroduce [AttributeUsage(AttributeTargets.Parameter)]
[Conditional("CODE_GENERATION_ATTRIBUTES")]
public sealed class LogPropertiesAttribute : Attribute
{
...
} API Usagepublic class Operation
{
public string Id {get; set;}
public string Name {get; set;}
}
public class ErrorDetails
{
public Operation Operation {get; set;}
public ErrorType Type {get; set;}
public string ErrorMessage {get; set;}
}
public static partial class Log
{
[LoggerMessage(
EventId = 0,
Level = LogLevel.Error,
Message = "Could not open socket")]
public static partial void CouldNotOpenSocket(
this ILogger logger, [LogProperties] error);
}
// log the error
var errorDetails = ...
logger.CouldNotOpenSocket(error); This will result in all parameters of the error details logged as This is a simplistic example. LogProperties can have parameters that extends its functionality with more options. The above can be augmented via some attributes to indicate sensitive data so the generated code takes care of redacting it appropriately. public static partial class Log
{
[LoggerMessage(
EventId = 0,
Level = LogLevel.Information,
Message = "Fetching profile failed for {user}")]
public static partial void ProfileFetchFailed(
this ILogger logger, IRedactorProvider redactorProvider, [XYZ] user); // XYZ here is a placeholder for the data classification attribute, I am intentionally omitting the real attributes
}
// Generated code will use redactorProvider to redact the `userId` according to the data class XYZ
// Usage is
logger.ProfileFetchFailed(redactorProvider, userId); Similarly the attribute XYZ can be added to the members of the complex object and the generated code should be able to know and redact it. Alternative DesignsNo response RisksNo response
|
Do we have more details about the behavior in general? This is a kind of serialization and needs to define the full scope of this serialization. For example, does it retrieve all properties which have getters either public or nonpublic inside the object? What do you expect to do when having types like collections (and which collections we should support)? What to expect if there is a cyclic reference? .... etc. |
Much prefer structured logging support of |
pinkfloydx33 Doing this in source generation provides the performance benefits as all of this expansion is done at the compile time as opposed the runtime. I am not saying that support for logging complex object shouldn't be added for the normal logging provider but with source generation we should utilize it for extracting the best performance out. |
@dpk83 per discussion I moved this issue to the future release. |
Background and motivation
.NET uses source generation to provide high performance logging via LoggerMessage attribute. Source generation opens up a lot of flexibility to do more things in an efficient manner. We have a telemetry solution that is used by services across our orgs, as part of our solution we have expanded the LoggerMessage to support the following features
logger.Log("Failed to fetch data due to error: {0}, {1}, {2}", errorDetails.operationId, errorDetails.Type, errorDetails.Message);
. Instead with the support of complex object logging in LoggerMessage attribute developer can log the object directly i.e.logger.DataFetchFailed(errorDetails)
. This will perform the expansion of the errorDetails object as part of the compile time source generation in an efficient way (i.e. no runtime cost)API Proposal
Introduce
LogPropertiesAttribute
which is used to annotate an object that needs to be expanded for logging.API Usage
This will result in all parameters of the error details logged as
error_Operation_Id
,error_Operation_Name
,error_Type
,error_ErrorMessage
.This is a simplistic example. LogProperties can have parameters that extends its functionality with more options.
The above can be augmented via some attributes to indicate sensitive data so the generated code takes care of redacting it appropriately.
Similarly the attribute XYZ can be added to the members of the complex object and the generated code should be able to know and redact it.
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: