| File: Utilities\TaskLoggingHelperExtension.cs | Web Access |
| Project: Microsoft.Build.Framework.csproj (Microsoft.Build.Framework) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Globalization; using System.Resources; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; #nullable disable namespace Microsoft.Build.Tasks { /// <summary> /// Helper logging class for tasks, used for dealing with two resource streams. /// </summary> public class TaskLoggingHelperExtension : TaskLoggingHelper { /// <summary> /// Used to load culture-specific resources. Derived classes should register their resources either during construction, or /// via this property, if they have localized strings. /// </summary> public ResourceManager TaskSharedResources { get; set; } public TaskLoggingHelperExtension(ITask taskInstance, ResourceManager primaryResources, ResourceManager sharedResources, string helpKeywordPrefix) : base(taskInstance) { TaskResources = primaryResources; TaskSharedResources = sharedResources; HelpKeywordPrefix = helpKeywordPrefix; } /// <summary> /// Loads the specified resource string and optionally formats it using the given arguments. The current thread's culture /// is used for formatting. /// </summary> /// <remarks> /// 1) This method requires the owner task to have registered its resources either via the Task (or TaskMarshalByRef) base /// class constructor, or the "Task.TaskResources" (or "AppDomainIsolatedTask.TaskResources") property. /// 2) This method is thread-safe. /// </remarks> /// <param name="resourceName">The name of the string resource to load.</param> /// <param name="args">Optional arguments for formatting the loaded string.</param> /// <returns>The formatted string.</returns> /// <exception cref="ArgumentNullException">Thrown when <c>resourceName</c> is null.</exception> /// <exception cref="ArgumentException">Thrown when the string resource indicated by <c>resourceName</c> does not exist.</exception> /// <exception cref="InvalidOperationException">Thrown when the <c>TaskResources</c> property of the owner task is not set.</exception> public override string FormatResourceString(string resourceName, params object[] args) { ArgumentNullException.ThrowIfNull(resourceName); if (TaskResources is null) { throw new InvalidOperationException(SR.FormatTaskResourcesNotRegistered(TaskName)); } if (TaskSharedResources is null) { throw new InvalidOperationException(SR.FormatTaskResourcesNotRegistered(TaskName)); } // NOTE: the ResourceManager.GetString() method is thread-safe string resourceString = TaskResources.GetString(resourceName, CultureInfo.CurrentUICulture) ?? TaskSharedResources.GetString(resourceName, CultureInfo.CurrentUICulture); return resourceString is not null ? FormatString(resourceString, args) : throw new ArgumentException(SR.FormatTaskResourceNotFound(resourceName, TaskName)); } } }