監視及回應事件Monitor and Respond to Events

本主題適用於:是SQL Server是Azure SQL Database (僅限受控執行個體)否Azure SQL 資料倉儲 否平行處理資料倉儲 THIS TOPIC APPLIES TO: yesSQL ServeryesAzure SQL Database (Managed Instance only)noAzure SQL Data Warehouse noParallel Data Warehouse

重要

Azure SQL Database 受控執行個體目前支援多數 (但非全部) 的 SQL Server Agent 功能。On Azure SQL Database Managed Instance, most, but not all SQL Server Agent features are currently supported. 如需詳細資料,請參閱 Azure SQL Database 受控執行個體與 SQL Server 之間的 T-SQL 差異See Azure SQL Database Managed Instance T-SQL differences from SQL Server for details.

SQL ServerSQL ServerAgent 會監視及自動回應「事件」(Event),例如:來自 SQL ServerSQL Server 的訊息、特定效能狀況與 Windows Management Instrumentation (WMI) 事件。 Agent can monitor and automatically respond to events, such as messages from SQL ServerSQL Server, specific performance conditions, and Windows Management Instrumentation (WMI) events.

本節內容In This Section

警示Alerts
包含如何命名警示和選取警示所回應之事件或效能狀況的相關資訊。Contains information about naming an alert and selecting the events or performance conditions to which alerts respond.

建立使用者定義的事件Create a User-Defined Event
包含有關於如何建立 SQL ServerSQL Server預先定義之事件以外的事件的資訊。Contains information about how to create events other than those that are predefined by SQL ServerSQL Server.

運算子Operators
包含當作業失敗或成功時,如何建立 SQL ServerSQL Server Agent 可用來傳送通知的管理員別名。Contains information about creating aliases for administrators that SQL ServerSQL Server Agent can use to send notifications when jobs fail or succeed.

關於監視及回應事件About Monitoring and Responding to Events

對於事件的自動回應稱之為「警示」。Automated responses to events are called alerts. 您可以在一或多個事件上定義警示,以指定 SQL ServerSQL Server Agent 回應事件的方式。You can define an alert on one or more events to specify how you want SQL ServerSQL Server Agent to respond to their occurrence. 警示可透過通知管理員或執行作業 (或兩者) 來回應事件。An alert can respond to an event by notifying an administrator or running a job, or both. 警示也可以將事件轉送到在另一部電腦上登入的 Microsoft Windows 應用程式。An alert can also forward an event to the Microsoft Windows application log on a different computer. 例如,如果發生嚴重性 19 的事件,您可以指定要立即通知操作員。For example, you can specify that an operator be notified immediately if an event of severity 19 occurs. 透過定義警示,資料庫管理員就可以更有效率地監視和管理 SQL ServerSQL ServerBy defining alerts, database administrators can more effectively monitor and manage SQL ServerSQL Server.

SQL ServerSQL Server Agent 只會回應已定義警示的事件。 Agent only responds to events for which an alert is defined. SQL ServerSQL Server Agent 用來監視事件的方法會取決於事件的類型。The method that SQL ServerSQL Server Agent uses to monitor events depends on the type of event.

對效能計數器定義了 SQL ServerSQL Server Agent 警示後, SQL ServerSQL Server Agent 就會直接監視效能計數器。When a SQL ServerSQL Server Agent alert is defined for a performance counter, SQL ServerSQL Server Agent directly monitors the performance counter. 若是 WMI 事件, SQL ServerSQL Server Agent 會登錄 WMI 事件的事件查詢。For a WMI event, SQL ServerSQL Server Agent registers an event query for the WMI event.

若要回應來自 SQL ServerSQL Server的訊息, SQL ServerSQL Server Agent 會監視 Windows 應用程式記錄檔。To respond to messages from SQL ServerSQL Server, SQL ServerSQL Server Agent monitors the Windows application log. SQL ServerSQL Server Agent 只會回應出現在這個記錄檔中的訊息。 Agent can only respond to messages that appear in this log. 根據預設,SQL Server 會將下列訊息記錄在 Windows 應用程式記錄檔中:By default, SQL Server logs the following messages in the Windows application log:

  • 嚴重性 19 或更高的 sysmessages 錯誤。Severity 19 or higher sysmessages errors.

    如果您還要記錄嚴重性低於 19 的特定 sysmessages 錯誤,請使用 sp_altermessage 預存程序將這類錯誤指定為「一律記錄」。If you also want to log specific sysmessages errors that have a severity lower than 19, use the sp_altermessage stored procedure to designate such errors as "always logged".

  • 使用 WITH LOG 語法叫用的任何 RAISERROR 陳述式。Any RAISERROR statement invoked by using the WITH LOG syntax.

    建議您使用 PAISERROR WITH LOG,從 SQL Server 執行個體寫入 Windows 應用程式記錄檔。Using RAISERROR WITH LOG is the recommended way to write to the Windows application log from an instance of SQL Server.

  • 使用 xp_logevent 記錄的任何應用程式事件。Any application event that is logged by using xp_logevent.

    注意

    記錄應用程式事件會消耗記錄檔空間,並且造成 Windows 應用程式記錄檔超出其大小上限。Logging application events consumes log space and can cause the Windows application log to exceed its maximum size. 請確定 Windows 應用程式記錄檔具有足夠的大小,以免遺失 SQL Server 事件資訊。Make sure that the maximum Windows application log size is large enough to avoid loss of SQL Server event information.

SQL ServerSQL Server 記錄訊息時, SQL ServerSQL Server Agent 服務會比較訊息和 SQL ServerSQL Server 管理員所定義的警示。When SQL ServerSQL Server logs a message, the SQL ServerSQL Server Agent service compares the message against the alerts defined by the SQL ServerSQL Server administrator.

不管事件的來源為何, SQL ServerSQL Server Agent 服務都會透過執行在事件警示中指定的工作來回應事件。Regardless of the source of the event, the SQL ServerSQL Server Agent service responds to the event by performing the tasks specified in the alert for the event.

另請參閱See Also

sp_altermessagesp_altermessage