Reference documentation and code samples for the Google.Cloud.Diagnostics.Common class ILoggerExtensions.
Extensions for ILogger to allow augmenting the logged information with contextual data.
GoogleLogger" will add the information to the LogEntry that is sent to
Google Cloud Logging.
For the consumption of other loggers, the information will be included in newly created scopes
at the moment of logging, in a format that all loggers should understand.
public static ILogger WithAddedLabels(this ILogger logger, IEnumerable<KeyValuePair<string, string>> labels)
Adds labels to the returned logger that may be included with every subsequent log.
How this information is included will depend on the actual logger.
GoogleLogger" will include this information in Labels.
For the consumption of other loggers, the information will be included in newly created scopes
at the moment of logging, in a format that all loggers should understand.
If logger had already been augmented with labels, labels
will be added to the existing ones on the returned ILogger
public static ILogger WithAddedLabels(this ILogger logger, params KeyValuePair<string, string>[] labels)
Adds labels to the returned logger that may be included with every subsequent log.
How this information is included will depend on the actual logger.
GoogleLogger" will include this information in Labels.
For the consumption of other loggers, the information will be included in newly created scopes
at the moment of logging, in a format that all loggers should understand.
If logger had already been augmented with labels, labels
will be added to the existing ones on the returned ILogger
public static ILogger WithLabels(this ILogger logger, IEnumerable<KeyValuePair<string, string>> labels)
Adds labels to the returned logger that may be included with every subsequent log.
How this information is included will depend on the actual logger.
GoogleLogger" will include this information in Labels.
For the consumption of other loggers, the information will be included in newly created scopes
at the moment of logging, in a format that all loggers should understand.
If logger had already been augmented with labels, the old labels will be replaced
by the new ones on the returned ILogger.
public static ILogger WithLabels(this ILogger logger, params KeyValuePair<string, string>[] labels)
Adds labels to the returned logger that may be included with every subsequent log.
How this information is included will depend on the actual logger.
GoogleLogger" will include this information in Labels.
For the consumption of other loggers, the information will be included in newly created scopes
at the moment of logging, in a format that all loggers should understand.
If logger had already been augmented with labels, the old labels will be replaced
by the new ones on the returned ILogger.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eILoggerExtensions\u003c/code\u003e class provides extension methods for \u003ccode\u003eILogger\u003c/code\u003e to enhance log data with contextual information.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eGoogleLogger\u003c/code\u003e will include contextual data in \u003ccode\u003eLogEntry\u003c/code\u003e labels, and other loggers will receive the information in newly created scopes.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eWithAddedLabels\u003c/code\u003e methods append new key-value pair labels to existing ones in the \u003ccode\u003eILogger\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eWithLabels\u003c/code\u003e methods replace existing labels in the \u003ccode\u003eILogger\u003c/code\u003e with new key-value pair labels.\u003c/p\u003e\n"],["\u003cp\u003eThe latest version of \u003ccode\u003eILoggerExtensions\u003c/code\u003e is 5.2.0.\u003c/p\u003e\n"]]],[],null,["# Google.Cloud.Diagnostics.Common - Class ILoggerExtensions (5.3.0)\n\nVersion latestkeyboard_arrow_down\n\n- [5.3.0 (latest)](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest/Google.Cloud.Diagnostics.Common.ILoggerExtensions)\n- [5.2.0](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/5.2.0/Google.Cloud.Diagnostics.Common.ILoggerExtensions)\n- [5.1.0](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/5.1.0/Google.Cloud.Diagnostics.Common.ILoggerExtensions)\n- [5.0.0](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/5.0.0/Google.Cloud.Diagnostics.Common.ILoggerExtensions)\n- [4.4.0](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/4.4.0/Google.Cloud.Diagnostics.Common.ILoggerExtensions)\n- [4.3.1](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/4.3.1/Google.Cloud.Diagnostics.Common.ILoggerExtensions) \n\n public static class ILoggerExtensions\n\nReference documentation and code samples for the Google.Cloud.Diagnostics.Common class ILoggerExtensions.\n\nExtensions for [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger) to allow augmenting the logged information with contextual data.\n[GoogleLogger](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest/Google.Cloud.Diagnostics.Common.GoogleLogger)\" will add the information to the [LogEntry](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Logging.V2/latest/Google.Cloud.Logging.V2.LogEntry.html) that is sent to\nGoogle Cloud Logging.\nFor the consumption of other loggers, the information will be included in newly created scopes\nat the moment of logging, in a format that all loggers should understand. \n\nInheritance\n-----------\n\n[object](https://learn.microsoft.com/dotnet/api/system.object) \\\u003e ILoggerExtensions \n\nInherited Members\n-----------------\n\n[object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) \n[object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) \n[object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone) \n[object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)\n\nNamespace\n---------\n\n[Google.Cloud.Diagnostics.Common](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest/Google.Cloud.Diagnostics.Common)\n\nAssembly\n--------\n\nGoogle.Cloud.Diagnostics.Common.dll\n\nMethods\n-------\n\n### WithAddedLabels(ILogger, IEnumerable\\\u003cKeyValuePair\\\u003cstring, string\\\u003e\\\u003e)\n\n public static ILogger WithAddedLabels(this ILogger logger, IEnumerable\u003cKeyValuePair\u003cstring, string\u003e\u003e labels)\n\n\nAdds labels to the returned logger that may be included with every subsequent log.\nHow this information is included will depend on the actual `logger`.\n[GoogleLogger](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest/Google.Cloud.Diagnostics.Common.GoogleLogger)\" will include this information in [Labels](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Logging.V2/latest/Google.Cloud.Logging.V2.LogEntry.html#Google_Cloud_Logging_V2_LogEntry_Labels).\nFor the consumption of other loggers, the information will be included in newly created scopes\nat the moment of logging, in a format that all loggers should understand.\n\n\nIf `logger` had already been augmented with labels, `labels`\nwill be added to the existing ones on the returned [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger)\n\n### WithAddedLabels(ILogger, params KeyValuePair\\\u003cstring, string\\\u003e\\[\\])\n\n public static ILogger WithAddedLabels(this ILogger logger, params KeyValuePair\u003cstring, string\u003e[] labels)\n\n\nAdds labels to the returned logger that may be included with every subsequent log.\nHow this information is included will depend on the actual `logger`.\n[GoogleLogger](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest/Google.Cloud.Diagnostics.Common.GoogleLogger)\" will include this information in [Labels](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Logging.V2/latest/Google.Cloud.Logging.V2.LogEntry.html#Google_Cloud_Logging_V2_LogEntry_Labels).\nFor the consumption of other loggers, the information will be included in newly created scopes\nat the moment of logging, in a format that all loggers should understand.\n\n\nIf `logger` had already been augmented with labels, `labels`\nwill be added to the existing ones on the returned [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger)\n\n### WithLabels(ILogger, IEnumerable\\\u003cKeyValuePair\\\u003cstring, string\\\u003e\\\u003e)\n\n public static ILogger WithLabels(this ILogger logger, IEnumerable\u003cKeyValuePair\u003cstring, string\u003e\u003e labels)\n\n\nAdds labels to the returned logger that may be included with every subsequent log.\nHow this information is included will depend on the actual `logger`.\n[GoogleLogger](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest/Google.Cloud.Diagnostics.Common.GoogleLogger)\" will include this information in [Labels](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Logging.V2/latest/Google.Cloud.Logging.V2.LogEntry.html#Google_Cloud_Logging_V2_LogEntry_Labels).\nFor the consumption of other loggers, the information will be included in newly created scopes\nat the moment of logging, in a format that all loggers should understand.\n\n\nIf `logger` had already been augmented with labels, the old labels will be replaced\nby the new ones on the returned [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger).\n\n### WithLabels(ILogger, params KeyValuePair\\\u003cstring, string\\\u003e\\[\\])\n\n public static ILogger WithLabels(this ILogger logger, params KeyValuePair\u003cstring, string\u003e[] labels)\n\n\nAdds labels to the returned logger that may be included with every subsequent log.\nHow this information is included will depend on the actual `logger`.\n[GoogleLogger](/dotnet/docs/reference/Google.Cloud.Diagnostics.Common/latest/Google.Cloud.Diagnostics.Common.GoogleLogger)\" will include this information in [Labels](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Logging.V2/latest/Google.Cloud.Logging.V2.LogEntry.html#Google_Cloud_Logging_V2_LogEntry_Labels).\nFor the consumption of other loggers, the information will be included in newly created scopes\nat the moment of logging, in a format that all loggers should understand.\n\n\nIf `logger` had already been augmented with labels, the old labels will be replaced\nby the new ones on the returned [ILogger](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.ilogger)."]]