TaskLoggingHelper 類別

定義

協助程式記錄類別:包含工作所使用的所有記錄方法。Helper logging class - contains all the logging methods used by tasks. MSBuild 會將 TaskLoggingHelper 物件傳遞給每個工作。A TaskLoggingHelper object is passed to every task by MSBuild. 對於衍生自 Task 類別的工作,其會在 Log 屬性中提供。For tasks that derive from the Task class, it is provided in the Log property. 此類別是安全執行緒:工作可以從任何執行緒記錄。This class is thread safe: tasks can log from any threads.

public ref class TaskLoggingHelper : MarshalByRefObject
public ref class TaskLoggingHelper
public class TaskLoggingHelper : MarshalByRefObject
public class TaskLoggingHelper
type TaskLoggingHelper = class
    inherit MarshalByRefObject
type TaskLoggingHelper = class
Public Class TaskLoggingHelper
Inherits MarshalByRefObject
Public Class TaskLoggingHelper
繼承
TaskLoggingHelper
繼承
TaskLoggingHelper
衍生

範例

下列範例顯示建立一或多個目錄之工作的程式碼。The following example shows the code for a task that creates one or more directories.

using System;
using System.IO;
using System.Security;
using System.Collections;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Microsoft.Build.Tasks
{
    /*
     * Class: MakeDir
     *
     * An MSBuild task that creates one or more directories.
     *
     */
    public class MakeDir : Task
    {
        // The Required attribute indicates the following to MSBuild:
        //	     - if the parameter is a scalar type, and it is not supplied, fail the build immediately
        //	     - if the parameter is an array type, and it is not supplied, pass in an empty array
        // In this case the parameter is an array type, so if a project fails to pass in a value for the
            // Directories parameter, the task will get invoked, but this implementation will do nothing,
            // because the array will be empty.
        [Required]
            // Directories to create.
        public ITaskItem[] Directories
        {
            get
            {
                return directories;
            }

            set
            {
                directories = value;
            }
        }

        // The Output attribute indicates to MSBuild that the value of this property can be gathered after the
        // task has returned from Execute(), if the project has an <Output> tag under this task's element for
        // this property.
        [Output]
        // A project may need the subset of the inputs that were actually created, so make that available here.
        public ITaskItem[] DirectoriesCreated
        {
            get
            {
                return directoriesCreated;
            }
        }

        private ITaskItem[] directories;
        private ITaskItem[] directoriesCreated;

        /// <summary>
        /// Execute is part of the Microsoft.Build.Framework.ITask interface.
        /// When it's called, any input parameters have already been set on the task's properties.
        /// It returns true or false to indicate success or failure.
        /// </summary>
        public override bool Execute()
        {
            ArrayList items = new ArrayList();
            foreach (ITaskItem directory in Directories)
            {
                // ItemSpec holds the filename or path of an Item
                if (directory.ItemSpec.Length > 0)
                {
                    try
                    {
                        // Only log a message if we actually need to create the folder
                        if (!Directory.Exists(directory.ItemSpec))
                        {
                            Log.LogMessage(MessageImportance.Normal, "Creating directory " + directory.ItemSpec);
                            Directory.CreateDirectory(directory.ItemSpec);
                        }

                        // Add to the list of created directories
                        items.Add(directory);
                    }
                    // If a directory fails to get created, log an error, but proceed with the remaining
                    // directories.
                    catch (Exception ex)
                    {
                        if (ex is IOException
                            || ex is UnauthorizedAccessException
                            || ex is PathTooLongException
                            || ex is DirectoryNotFoundException
                            || ex is SecurityException)
                        {
                            Log.LogError("Error trying to create directory " + directory.ItemSpec + ". " + ex.Message);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }

            // Populate the "DirectoriesCreated" output items.
            directoriesCreated = (ITaskItem[])items.ToArray(typeof(ITaskItem));

            // Log.HasLoggedErrors is true if the task logged any errors -- even if they were logged
            // from a task's constructor or property setter. As long as this task is written to always log an error
            // when it fails, we can reliably return HasLoggedErrors.
            return !Log.HasLoggedErrors;
        }
    }
}

建構函式

TaskLoggingHelper(IBuildEngine, String)

公用建構函式,可協助工作 Factory 記錄訊息。Public constructor which can be used by task factories to assist them in logging messages.

TaskLoggingHelper(ITask)

公用建構函式public constructor

屬性

BuildEngine

用來取得建置引擎的捷徑屬性:我們會從工作執行個體擷取此屬性Shortcut property for getting our build engine - we retrieve it from the task instance

HasLoggedErrors

工作是否已透過此記錄協助程式物件記錄任何錯誤?Has the task logged any errors through this logging helper object?

HelpKeywordPrefix

取得或設定用於撰寫來自字串資源名稱之 Help 關鍵字的前置詞。Gets or sets the prefix used to compose help keywords from string resource names.

TaskName

取得父工作的名稱。Gets the name of the parent task.

TaskResources

用來載入特定於文化特性 (Culture) 的資源。Used to load culture-specific resources. 衍生類別應該在建構期間,或是透過這個屬性 (如果其有當地語系化字串的話) 註冊其資源。Derived classes should register their resources either during construction, or via this property, if they have localized strings.

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
ExtractMessageCode(String, String)

擷取已設定為指定訊息字串前置詞的訊息碼 (如果有的話)。Extracts the message code (if any) prefixed to the given message string. 訊息碼前置詞必須符合下列 .NET 規則運算式,才能加以辨識:^\s [A-Za-z]+\d+:\s 安全執行緒。Message code prefixes must match the following .NET regular expression in order to be recognized: ^\s [A-Za-z]+\d+:\s Thread safe.

FormatResourceString(String, Object[])

載入指定的資源字串,並選擇性地使用指定的引數來格式化字串。Loads the specified resource string and optionally formats it using the given arguments. 使用目前執行緒的文化特性進行格式化。The current thread's culture is used for formatting.

要求擁有者工作必須透過 Task (或 TaskMarshalByRef) 基底類別建構函式,或是 Task.TaskResources (或 AppDomainIsolatedTask.TaskResources) 屬性來註冊其資源。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.

安全執行緒。Thread safe.

FormatString(String, Object[])

使用傳入的變數引數來將指定的字串格式化。Formats the given string using the variable arguments passed in. 使用目前執行緒的文化特性進行格式化。The current thread's culture is used for formatting. 安全執行緒。Thread safe.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()
已過時。

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetResourceMessage(String)

取得來自工作程式庫中資源的訊息。Get the message from resource in task library. 安全執行緒。Thread safe.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

當啟動遠端物件時,會呼叫 InitializeLifetimeService。InitializeLifetimeService is called when the remote object is activated. 這個方法會決定物件存留期的長短。This method will determine how long the lifetime for the object will be. 安全執行緒。Thread safe. 不過,InitializeLifetimeService 和 MarkAsInactive 只能以該從順序一起呼叫,或根本不呼叫,並不能超過一次。However, InitializeLifetimeService and MarkAsInactive should only be called in that order, together or not at all, and no more than once.

InitializeLifetimeService()
已過時。

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
LogCommandLine(MessageImportance, String)

使用指定的重要性層級,記錄適用於工作之底層工具/可執行檔/殼層命令的命令列。Logs the command line for a task's underlying tool/executable/shell command, using the given importance level. 安全執行緒。Thread safe.

LogCommandLine(String)

記錄適用於工作之底層工具/可執行檔/殼層命令的命令列。Logs the command line for a task's underlying tool/executable/shell command. 安全執行緒。Thread safe.

LogCriticalMessage(String, String, String, String, Int32, Int32, Int32, Int32, String, Object[])

使用指定的字串和其他訊息詳細資料,以記錄重大訊息。Logs a critical message using the specified string and other message details. 安全執行緒。Thread safe.

LogError(String, Object[])

使用指定的字串來記錄錯誤。Logs an error using the specified string. 安全執行緒。Thread safe.

LogError(String, String, String, String, Int32, Int32, Int32, Int32, String, Object[])

使用指定的字串和其他錯誤詳細資料來記錄錯誤。Logs an error using the specified string and other error details. 安全執行緒。Thread safe.

LogErrorFromException(Exception)

使用來自指定例外狀況內容的訊息來記錄錯誤。Logs an error using the message from the given exception context. 系統將不會顯示任何呼叫堆疊。No callstack will be shown. 安全執行緒。Thread safe.

LogErrorFromException(Exception, Boolean)

使用來自指定例外狀況內容的訊息 (以及選擇性的堆疊追蹤) 來記錄錯誤。Logs an error using the message (and optionally the stack-trace) from the given exception context. 安全執行緒。Thread safe.

LogErrorFromException(Exception, Boolean, Boolean, String)

使用來自指定例外狀況的訊息及選擇性的堆疊追蹤,以及選擇性的內部例外狀況來記錄錯誤。Logs an error using the message, and optionally the stack-trace from the given exception, and optionally inner exceptions too. 安全執行緒。Thread safe.

LogErrorFromResources(String, Object[])

記錄使用指定的資源字串之錯誤。Logs an error using the specified resource string. 安全執行緒。Thread safe.

LogErrorFromResources(String, String, String, String, Int32, Int32, Int32, Int32, String, Object[])

使用指定的資源字串和其他錯誤詳細資料來記錄錯誤。Logs an error using the specified resource string and other error details. 安全執行緒。Thread safe.

LogErrorWithCodeFromResources(String, Object[])

記錄使用指定的資源字串之錯誤。Logs an error using the specified resource string. 如果訊息的前置詞為錯誤碼,即會擷取該錯誤碼並與訊息一起記錄。If the message has an error code prefixed to it, the code is extracted and logged with the message. 如果已提供 Help 關鍵字前置詞,則主機 IDE 的 Help 關鍵字也會與訊息一起記錄。If a help keyword prefix has been provided, a help keyword for the host IDE is also logged with the message. Help 關鍵字的組成方式是將字串資源名稱附加至前置詞。The help keyword is composed by appending the string resource name to the prefix.

工作可以透過 Task (或 TaskMarshalByRef) 基底類別建構函式,或是 Task.HelpKeywordPrefix (或 AppDomainIsolatedTask.HelpKeywordPrefix) 屬性,來提供 Help 關鍵字前置詞。A task can provide a help keyword prefix either via the Task (or TaskMarshalByRef) base class constructor, or the Task.HelpKeywordPrefix (or AppDomainIsolatedTask.HelpKeywordPrefix) property.

安全執行緒。Thread safe.

LogErrorWithCodeFromResources(String, String, Int32, Int32, Int32, Int32, String, Object[])

使用指定的資源字串和其他錯誤詳細資料來記錄錯誤。Logs an error using the specified resource string and other error details. 如果訊息的前置詞為錯誤碼,即會擷取該錯誤碼並與訊息一起記錄。If the message has an error code prefixed, the code is extracted and logged with the message. 如果已提供 Help 關鍵字前置詞,則主機 IDE 的 Help 關鍵字也會與訊息一起記錄。If a help keyword prefix has been provided, a help keyword for the host IDE is also logged with the message. Help 關鍵字的組成方式是將錯誤訊息資源字串名稱附加至前置詞。The help keyword is composed by appending the error message resource string name to the prefix.

工作可以透過 Task (或 TaskMarshalByRef) 基底類別建構函式,或是 Task.HelpKeywordPrefix (或 AppDomainIsolatedTask.HelpKeywordPrefix) 屬性,來提供 Help 關鍵字前置詞。A task can provide a help keyword prefix either via the Task (or TaskMarshalByRef) base class constructor, or the Task.HelpKeywordPrefix (or AppDomainIsolatedTask.HelpKeywordPrefix) property.

安全執行緒。Thread safe.

LogExternalProjectFinished(String, String, String, Boolean)

用來記錄自訂 ExternalProjectFinished 建置事件的小型協助程式。Small helper for logging the custom ExternalProjectFinished build event. 安全執行緒。Thread safe.

LogExternalProjectStarted(String, String, String, String)

用來記錄自訂 ExternalProjectStarted 建置事件安全執行緒的小型協助程式。Small helper for logging the custom ExternalProjectStarted build event Thread safe.

LogMessage(MessageImportance, String, Object[])

使用指定的字串來記錄指定重要性的訊息。Logs a message of the given importance using the specified string. 安全執行緒。Thread safe.

LogMessage(String, Object[])

使用指定的字串來記錄訊息。Logs a message using the specified string. 安全執行緒。Thread safe.

LogMessage(String, String, String, String, Int32, Int32, Int32, Int32, MessageImportance, String, Object[])

使用指定的字串和其他訊息詳細資料,以記錄訊息。Logs a message using the specified string and other message details. 安全執行緒。Thread safe.

LogMessageFromResources(MessageImportance, String, Object[])

使用指定的資源字串來記錄指定重要性的訊息。Logs a message of the given importance using the specified resource string. 安全執行緒。Thread safe.

LogMessageFromResources(String, Object[])

使用指定的資源字串來記錄訊息。Logs a message using the specified resource string. 安全執行緒。Thread safe.

LogMessageFromText(String, MessageImportance)

記錄來自指定文字行的錯誤/警告/訊息。Logs an error/warning/message from the given line of text. 只會針對符合特定 (標準) 格式的行記錄錯誤/警告,所有其他行都會被視為訊息。Errors/warnings are only logged for lines that fit a particular (canonical) format -- all other lines are treated as messages. 安全執行緒。Thread safe.

LogMessagesFromFile(String)

針對指定檔案中的每一行文字記錄錯誤/警告/訊息。Logs errors/warnings/messages for each line of text in the given file. 只會針對符合特定 (標準) 格式的行記錄錯誤/警告,其餘的行則會被視為訊息。Errors/warnings are only logged for lines that fit a particular (canonical) format -- the remaining lines are treated as messages. 安全執行緒。Thread safe.

LogMessagesFromFile(String, MessageImportance)

針對指定檔案中的每一行文字記錄錯誤/警告/訊息。Logs errors/warnings/messages for each line of text in the given file. 只會針對符合特定 (標準) 格式的行記錄錯誤/警告,其餘的行則會被視為訊息。Errors/warnings are only logged for lines that fit a particular (canonical) format -- the remaining lines are treated as messages. 安全執行緒。Thread safe.

LogMessagesFromStream(TextReader, MessageImportance)

針對指定資料流中的每一行文字記錄錯誤/警告/訊息。Logs errors/warnings/messages for each line of text in the given stream. 只會針對符合特定 (標準) 格式的行記錄錯誤/警告,其餘的行則會被視為訊息。Errors/warnings are only logged for lines that fit a particular (canonical) format -- the remaining lines are treated as messages. 安全執行緒。Thread safe.

LogTelemetry(String, IDictionary<String,String>)

記錄具有指定事件名稱和屬性的遙測。Logs telemetry with the specified event name and properties.

LogWarning(String, Object[])

使用指定的字串來記錄警告。Logs a warning using the specified string. 安全執行緒。Thread safe.

LogWarning(String, String, String, String, Int32, Int32, Int32, Int32, String, Object[])

使用指定的字串和其他警告詳細資料來記錄警告。Logs a warning using the specified string and other warning details. 安全執行緒。Thread safe.

LogWarningFromException(Exception)

使用來自指定例外狀況內容的訊息來記錄警告。Logs a warning using the message from the given exception context. 安全執行緒。Thread safe.

LogWarningFromException(Exception, Boolean)

使用來自指定例外狀況內容的訊息 (以及選擇性的堆疊追蹤) 來記錄警告。Logs a warning using the message (and optionally the stack-trace) from the given exception context. 安全執行緒。Thread safe.

LogWarningFromResources(String, Object[])

使用指定的資源字串記錄警告。Logs a warning using the specified resource string. 安全執行緒。Thread safe.

LogWarningFromResources(String, String, String, String, Int32, Int32, Int32, Int32, String, Object[])

使用指定的資源字串和其他警告詳細資來記錄警告。Logs a warning using the specified resource string and other warning details. 安全執行緒。Thread safe.

LogWarningWithCodeFromResources(String, Object[])

使用指定的資源字串記錄警告。Logs a warning using the specified resource string. 如果訊息的前置詞為警告碼,即會擷取該警告碼並與訊息一起記錄。If the message has a warning code prefixed to it, the code is extracted and logged with the message. 如果已提供 Help 關鍵字前置詞,則主機 IDE 的 Help 關鍵字也會與訊息一起記錄。If a help keyword prefix has been provided, a help keyword for the host IDE is also logged with the message. Help 關鍵字的組成方式是將字串資源名稱附加至前置詞。The help keyword is composed by appending the string resource name to the prefix.

工作可以透過 Task (或 TaskMarshalByRef) 基底類別建構函式,或是 Task.HelpKeywordPrefix (或 AppDomainIsolatedTask.HelpKeywordPrefix) 屬性,來提供 Help 關鍵字前置詞。A task can provide a help keyword prefix either via the Task (or TaskMarshalByRef) base class constructor, or the Task.HelpKeywordPrefix (or AppDomainIsolatedTask.HelpKeywordPrefix) property.

安全執行緒。Thread safe.

LogWarningWithCodeFromResources(String, String, Int32, Int32, Int32, Int32, String, Object[])

使用指定的資源字串和其他警告詳細資來記錄警告。Logs a warning using the specified resource string and other warning details. 如果訊息有警告碼,即會擷取該警告碼並與訊息一起記錄。If the message has a warning code, the code is extracted and logged with the message. 如果已提供 Help 關鍵字前置詞,則主機 IDE 的 Help 關鍵字也會與訊息一起記錄。If a help keyword prefix has been provided, a help keyword for the host IDE is also logged with the message. Help 關鍵字的組成方式是將警告訊息資源字串名稱附加至前置詞。The help keyword is composed by appending the warning message resource string name to the prefix.

工作可以透過 Task (或 TaskMarshalByRef) 基底類別建構函式,或是 Task.HelpKeywordPrefix (或 AppDomainIsolatedTask.HelpKeywordPrefix) 屬性,來提供 Help 關鍵字前置詞。A task can provide a help keyword prefix either via the Task (or TaskMarshalByRef) base class constructor, or the Task.HelpKeywordPrefix (or AppDomainIsolatedTask.HelpKeywordPrefix) property.

安全執行緒。Thread safe.

MarkAsInactive()

通知此物件其工作已完成。Notifies this object that its work is done. 安全執行緒。Thread safe. 不過,InitializeLifetimeService 和 MarkAsInactive 只能以該從順序一起呼叫,或根本不呼叫,並不能超過一次。However, InitializeLifetimeService and MarkAsInactive should only be called in that order, together or not at all, and no more than once.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於